From: Paul Eggert Date: Thu, 13 Feb 2003 07:13:57 +0000 (+0000) Subject: (yyerrlab) [YYERROR_VERBOSE]: X-Git-Tag: BISON-2_0~399 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/3aa2f55d71d530eb8752fbb32ac407dcc3ea1804?ds=inline (yyerrlab) [YYERROR_VERBOSE]: Avoid subscript error in yycheck. Bug reported by Andrew Suffield in . --- diff --git a/data/yacc.c b/data/yacc.c index 7a04a7d7..e562da0a 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -1039,17 +1039,34 @@ yyerrlab: YYSIZE_T yysize = 0; int yytype = YYTRANSLATE (yychar); char *yymsg; - int yyx, yycount; + int yyx; - yycount = 0; /* Start YYX at -YYN if negative to avoid negative indexes in YYCHECK. */ - for (yyx = yyn < 0 ? -yyn : 0; - yyx < (int) (sizeof (yytname) / sizeof (char *)); yyx++) + int yyxbase = yyn < 0 ? -yyn : 0; + + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = YYLAST - yyn; + int yynsyms = sizeof (yytname) / sizeof (yytname[0]); + int yyxlim = yychecklim < yynsyms ? yychecklim : yynsyms; + int yycount = 0; + + for (yyx = yyxbase; yyx < yyxlim; yyx++) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) - yysize += yystrlen (yytname[yyx]) + 15, yycount++; - yysize += yystrlen ("syntax error, unexpected ") + 1; - yysize += yystrlen (yytname[yytype]); + { + yysize += (! yycount + ? sizeof (", expecting ") - 1 + : sizeof (" or ") - 1); + yysize += yystrlen (yytname[yyx]); + yycount++; + if (yycount == 5) + { + yysize = 0; + break; + } + } + yysize += (sizeof ("syntax error, unexpected ") + + yystrlen (yytname[yytype])); yymsg = (char *) YYSTACK_ALLOC (yysize); if (yymsg != 0) { @@ -1059,9 +1076,7 @@ yyerrlab: if (yycount < 5) { yycount = 0; - for (yyx = yyn < 0 ? -yyn : 0; - yyx < (int) (sizeof (yytname) / sizeof (char *)); - yyx++) + for (yyx = yyxbase; yyx < yyxlim; yyx++) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) { const char *yyq = ! yycount ? ", expecting " : " or ";