shithub: rgbds

Download patch

ref: 72911ada2d254c08181b12d63016976e76f54eb4
parent: 037bc7abb3b7b85d38575cdecefdf7936318b1bb
author: ISSOtm <eldredhabert0@gmail.com>
date: Sun Feb 21 14:13:52 EST 2021

Prevent non-numeric symbol from overriding refs

Fixes #751

--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -365,8 +365,10 @@
  * Create a symbol that will be non-relocatable and ensure that it
  * hasn't already been defined or referenced in a context that would
  * require that it be relocatable
+ * @param symbolName The name of the symbol to create
+ * @param numeric If false, the symbol may not have been referenced earlier
  */
-static struct Symbol *createNonrelocSymbol(char const *symbolName)
+static struct Symbol *createNonrelocSymbol(char const *symbolName, bool numeric)
 {
 	struct Symbol *symbol = sym_FindExactSymbol(symbolName);
 
@@ -376,6 +378,12 @@
 		error("'%s' already defined at ", symbolName);
 		dumpFilename(symbol);
 		putc('\n', stderr);
+	} else if (!numeric) {
+		// The symbol has already been referenced, but it's not allowed
+		error("'%s' already referenced at ", symbolName);
+		dumpFilename(symbol);
+		putc('\n', stderr);
+		return NULL; // Don't allow overriding the symbol, that'd be bad!
 	}
 
 	return symbol;
@@ -386,8 +394,11 @@
  */
 struct Symbol *sym_AddEqu(char const *symName, int32_t value)
 {
-	struct Symbol *sym = createNonrelocSymbol(symName);
+	struct Symbol *sym = createNonrelocSymbol(symName, true);
 
+	if (!sym)
+		return NULL;
+
 	sym->type = SYM_EQU;
 	sym->value = value;
 
@@ -408,10 +419,12 @@
  */
 struct Symbol *sym_AddString(char const *symName, char const *value)
 {
-	struct Symbol *sym = createNonrelocSymbol(symName);
+	struct Symbol *sym = createNonrelocSymbol(symName, false);
 
-	assignStringSymbol(sym, value);
+	if (!sym)
+		return NULL;
 
+	assignStringSymbol(sym, value);
 	return sym;
 }
 
@@ -614,7 +627,10 @@
  */
 struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size)
 {
-	struct Symbol *sym = createNonrelocSymbol(symName);
+	struct Symbol *sym = createNonrelocSymbol(symName, false);
+
+	if (!sym)
+		return NULL;
 
 	sym->type = SYM_MACRO;
 	sym->macroSize = size;
--- /dev/null
+++ b/test/asm/ref-override-bad.asm
@@ -1,0 +1,13 @@
+
+SECTION "Bad!", ROM0
+
+	db W
+W equ 0 ; OK
+
+	db X
+X equs "0" ; Not OK
+
+	db Y
+Y: macro ; Not ok
+	db 0
+endm
--- /dev/null
+++ b/test/asm/ref-override-bad.err
@@ -1,0 +1,5 @@
+ERROR: ref-override-bad.asm(8):
+    'X' already referenced at ref-override-bad.asm(7)
+ERROR: ref-override-bad.asm(13):
+    'Y' already referenced at ref-override-bad.asm(10)
+error: Assembly aborted (2 errors)!