Skip to content

Commit 7aaddf5

Browse files
committed
Fix buffer overflow problem when reporting a syntax error with a very long
input line
1 parent d55bb74 commit 7aaddf5

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

Diff for: lib/cgraph/scan.l

+15-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
%{
1717
#include <grammar.h>
1818
#include <cghdr.h>
19+
#include <agxbuf.h>
1920
#include <ctype.h>
2021
#define GRAPH_EOF_TOKEN '@' /* lex class must be defined below */
2122
/* this is a workaround for linux flex */
@@ -191,13 +192,21 @@ ID ({NAME}|{NUMBER})
191192
%%
192193
void yyerror(char *str)
193194
{
195+
unsigned char xbuf[BUFSIZ];
194196
char buf[BUFSIZ];
195-
if (InputFile)
196-
sprintf(buf,"%s:%d: %s in line %d near '%s'\n",InputFile, line_num,
197-
str,line_num,yytext);
198-
else
199-
sprintf(buf," %s in line %d near '%s'\n", str,line_num,yytext);
200-
agerr(AGWARN,buf);
197+
agxbuf xb;
198+
199+
agxbinit(&xb, BUFSIZ, xbuf);
200+
if (InputFile) {
201+
agxbput (&xb, InputFile);
202+
agxbput (&xb, ": ");
203+
}
204+
sprintf(buf," %s in line %d near '", str,line_num);
205+
agxbput (&xb, buf);
206+
agxbput (&xb, yytext);
207+
agxbput (&xb,"'\n");
208+
agerr(AGWARN,agxbuse(&xb));
209+
agxbfree(&xb);
201210
}
202211
/* must be here to see flex's macro defns */
203212
void aglexeof() { unput(GRAPH_EOF_TOKEN); }

0 commit comments

Comments
 (0)