From 8df1881c8fc9c173963e8f7d0d078e8d56640903 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 29 Jan 2021 18:06:13 +0900 Subject: [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 --- ext/bigdecimal/bigdecimal.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index c553e1d748..e0832b82bf 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); -- cgit v1.2.3