]> git.saurik.com Git - bison.git/commitdiff
(yyreportSyntaxError) [YYERROR_VERBOSE]:
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 13 Feb 2003 07:14:29 +0000 (07:14 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 13 Feb 2003 07:14:29 +0000 (07:14 +0000)
Avoid subscript error in yycheck.  Bug reported by Andrew Suffield in
<http://mail.gnu.org/archive/html/bug-bison/2003-02/msg00003.html>.
Check for malloc failure, for consistency with yacc.c.
(yytname_size): Remove, for consistency with yacc.c.

data/glr.c

index 04842893853c78761ab6e836a6496596d8ce2954..c2b678bf157f2355f2f0c74524909a22830bf6ea 100644 (file)
@@ -297,8 +297,6 @@ static const char *const yytname[] =
 {
   ]b4_tname[
 };
 {
   ]b4_tname[
 };
-
-#define yytname_size ((int) (sizeof (yytname) / sizeof (yytname[0])))
 #endif
 
 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
 #endif
 
 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
@@ -1598,45 +1596,66 @@ yyreportSyntaxError (yyGLRStack* yystack,
     {
 #if YYERROR_VERBOSE
       yySymbol* const yytokenp = yystack->yytokenp;
     {
 #if YYERROR_VERBOSE
       yySymbol* const yytokenp = yystack->yytokenp;
-      int yyn, yyx, yycount;
-      size_t yysize;
+      int yyn;
       const char* yyprefix;
       const char* yyprefix;
-      char* yyp;
-      char* yymsg;
       yyn = yypact[yystack->yytops.yystates[0]->yylrState];
       if (YYPACT_NINF < yyn && yyn < YYLAST)
        {
       yyn = yypact[yystack->yytops.yystates[0]->yylrState];
       if (YYPACT_NINF < yyn && yyn < YYLAST)
        {
-         yycount = 0;
+         size_t yysize = 0;
+         char* yymsg;
+         int yyx;
+
          /* Start YYX at -YYN if negative to avoid negative indexes in
             YYCHECK.  */
          /* Start YYX at -YYN if negative to avoid negative indexes in
             YYCHECK.  */
-         yysize = sizeof ("syntax error, unexpected ")
-           + strlen (yytokenName (*yytokenp));
+         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;
+
          yyprefix = ", expecting ";
          yyprefix = ", expecting ";
-         for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size && yycount <= 5;
-              yyx += 1)
+         for (yyx = yyxbase; yyx < yyxlim && yycount < 5; yyx += 1)
            if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
            if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
-             yysize += strlen (yytokenName (yyx)) + strlen (yyprefix),
-               yycount += 1, yyprefix = " or ";
-         yymsg = yyp = (char*) malloc (yysize);
-         sprintf (yyp, "syntax error, unexpected %s",
-                  yytokenName (*yytokenp));
-         yyp += strlen (yyp);
-         if (yycount < 5)
-           {
-             yyprefix = ", expecting ";
-             for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size; yyx += 1)
-               if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
+             {
+               yysize += strlen (yytokenName (yyx)) + strlen (yyprefix);
+               yycount += 1;
+               if (yycount == 5)
                  {
                  {
-                   sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx));
-                   yyp += strlen (yyp);
-                   yyprefix = " or ";
+                   yysize = 0;
+                   break;
                  }
                  }
+               yyprefix = " or ";
+             }
+         yysize += (sizeof ("syntax error, unexpected ")
+                    + strlen (yytokenName (*yytokenp)));
+         yymsg = (char*) malloc (yysize);
+         if (yymsg != 0)
+           {
+             char* yyp = yymsg;
+             sprintf (yyp, "syntax error, unexpected %s",
+                      yytokenName (*yytokenp));
+             yyp += strlen (yyp);
+             if (yycount < 5)
+               {
+                 yyprefix = ", expecting ";
+                 for (yyx = yyxbase; yyx < yyxlim; yyx += 1)
+                   if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
+                     {
+                       sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx));
+                       yyp += strlen (yyp);
+                       yyprefix = " or ";
+                     }
+               }
+             yyerror (]b4_lyyerror_args[yymsg);
+             free (yymsg);
            }
            }
-         yyerror (]b4_lyyerror_args[yymsg);
-         free (yymsg);
+         else
+           yyerror (]b4_lyyerror_args["syntax error; also virtual memory exhausted");
        }
       else
        }
       else
-#endif
+#endif /* YYERROR_VERBOSE */
        yyerror (]b4_lyyerror_args["syntax error");
       yynerrs += 1;
     }
        yyerror (]b4_lyyerror_args["syntax error");
       yynerrs += 1;
     }