[前][次][番号順一覧][スレッド一覧]

ruby-changes:65129

From: Kenta <ko1@a...>
Date: Wed, 3 Feb 2021 18:25:29 +0900 (JST)
Subject: [ruby-changes:65129] 8df1881c8f (master): [ruby/bigdecimal] Fix the maximum length of float number

https://git.ruby-lang.org/ruby.git/commit/?id=8df1881c8f

From 8df1881c8fc9c173963e8f7d0d078e8d56640903 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
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 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) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2856
     }
 
     /* 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.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]