summaryrefslogtreecommitdiff
path: root/ext/bigdecimal
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2021-01-29 18:06:13 +0900
committerKenta Murata <mrkn@mrkn.jp>2021-02-03 18:25:03 +0900
commit8df1881c8fc9c173963e8f7d0d078e8d56640903 (patch)
tree8e5c8b6ef55283d08400a68b3c00468ee1220942 /ext/bigdecimal
parentb4eba8dfee50a8b9085b32a1750be5313b9cf96b (diff)
[ruby/bigdecimal] Fix the maximum length of float number
This change is for preventing the false-positive alert by CoverityScan. See CID-1471770 for the detail. https://github.com/ruby/bigdecimal/commit/4d5b97125b
Diffstat (limited to 'ext/bigdecimal')
-rw-r--r--ext/bigdecimal/bigdecimal.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index c553e1d..e0832b8 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -2856,14 +2856,16 @@ rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
}
/* Use the same logic in flo_to_s to convert a float to a decimal string */
- char buf[DBLE_FIG + BASE_FIG + 2 + 1];
+ char buf[DBLE_FIG + BASE_FIG + 2 + 1]; /* sizeof(buf) == 28 in the typical case */
int decpt, negative_p;
char *e;
const int mode = digs == 0 ? 0 : 2;
char *p = BigDecimal_dtoa(d, mode, (int)digs, &decpt, &negative_p, &e);
int len10 = (int)(e - p);
- if (len10 >= (int)sizeof(buf))
- len10 = (int)sizeof(buf) - 1;
+ if (len10 > DBLE_FIG) {
+ /* TODO: Presumably, rounding should be done here. */
+ len10 = DBLE_FIG;
+ }
memcpy(buf, p, len10);
xfree(p);