From 967dc2cf3f57b0a3d0f8dddffc7585d12724fec0 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 3 Jan 2021 00:00:00 +0000 Subject: [PATCH] Replaces assertion of user input to validation. --- biner.l | 31 +++++++++++++++---------------- biner.y | 4 +++- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/biner.l b/biner.l index bd7cea9..c98bf4b 100644 --- a/biner.l +++ b/biner.l @@ -4,6 +4,7 @@ #include #include #include +#include #include "./tree.h" #include "./zone.h" @@ -11,7 +12,7 @@ #include "generated/biner.y.h" static inline biner_zone_ptr(char) strnew_(void); -static inline intmax_t parse_int_(int base); +static inline bool parse_int_(intmax_t* v, int base); static inline void count_(void); %} @@ -21,7 +22,8 @@ H [0-9A-Fa-f] %% -"//".* count_(); +[ \t\r\n]+ count_(); +"//".* count_(); [/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] count_(); /* TODO: detect unterminated comment */ "==" { count_(); return EQUAL; } @@ -40,27 +42,24 @@ H [0-9A-Fa-f] {I}({I}|{D})* { yylval.ptr = strnew_(); count_(); return IDENT; } -{D}+ { yylval.i = parse_int_(10); count_(); return INTEGER; } -0[xX]{H}+ { yylval.i = parse_int_(16); count_(); return INTEGER; } +{D}+ { return parse_int_(&yylval.i, 10)? INTEGER: OVERFLOWN_INTEGER; } +0[xX]{H}+ { return parse_int_(&yylval.i, 16)? INTEGER: OVERFLOWN_INTEGER; } -[\+\-\*\/\!\=\<\>\.\(\)[\]\{\}\;\,\&\|\~\^] { count_(); return yytext[0]; } - -(.|\n) count_(); +. { count_(); return yytext[0]; } %% static inline uintptr_t strnew_(void) { return biner_zone_strnew(&biner_tree_parse_context_.zone, yytext); } -static inline intmax_t parse_int_(int base) { - char* end = NULL; - const intmax_t v = strtoimax(yytext, &end, base); +static inline bool parse_int_(intmax_t* v, int base) { + assert(v != NULL); - /* TODO: replace asserts with throwing error */ - assert((v != INTMAX_MIN && v != INTMAX_MAX) || errno != ERANGE); - assert(INT64_MIN <= v && v <= INT64_MAX); - assert(end != NULL && *end == 0); - - return v; + char* end = NULL; + *v = strtoimax(yytext, &end, base); + return + ((*v != INTMAX_MIN && *v != INTMAX_MAX) || errno != ERANGE) && + (INT64_MIN <= *v && *v <= INT64_MAX) && + (end != NULL && *end == 0); } static inline void count_(void) { const char* s = yytext; diff --git a/biner.y b/biner.y index 4be1f55..daa95af 100644 --- a/biner.y +++ b/biner.y @@ -90,10 +90,12 @@ resolve_constant_( uintptr_t ptr; } +%token OVERFLOWN_INTEGER + %token EQUAL NEQUAL LESS_EQUAL GREATER_EQUAL AND OR BIT_LSHIFT BIT_RSHIFT %token CONST ENUM STRUCT UNION %token IDENT -%token INTEGER; +%token INTEGER %type decl_list decl %type enum_member_list enum_member