When parsing constants which don't fit into an arith, handle overflow properly dtrg-mips
authorDavid Given <dg@cowlark.com>
Sat, 22 Sep 2018 10:45:59 +0000 (12:45 +0200)
committerDavid Given <dg@cowlark.com>
Sat, 22 Sep 2018 10:45:59 +0000 (12:45 +0200)
(and don't turn them all into INT_MAX...).

mach/proto/mcg/mcg.h
mach/proto/mcg/parse_em.c

index 06d444d..0bd9928 100644 (file)
@@ -8,6 +8,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 #include "em_arith.h"
 #include "em_label.h"
 #include "em.h"
index bb6d6f1..e8859d2 100644 (file)
@@ -211,6 +211,19 @@ static void data_block_label(const char* label)
        }
 }
 
+static arith safe_atol(const char* s)
+{
+       arith result;
+
+       errno = 0;
+       result = strtoul(s, NULL, 0);
+       if (errno == ERANGE)
+               result = strtol(s, NULL, 0);
+       if (errno == ERANGE)
+               fatal("constant '%s' not parseable", s);
+       return result;
+}
+
 static void parse_pseu(void)
 {
        switch (em.em_opcode)
@@ -255,7 +268,7 @@ static void parse_pseu(void)
                                case ico_ptyp:
                                case uco_ptyp:
                 {
-                    arith val = atol(em.em_string);
+                    arith val = safe_atol(em.em_string);
                     data_int(val, em.em_size, ro);
                     data_block_int(val);
                     break;
@@ -313,7 +326,7 @@ static void parse_pseu(void)
                                case ico_ptyp:
                                case uco_ptyp:
                 {
-                    arith val = atol(em.em_string);
+                    arith val = safe_atol(em.em_string);
                     data_int(val, em.em_size, false);
                     break;
                 }