[Nasm-devel] [PATCH] preproc: don't unmacro if macro cannot be found.

Marco Vanotti mvanotti at dc.uba.ar
Fri Feb 4 08:58:10 PST 2022


This commit adds a check to see if the macro that we want to unmacro
exists.  A previous commit, introduced a check to see if the unmacro was
undefining a macro being expanded, but that same check included a null
pointer dereference if the macro to undefine did not exist.

The following code reproduced the issue:

```asm
%macro baz 0
  %unmacro F 0
%endmacro
baz
```

Compile with:
```shell
$ nasm -f elf64 -g -FDWARF -o tmp.o -werror file.asm
```

Fixes bug 3392761
Signed-off-by: Marco Vanotti <mvanotti at dc.uba.ar>
---
 asm/preproc.c      | 5 +++++
 test/br3392761.asm | 7 +++++++
 2 files changed, 12 insertions(+)
 create mode 100644 test/br3392761.asm

diff --git a/asm/preproc.c b/asm/preproc.c
index cb1669da..545c257b 100644
--- a/asm/preproc.c
+++ b/asm/preproc.c
@@ -4330,6 +4330,11 @@ issue_error:
             goto done;
         }
         mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
+        if (mmac_p == NULL) {
+          // If the macro cannot be found, there's nothing to do.
+          free_tlist(spec.dlist);
+          break;
+        }
 
         /* Check the macro to be undefined is not being expanded */
         list_for_each(l, istk->expansion) {
diff --git a/test/br3392761.asm b/test/br3392761.asm
new file mode 100644
index 00000000..b5ba6d77
--- /dev/null
+++ b/test/br3392761.asm
@@ -0,0 +1,7 @@
+;Testname=undefine-missing-macro; Arguments=-felf64 -obr3392761.o; Files=stderr
+; This test needs to be run with address sanitizer, with leak sanitizer disabled.
+; Something like: ASAN_OPTIONS=detect_leaks=0 ./performtest.pl --nasm=../nasm br3392761.asm
+%macro baz 0
+   %unmacro F 0
+%endmacro
+baz
-- 
2.25.1



More information about the Nasm-devel mailing list