]> git.saurik.com Git - bison.git/commitdiff
(yyerrlab) [YYERROR_VERBOSE]:
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 13 Feb 2003 07:13:57 +0000 (07:13 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 13 Feb 2003 07:13:57 +0000 (07:13 +0000)
Avoid subscript error in yycheck.  Bug reported by Andrew Suffield in
<http://mail.gnu.org/archive/html/bug-bison/2003-02/msg00003.html>.

data/yacc.c

index 7a04a7d7a8acc76fe6b69b6fb91e387d87ff2d9e..e562da0a8c2e771ce137630f4b62f4d84f4dfed5 100644 (file)
@@ -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 ";