From e287ee27249835ed75e4a0c49aded2b14508e52e Mon Sep 17 00:00:00 2001 From: Rangi Date: Sat, 22 Aug 2026 18:48:36 -0400 Subject: [PATCH] Fix C++ UB from negating INT_MIN with macro `shift INT_MIN` --- src/asm/macro.cpp | 2 +- test/asm/shift-int-min.asm | 6 ++++++ test/asm/shift-int-min.err | 2 ++ test/asm/shift-int-min.out | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/asm/shift-int-min.asm create mode 100644 test/asm/shift-int-min.err create mode 100644 test/asm/shift-int-min.out diff --git a/src/asm/macro.cpp b/src/asm/macro.cpp index b4999288..2a6b60ae 100644 --- a/src/asm/macro.cpp +++ b/src/asm/macro.cpp @@ -72,7 +72,7 @@ void MacroArgs::shiftArgs(int32_t count) { count > 0 && (static_cast(count) > nbArgs || shift > nbArgs - count)) { warning(WARNING_MACRO_SHIFT, "Cannot shift macro arguments past their end"); shift = nbArgs; - } else if (count < 0 && shift < static_cast(-count)) { + } else if (count < 0 && (count == INT32_MIN || shift < static_cast(-count))) { warning(WARNING_MACRO_SHIFT, "Cannot shift macro arguments past their beginning"); shift = 0; } else { diff --git a/test/asm/shift-int-min.asm b/test/asm/shift-int-min.asm new file mode 100644 index 00000000..d4f061c8 --- /dev/null +++ b/test/asm/shift-int-min.asm @@ -0,0 +1,6 @@ +MACRO m + println \1 + shift $8000_0000 ; INT32_MIN + println \1 +ENDM + m 1, 2, 3 diff --git a/test/asm/shift-int-min.err b/test/asm/shift-int-min.err new file mode 100644 index 00000000..15557ee3 --- /dev/null +++ b/test/asm/shift-int-min.err @@ -0,0 +1,2 @@ +warning: Cannot shift macro arguments past their beginning [-Wmacro-shift] + at shift-int-min.asm::m(3) <- shift-int-min.asm(6) diff --git a/test/asm/shift-int-min.out b/test/asm/shift-int-min.out new file mode 100644 index 00000000..961cedec --- /dev/null +++ b/test/asm/shift-int-min.out @@ -0,0 +1,2 @@ +$1 +$1