From ed91ea43710f5f16c83c55389f1813efc3056f1e Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 22 Sep 2018 12:45:59 +0200 Subject: [PATCH] When parsing constants which don't fit into an arith, handle overflow properly (and don't turn them all into INT_MAX...). --- mach/proto/mcg/mcg.h | 1 + mach/proto/mcg/parse_em.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) 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; } -- 2.34.1