When parsing constants which don't fit into an arith, handle overflow properly

(and don't turn them all into INT_MAX...).
This commit is contained in:
David Given 2018-09-22 12:45:59 +02:00
parent c6a1cf8de4
commit ed91ea4371
2 changed files with 16 additions and 2 deletions

View 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"

View 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;
}