wrc: Check for overflows when parsing integer constants.
diff --git a/tools/wrc/parser.l b/tools/wrc/parser.l index 0ef1b0a..6d5189a 100644 --- a/tools/wrc/parser.l +++ b/tools/wrc/parser.l
@@ -103,6 +103,7 @@ #include <string.h> #include <ctype.h> #include <assert.h> +#include <limits.h> #ifndef HAVE_UNISTD_H #define YY_NO_UNISTD_H @@ -295,6 +296,19 @@ return kwp; } +/* converts an integer in string form to an unsigned long and prints an error + * on overflow */ +static unsigned long xstrtoul(const char *nptr, char **endptr, int base) +{ + unsigned long l; + + errno = 0; + l = strtoul(nptr, endptr, base); + if (l == ULONG_MAX && errno == ERANGE) + parser_error("integer constant %s is too large\n", nptr); + return l; +} + %} /* @@ -378,9 +392,9 @@ \{ return tBEGIN; \} return tEND; -[0-9]+[lL]? { parser_lval.num = strtoul(yytext, 0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; } -0[xX][0-9A-Fa-f]+[lL]? { parser_lval.num = strtoul(yytext, 0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; } -0[oO][0-7]+[lL]? { parser_lval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; } +[0-9]+[lL]? { parser_lval.num = xstrtoul(yytext, 0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; } +0[xX][0-9A-Fa-f]+[lL]? { parser_lval.num = xstrtoul(yytext, 0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; } +0[oO][0-7]+[lL]? { parser_lval.num = xstrtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; } /* * The next two rules scan identifiers and filenames.