shithub: rgbds

Download patch

ref: bad66e54fad5c9d3773d30ae14eda94db44b4894
parent: 5cb6c4af4bdf4f9cbcab531fa85f9a07130f3dd9
author: dbrotz <43593771+dbrotz@users.noreply.github.com>
date: Sat Dec 1 02:10:59 EST 2018

Fix buffer overflow when file ends with \

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -145,7 +145,7 @@
 	size = ftell(f);
 	fseek(f, 0, SEEK_SET);
 
-	pBuffer->pBufferRealStart = malloc(size + 2 + SAFETYMARGIN);
+	pBuffer->pBufferRealStart = malloc(size + 3 + SAFETYMARGIN);
 
 	if (pBuffer->pBufferRealStart == NULL)
 		fatalerror("%s: Out of memory for buffer!", __func__);
@@ -156,8 +156,9 @@
 	size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
 
 	pBuffer->pBuffer[size] = '\n';
-	pBuffer->pBuffer[size + 1] = 0;
-	pBuffer->nBufferSize = size + 1;
+	pBuffer->pBuffer[size + 1] = '\n'; /* in case the file ends with \ */
+	pBuffer->pBuffer[size + 2] = 0;
+	pBuffer->nBufferSize = size + 2;
 
 	/* Convert all line endings to LF and spaces */