From 8d048699ee6b33c68f06bc6d0a16e521a733a386 Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Fri, 5 May 2023 21:32:34 -0400 Subject: [PATCH 1/3] grc65 fix flawed text parsing Was using fseek(F,-1,SEEK_CUR) which is invalid for text files, behaviour unreliable across platforms. Added check for internal buffer overflow. --- src/grc65/main.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/grc65/main.c b/src/grc65/main.c index 349b5c110..ac654300d 100644 --- a/src/grc65/main.c +++ b/src/grc65/main.c @@ -850,8 +850,12 @@ static char *filterInput (FILE *F, char *tbl) /* loads file into buffer filtering it out */ int a, prevchar = -1, i = 0, bracket = 0, quote = 1; - for (;;) { - a = getc(F); + a = getc(F); + while (1) + { + if (i >= BLOODY_BIG_BUFFER) { + AbEnd ("File too large for internal parsing buffer (%d bytes).",BLOODY_BIG_BUFFER); + } if ((a == '\n') || (a == '\015')) a = ' '; if (a == ',' && quote) a = ' '; if (a == '\042') quote =! quote; @@ -873,13 +877,18 @@ static char *filterInput (FILE *F, char *tbl) if (a == ';' && quote) { do { a = getc (F); - } while (a != '\n'); - fseek (F, -1, SEEK_CUR); + } while (a != '\n' && a != EOF); + /* Don't discard this newline/EOF, continue to next loop. + ** A previous implementation used fseek(F,-1,SEEK_CUR), + ** which is invalid for text mode files, and was unreliable across platforms. + */ + continue; } else { tbl[i++] = a; prevchar = a; } } + a = getc(F); } if (bracket != 0) AbEnd ("There are unclosed brackets!"); From 1df7ab0352a43546c6ece76830594a687681c938 Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Sat, 6 May 2023 11:55:21 -0400 Subject: [PATCH 2/3] opening brace on same line as while other AbEnd messages don't end in . --- src/grc65/main.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/grc65/main.c b/src/grc65/main.c index ac654300d..adce3dc47 100644 --- a/src/grc65/main.c +++ b/src/grc65/main.c @@ -851,10 +851,9 @@ static char *filterInput (FILE *F, char *tbl) int a, prevchar = -1, i = 0, bracket = 0, quote = 1; a = getc(F); - while (1) - { + while (1) { if (i >= BLOODY_BIG_BUFFER) { - AbEnd ("File too large for internal parsing buffer (%d bytes).",BLOODY_BIG_BUFFER); + AbEnd ("File too large for internal parsing buffer (%d bytes)",BLOODY_BIG_BUFFER); } if ((a == '\n') || (a == '\015')) a = ' '; if (a == ',' && quote) a = ' '; From 532681c9613af15b1993e08db12761855c9707c6 Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Sat, 6 May 2023 12:06:06 -0400 Subject: [PATCH 3/3] braces were requested combining the two a = ' ' cases was requested --- src/grc65/main.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/grc65/main.c b/src/grc65/main.c index adce3dc47..7d31bfc52 100644 --- a/src/grc65/main.c +++ b/src/grc65/main.c @@ -855,12 +855,20 @@ static char *filterInput (FILE *F, char *tbl) if (i >= BLOODY_BIG_BUFFER) { AbEnd ("File too large for internal parsing buffer (%d bytes)",BLOODY_BIG_BUFFER); } - if ((a == '\n') || (a == '\015')) a = ' '; - if (a == ',' && quote) a = ' '; - if (a == '\042') quote =! quote; + if (((a == '\n') || (a == '\015')) || + (a == ',' && quote)) { + a = ' '; + } + if (a == '\042') { + quote =! quote; + } if (quote) { - if ((a == '{') || (a == '(')) bracket++; - if ((a == '}') || (a == ')')) bracket--; + if ((a == '{') || (a == '(')) { + bracket++; + } + if ((a == '}') || (a == ')')) { + bracket--; + } } if (a == EOF) { tbl[i] = '\0';