shithub: rgbds

Download patch

ref: b27b821e7f7bf1c3e0ce8c07cb047cca6cf09434
parent: d9ecaabac1f49ebf074d1170777135bd6359b3a7
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Aug 15 10:34:47 EDT 2020

Fix RAW lexer length underflow

Also added an assertion to check against more such overflows

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -792,6 +792,7 @@
 		/* Wrap around if necessary */
 		if (lexerState->index >= LEXER_BUF_SIZE)
 			lexerState->index %= LEXER_BUF_SIZE;
+		assert(lexerState->nbChars >= distance);
 		lexerState->nbChars -= distance;
 	}
 }
@@ -1467,9 +1468,7 @@
 		case '8':
 		case '9':
 			readNumber(10, c - '0');
-			int perhapsPeriod = peek(0);
-
-			if (perhapsPeriod == '.') {
+			if (peek(0) == '.') {
 				shiftChars(1);
 				readFractionalPart();
 			}
@@ -1597,8 +1596,11 @@
 				i--;
 			/* Empty macro args break their expansion, so prevent that */
 			if (i == 0) {
+				/* Return the EOF token, and don't shift a non-existent char! */
+				if (c == EOF)
+					return 0;
 				shiftChars(1);
-				return c == EOF ? 0 : c;
+				return c;
 			}
 			yylval.tzString[i] = '\0';
 			dbgPrint("Read raw string \"%s\"\n", yylval.tzString);