shithub: rgbds

Download patch

ref: 6be3584467ee5b56d7c7f857f8cbc70cd460d411
parent: 8c90d9d2d70c3f7872730c467d3151704f06bcb5
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Thu Apr 15 15:52:50 EDT 2021

LexerState's 'size' and 'offset' for mmapped files are unsigned

These were using signed 'off_t' because that is the type of
'st_size' from 'stat()', but neither one can be negative.

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -309,7 +309,7 @@
 		char const *unowned;
 		char *owned;
 	} contents;
-	size_t len;
+	size_t len; /* Length of the contents */
 	size_t totalLen;
 	size_t distance; /* Distance between the beginning of this expansion and of its parent */
 	bool owned; /* Whether or not to free contents when this expansion is freed */
@@ -329,8 +329,8 @@
 	union {
 		struct { /* If mmap()ed */
 			char *ptr; /* Technically `const` during the lexer's execution */
-			off_t size;
-			off_t offset;
+			size_t size;
+			size_t offset;
 			bool isReferenced; /* If a macro in this file requires not unmapping it */
 		};
 		struct { /* Otherwise */
@@ -501,7 +501,8 @@
 			state->isMmapped = true;
 			state->isReferenced = false; // By default, a state isn't referenced
 			state->ptr = mappingAddr;
-			state->size = fileInfo.st_size;
+			assert(fileInfo.st_size >= 0);
+			state->size = (size_t)fileInfo.st_size;
 			state->offset = 0;
 
 			if (verbose)
@@ -876,7 +877,7 @@
 	if (lexerState->macroArgScanDistance > 0)
 		return c;
 
-	lexerState->macroArgScanDistance = 1; /* Do not consider again */
+	lexerState->macroArgScanDistance++; /* Do not consider again */
 
 	if (c == '\\' && !lexerState->disableMacroArgs) {
 		/* If character is a backslash, check for a macro arg */