From: David Given Date: Sat, 22 Sep 2018 10:45:59 +0000 (+0200) Subject: When parsing constants which don't fit into an arith, handle overflow properly X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fdtrg-mips;p=ack.git When parsing constants which don't fit into an arith, handle overflow properly (and don't turn them all into INT_MAX...). --- diff --git a/mach/proto/mcg/mcg.h b/mach/proto/mcg/mcg.h index 06d444d61..0bd9928df 100644 --- a/mach/proto/mcg/mcg.h +++ b/mach/proto/mcg/mcg.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "em_arith.h" #include "em_label.h" #include "em.h" diff --git a/mach/proto/mcg/parse_em.c b/mach/proto/mcg/parse_em.c index bb6d6f108..e8859d2fb 100644 --- a/mach/proto/mcg/parse_em.c +++ b/mach/proto/mcg/parse_em.c @@ -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; }