I compiled m4 1.4.17 with -fsanitize=undefined
enabled. -fsanitize=undefined is a compiler flag that enables runtime
checks for different types of undefined behavior.
It turned out that a test 189.eval fails with m4 compiled with this option.

The problem is signed integer overflow. I fixed this problem in the same
manner as it is done in evaluation of expressions.

See attached patch.
commit 3471b9ef4286f725937ca14fb373a21166edc3df
Author: Ivan Sorokin <vanya...@gmail.com>
Date:   Tue Dec 29 02:25:39 2015 +0300

    fix signed integer overflow on 189.eval

diff --git a/src/builtin.c b/src/builtin.c
index b3700c3..01bc826 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -1152,7 +1152,7 @@ m4_incr (struct obstack *obs, int argc, token_data **argv)
   if (!numeric_arg (argv[0], ARG (1), &value))
     return;
 
-  shipout_int (obs, value + 1);
+  shipout_int (obs, (uint32_t)value + 1);
 }
 
 static void
diff --git a/src/eval.c b/src/eval.c
index 8b4b05a..b04d6d9 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -177,7 +177,7 @@ eval_lex (int32_t *val)
           else if (digit >= base)
             break;
           else
-            *val = *val * base + digit;
+            *val = (uint32_t)*val * (uint32_t)base + (uint32_t)digit;
         }
       return NUMBER;
     }

Reply via email to