>From d1ddb2114fbd068f3919463a2af7d28ed92bd6ff Mon Sep 17 00:00:00 2001
From: Paul Eggert
Date: Thu, 17 Oct 2019 11:33:54 -0700
Subject: [PATCH 06/11] bison: check for int overflow when scanning
* src/scan-gram.l: Include errno.h, for errno.
(scan_integer, handle_syncline): Check for integer overflow.
* tests/input.at (too-large.y): Adjust to match new diagnostics.
---
src/scan-gram.l | 8 ++++++--
tests/input.at | 2 ++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/scan-gram.l b/src/scan-gram.l
index 861a58cb..7667648d 100644
--- a/src/scan-gram.l
+++ b/src/scan-gram.l
@@ -21,6 +21,8 @@
%option prefix="gram_" outfile="lex.yy.c"
%{
+#include
+
#include
#include
#include
@@ -818,9 +820,10 @@ scan_integer (char const *number, int base, location loc)
complain (&loc, Wyacc,
_("POSIX Yacc does not support hexadecimal literals"));
+ errno = 0;
long num = strtol (number, NULL, base);
- if (! (0 <= num && num <= INT_MAX))
+ if (! (0 <= num && num <= INT_MAX && errno == 0))
{
complain (&loc, complaint, _("integer out of range: %s"),
quote (number));
@@ -896,8 +899,9 @@ static void
handle_syncline (char *args, location loc)
{
char *file;
+ errno = 0;
long lineno = strtol (args, &file, 10);
- if (! (0 <= lineno && lineno <= INT_MAX))
+ if (! (0 <= lineno && lineno <= INT_MAX && errno == 0))
{
complain (&loc, Wother, _("line number overflow"));
lineno = INT_MAX;
diff --git a/tests/input.at b/tests/input.at
index f696f0cf..fe03f2e1 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -1523,7 +1523,9 @@ start: TOO_LARGE_DEC TOO_LARGE_HEX
AT_BISON_CHECK([too-large.y], [1], [],
[[too-large.y:9.22-42: error: integer out of range: '999999999999999999999'
+too-large.y:9.22-42: error: user token number of TOO_LARGE_DEC too large
too-large.y:10.24-44: error: integer out of range: '0xFFFFFFFFFFFFFFFFFFF'
+too-large.y:10.24-44: error: user token number of TOO_LARGE_HEX too large
]])
AT_BISON_OPTION_POPDEFS
--
2.21.0