aboutsummaryrefslogtreecommitdiffstats
path: root/frontends/ilang
diff options
context:
space:
mode:
authorwhitequark <whitequark@whitequark.org>2020-04-06 10:32:02 +0000
committerwhitequark <whitequark@whitequark.org>2020-04-06 10:32:02 +0000
commitca70a1049ff18c8702fdc2ac4b3901685551cb28 (patch)
tree31df2eca62fa4ec581cb995d18361ab7b1e673db /frontends/ilang
parentcf716e1fff53fe4633dce98ebca04fe36c283106 (diff)
downloadyosys-ca70a1049ff18c8702fdc2ac4b3901685551cb28.tar.gz
yosys-ca70a1049ff18c8702fdc2ac4b3901685551cb28.tar.bz2
yosys-ca70a1049ff18c8702fdc2ac4b3901685551cb28.zip
read_ilang: detect overflow of integer literals.
Diffstat (limited to 'frontends/ilang')
-rw-r--r--frontends/ilang/ilang_lexer.l12
1 files changed, 11 insertions, 1 deletions
diff --git a/frontends/ilang/ilang_lexer.l b/frontends/ilang/ilang_lexer.l
index 4fd0ae855..e36818e4f 100644
--- a/frontends/ilang/ilang_lexer.l
+++ b/frontends/ilang/ilang_lexer.l
@@ -29,6 +29,7 @@
#pragma clang diagnostic ignored "-Wdeprecated-register"
#endif
+#include <cstdlib>
#include "frontends/ilang/ilang_frontend.h"
#include "ilang_parser.tab.hh"
@@ -88,7 +89,16 @@ USING_YOSYS_NAMESPACE
"."[0-9]+ { rtlil_frontend_ilang_yylval.string = strdup(yytext); return TOK_ID; }
[0-9]+'[01xzm-]* { rtlil_frontend_ilang_yylval.string = strdup(yytext); return TOK_VALUE; }
--?[0-9]+ { rtlil_frontend_ilang_yylval.integer = atoi(yytext); return TOK_INT; }
+-?[0-9]+ {
+ char *end = NULL;
+ long value = strtol(yytext, &end, 10);
+ if (end != yytext + strlen(yytext))
+ return TOK_INVALID; // literal out of range of long
+ if (value < INT_MIN || value > INT_MAX)
+ return TOK_INVALID; // literal out of range of int (relevant mostly for LP64 platforms)
+ rtlil_frontend_ilang_yylval.integer = value;
+ return TOK_INT;
+}
\" { BEGIN(STRING); }
<STRING>\\. { yymore(); }