From f4c6221eb119254e47f051c9bcb1f675db4dc010 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Tue, 15 Sep 2026 20:22:33 -0400 Subject: [PATCH] Fix underflow of `ALIGN[16, offset]` (#2090) --- src/asm/section.cpp | 13 +++++++++++-- test/asm/align-beyond-size.asm | 3 +++ test/asm/align-beyond-size.err | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/asm/align-beyond-size.asm create mode 100644 test/asm/align-beyond-size.err diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 4acf8b52..5157a6ec 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -717,8 +717,17 @@ void sect_AlignPC(uint8_t alignment, uint16_t offset) { } else if (alignment == 16) { // Treat an alignment large enough as fixing the address. // Note that this also ensures that a section's alignment never becomes 16 or greater. - sect->align = 0; // Reset the alignment, since we're fixing the address. - sect->org = offset - curOffset; + if (offset < curOffset) { + error( + "Section already contains %" PRIu32 + " bytes, higher than this aligned address $%04" PRIx32, + curOffset, + offset + ); + } else { + sect->align = 0; // Reset the alignment, since we're fixing the address. + sect->org = offset - curOffset; + } } else if (alignment > sect->align) { sect->align = alignment; // We need `(sect->alignOfs + curOffset) & alignMask == offset` diff --git a/test/asm/align-beyond-size.asm b/test/asm/align-beyond-size.asm new file mode 100644 index 00000000..a570f6d9 --- /dev/null +++ b/test/asm/align-beyond-size.asm @@ -0,0 +1,3 @@ +SECTION "test", ROM0 +db 1, 2, 3 +align 16, 2 diff --git a/test/asm/align-beyond-size.err b/test/asm/align-beyond-size.err new file mode 100644 index 00000000..73f24fc4 --- /dev/null +++ b/test/asm/align-beyond-size.err @@ -0,0 +1,3 @@ +error: Section already contains 3 bytes, higher than this aligned address $0002 + at align-beyond-size.asm(3) +Assembly aborted with 1 error