]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
json: Fix undefined behaviour when converting integer to double
authorDavid Woodhouse <dwmw2@infradead.org>
Sat, 1 May 2021 18:48:57 +0000 (19:48 +0100)
committerDavid Woodhouse <dwmw2@infradead.org>
Wed, 5 May 2021 22:12:21 +0000 (23:12 +0100)
Coverity doesn't like this, complaining that assignment to an object
with overlapping storage without exact overlap and compatible types can
cause undefined behaviour.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
json/json.c

index 71a42b8232c7f86e218ead762be7f0a15403d76e..5c961f982baed8bb5de0ba0adc159f8a6445b667 100644 (file)
@@ -783,10 +783,11 @@ json_value * json_parse_ex (json_settings * settings,
                      }
 
                      if (would_overflow(top->u.integer, b))
-                     {  -- num_digits;
+                     {  double dbl = (double)top->u.integer;
+                        -- num_digits;
                         -- state.ptr;
                         top->type = json_double;
-                        top->u.dbl = (double)top->u.integer;
+                        top->u.dbl = dbl;
                         continue;
                      }
 
@@ -816,13 +817,14 @@ json_value * json_parse_ex (json_settings * settings,
                }
                else if (b == '.' && top->type == json_integer)
                {
+                  double dbl = (double)top->u.integer;
                   if (!num_digits)
                   {  sprintf (error, "%d:%d: Expected digit before `.`", line_and_col);
                      goto e_failed;
                   }
 
                   top->type = json_double;
-                  top->u.dbl = (double) top->u.integer;
+                  top->u.dbl = dbl;
 
                   flags |= flag_num_got_decimal;
                   num_digits = 0;
@@ -847,8 +849,9 @@ json_value * json_parse_ex (json_settings * settings,
 
                      if (top->type == json_integer)
                      {
+                        double dbl = (double) top->u.integer;
                         top->type = json_double;
-                        top->u.dbl = (double) top->u.integer;
+                        top->u.dbl = dbl;
                      }
 
                      num_digits = 0;