shithub: rgbds

Download patch

ref: 6c1ec59a5b7fd1a41d3bb8e213f0c5a8c4c7f3f1
parent: a060f135b83a1574e20a52d8aef8b21dd193238a
author: dbrotz <43593771+dbrotz@users.noreply.github.com>
date: Tue Dec 4 20:32:06 EST 2018

Use separate function to append newlines

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -6,6 +6,7 @@
  * SPDX-License-Identifier: MIT
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -110,6 +111,21 @@
 	free(buf);
 }
 
+/*
+ * Maintains the following invariants:
+ * 1. nBufferSize < capacity
+ * 2. The buffer is terminated with 0
+ * 3. nBufferSize is the size without the terminator
+ */
+static void yy_buffer_append(YY_BUFFER_STATE buf, uint32_t capacity, char c)
+{
+	assert(buf->pBuffer[buf->nBufferSize] == 0);
+	assert(buf->nBufferSize + 1 < capacity);
+
+	buf->pBuffer[buf->nBufferSize++] = c;
+	buf->pBuffer[buf->nBufferSize] = 0;
+}
+
 YY_BUFFER_STATE yy_scan_bytes(char *mem, uint32_t size)
 {
 	YY_BUFFER_STATE pBuffer = malloc(sizeof(struct yy_buffer_state));
@@ -145,8 +161,11 @@
 	size = ftell(f);
 	fseek(f, 0, SEEK_SET);
 
-	pBuffer->pBufferRealStart = malloc(size + 3 + SAFETYMARGIN);
+	/* Give extra room for 2 newlines and terminator */
+	uint32_t capacity = size + 3;
 
+	pBuffer->pBufferRealStart = malloc(capacity + SAFETYMARGIN);
+
 	if (pBuffer->pBufferRealStart == NULL)
 		fatalerror("%s: Out of memory for buffer!", __func__);
 
@@ -156,6 +175,7 @@
 	size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
 
 	pBuffer->pBuffer[size] = 0;
+	pBuffer->nBufferSize = size;
 
 	/* Convert all line endings to LF and spaces */
 
@@ -216,13 +236,9 @@
 		}
 	}
 
-	pBuffer->nBufferSize = size;
-
 	/* Add newline if file doesn't end with one */
-	if (size == 0 || pBuffer->pBuffer[size - 1] != '\n') {
-		pBuffer->pBuffer[pBuffer->nBufferSize] = '\n';
-		pBuffer->nBufferSize++;
-	}
+	if (size == 0 || pBuffer->pBuffer[size - 1] != '\n')
+		yy_buffer_append(pBuffer, capacity, '\n');
 
 	/* Add newline if \ will eat the last newline */
 	if (pBuffer->nBufferSize >= 2) {
@@ -232,13 +248,9 @@
 		while (pos > 0 && pBuffer->pBuffer[pos] == ' ')
 			pos--;
 
-		if (pBuffer->pBuffer[pos] == '\\') {
-			pBuffer->pBuffer[pBuffer->nBufferSize] = '\n';
-			pBuffer->nBufferSize++;
-		}
+		if (pBuffer->pBuffer[pos] == '\\')
+			yy_buffer_append(pBuffer, capacity, '\n');
 	}
-
-	pBuffer->pBuffer[pBuffer->nBufferSize] = 0;
 
 	pBuffer->oAtLineStart = 1;
 	return pBuffer;