bug-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: oddity in yyerrlab


From: Paul Eggert
Subject: Re: oddity in yyerrlab
Date: 04 Feb 2003 23:05:09 -0800
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/21.2

Andrew Suffield <address@hidden> writes:

> I can reproduce this at will locally, but since trapping it involves a
> patched version of gcc, it's rather difficult to provide a test case.

Thanks very much for reporting this bug.  Can you please try this patch?
(Note to Bison maintainers: data/lalr1.cc has the same bug, I but
didn't have time to fix that.)

2003-02-04  Paul Eggert  <address@hidden>

        * data/yacc.c (yyerrlab) [YYERROR_VERBOSE]:
        Avoid subscript error in yycheck.  Bug reported by Andrew Suffield.
        * data/glr.c (yyreportSyntaxError) [YYERROR_VERBOSE]: Likewise.
        Check for malloc failure, for consistency with yacc.c.
        (yytname_size): Remove, for consistency with yacc.c.

Index: data/glr.c
===================================================================
RCS file: /cvsroot/bison/bison/data/glr.c,v
retrieving revision 1.50
diff -p -u -r1.50 glr.c
--- data/glr.c  29 Jan 2003 23:37:15 -0000      1.50
+++ data/glr.c  5 Feb 2003 06:50:20 -0000
@@ -297,8 +297,6 @@ static const char *const yytname[] =
 {
   ]b4_tname[
 };
-
-#define yytname_size ((int) (sizeof (yytname) / sizeof (yytname[0])))
 #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;
-      int yyn, yyx, yycount;
-      size_t yysize;
+      int yyn;
       const char* yyprefix;
-      char* yyp;
-      char* yymsg;
       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.  */
-         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 ";
-         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)
-             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
-#endif
+#endif /* YYERROR_VERBOSE */
        yyerror (]b4_lyyerror_args["syntax error");
       yynerrs += 1;
     }
Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.51
diff -p -u -r1.51 yacc.c
--- data/yacc.c 22 Jan 2003 06:38:32 -0000      1.51
+++ data/yacc.c 5 Feb 2003 06:50:21 -0000
@@ -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 ";




reply via email to

[Prev in Thread] Current Thread [Next in Thread]