From c5e8e4ff83c70ddf32f00bfa69f0d4f918af3c15 Mon Sep 17 00:00:00 2001 From: dbrotz <43593771+dbrotz@users.noreply.github.com> Date: Mon, 9 Sep 2019 17:27:56 -0700 Subject: [PATCH] Reject input that contains null characters Null characters in the middle of strings interact badly with the RGBDS codebase, which assumes null-terminated strings. There is no reason to support null characters in input source code, so the simplest way to deal with null characters is to reject them early. --- src/asm/lexer.c | 8 ++++++++ test/asm/null-in-macro.out | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/asm/lexer.c b/src/asm/lexer.c index e5fd594e..e26019c5 100644 --- a/src/asm/lexer.c +++ b/src/asm/lexer.c @@ -269,6 +269,7 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f) /* Convert all line endings to LF and spaces */ char *mem = pBuffer->pBuffer; + int32_t lineCount = 0; while (*mem) { if ((mem[0] == '\\') && (mem[1] == '\"' || mem[1] == '\\')) { @@ -279,15 +280,22 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f) || ((mem[0] == '\r') && (mem[1] == '\n'))) { *mem++ = ' '; *mem++ = '\n'; + lineCount++; /* LF and CR */ } else if ((mem[0] == '\n') || (mem[0] == '\r')) { *mem++ = '\n'; + lineCount++; } else { mem++; } } } + if (mem != pBuffer->pBuffer + size) { + nLineNo = lineCount + 1; + fatalerror("Found null character"); + } + /* Remove comments */ mem = pBuffer->pBuffer; diff --git a/test/asm/null-in-macro.out b/test/asm/null-in-macro.out index 13f603d2..90943ee0 100644 --- a/test/asm/null-in-macro.out +++ b/test/asm/null-in-macro.out @@ -1,2 +1,2 @@ -ERROR: null-in-macro.asm(1): - Unterminated MACRO definition. +ERROR: null-in-macro.asm(2): + Found null character