From a2c52caca24584b732c4e5c13dce78df9f0241d1 Mon Sep 17 00:00:00 2001 From: Rangi Date: Sat, 22 Aug 2026 19:08:47 -0400 Subject: [PATCH] Disallow NUL characters in section names and assertion messages Allowing these in object files would lose anything after the '\0' when RGBLINK reads the object. --- src/asm/output.cpp | 4 ++++ src/asm/section.cpp | 4 ++++ test/asm/assert-nul.asm | 1 + test/asm/assert-nul.err | 2 ++ test/asm/section-name-nul.asm | 2 ++ test/asm/section-name-nul.err | 2 ++ test/link/script.asm | 2 +- 7 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/asm/assert-nul.asm create mode 100644 test/asm/assert-nul.err create mode 100644 test/asm/section-name-nul.asm create mode 100644 test/asm/section-name-nul.err diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 5d017888..2c22cd09 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -162,6 +162,10 @@ void out_CreatePatch(uint32_t type, Expression const &expr, uint32_t ofs, uint32 void out_CreateAssert( AssertionType type, Expression const &expr, std::string const &message, uint32_t ofs ) { + if (message.find('\0') != std::string::npos) { + fatal("Assertion messages cannot contain '\\0' characters"); + } + Assertion &assertion = assertions.emplace_front(); initPatch(assertion.patch, type, expr, ofs); diff --git a/src/asm/section.cpp b/src/asm/section.cpp index c9723f58..ea35ae39 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -544,6 +544,10 @@ void sect_NewSection( SectionSpec const &attrs, SectionModifier mod ) { + if (name.find('\0') != std::string::npos) { + fatal("Section names cannot contain '\\0' characters"); + } + for (SectionStackEntry &entry : sectionStack) { if (entry.section && entry.section->name == name) { fatal("Section \"%s\" is already on the stack", name.c_str()); diff --git a/test/asm/assert-nul.asm b/test/asm/assert-nul.asm new file mode 100644 index 00000000..3be84803 --- /dev/null +++ b/test/asm/assert-nul.asm @@ -0,0 +1 @@ +assert x, "oops \0 null" diff --git a/test/asm/assert-nul.err b/test/asm/assert-nul.err new file mode 100644 index 00000000..1c6d2b87 --- /dev/null +++ b/test/asm/assert-nul.err @@ -0,0 +1,2 @@ +FATAL: Assertion messages cannot contain '\0' characters + at assert-nul.asm(1) diff --git a/test/asm/section-name-nul.asm b/test/asm/section-name-nul.asm new file mode 100644 index 00000000..e888cbcf --- /dev/null +++ b/test/asm/section-name-nul.asm @@ -0,0 +1,2 @@ +SECTION "test\0 foo", ROM0 +SECTION "test\0 bar", ROM0 diff --git a/test/asm/section-name-nul.err b/test/asm/section-name-nul.err new file mode 100644 index 00000000..99fb73a4 --- /dev/null +++ b/test/asm/section-name-nul.err @@ -0,0 +1,2 @@ +FATAL: Section names cannot contain '\0' characters + at section-name-nul.asm(1) diff --git a/test/link/script.asm b/test/link/script.asm index 44f175c9..a173a9d9 100644 --- a/test/link/script.asm +++ b/test/link/script.asm @@ -7,4 +7,4 @@ SECTION "ROM2 1K", ROMX,BANK[2] ds $1000 SECTION "ROM2 1", ROMX,BANK[2] ds 1 -SECTION "\\\"\'\n\r\t\0", ROM0 +SECTION "\\\"\'\n\r\t", ROM0