Skip to content

Commit

Permalink
fix memory leak of MMacro::name on macro undefining
Browse files Browse the repository at this point in the history
When running with -fsanitize=leak enabled nasm prints this error:

ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6 byte(s) in 1 object(s) allocated from:
    #0 0x7f17d8a60867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    netwide-assembler#1 0x5613fd57401c in nasm_malloc nasmlib/alloc.c:55
    netwide-assembler#2 0x5613fd5be840 in dup_text asm/preproc.c:436
    netwide-assembler#3 0x5613fd5dc71d in parse_mmacro_spec asm/preproc.c:3325
    netwide-assembler#4 0x5613fd5e5f7a in do_directive asm/preproc.c:4615
    netwide-assembler#5 0x5613fd5f5e19 in pp_tokline asm/preproc.c:7766
    netwide-assembler#6 0x5613fd5f5e19 in pp_getline asm/preproc.c:7830
    netwide-assembler#7 0x5613fd56e678 in assemble_file asm/nasm.c:1722
    netwide-assembler#8 0x5613fd568801 in main asm/nasm.c:719
    netwide-assembler#9 0x7f17d8178d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    netwide-assembler#10 0x7f17d8178e3f in __libc_start_main_impl ../csu/libc-start.c:392
    netwide-assembler#11 0x5613fd56acd4 in _start (/home/ivan/d/nasm/nasm+0x2e5cd4)

SUMMARY: AddressSanitizer: 6 byte(s) leaked in 1 allocation(s).

This error was reproducible on align13s.asm test.

The problem was caused by the fact that do_directive didn't
cleaup properly the macro name returned from parse_mmacro_spec.
  • Loading branch information
sorokin committed Feb 26, 2023
1 parent a916e41 commit 49030a2
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions asm/preproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3318,6 +3318,7 @@ static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
#if 0
def->prev = NULL;
#endif
nasm_assert(!def->name);
def->name = dup_text(tline);
def->plus = false;
def->nolist = 0;
Expand Down Expand Up @@ -4615,12 +4616,14 @@ static int do_directive(Token *tline, Token **output)
if (!mmac_p) {
/* No such macro */
free_tlist(spec.dlist);
nasm_free(spec.name);
break;
}

/* Check the macro to be undefined is not being expanded */
list_for_each(l, istk->expansion) {
if (l->finishes == *mmac_p) {
nasm_free(spec.name);
nasm_nonfatal("`%%unmacro' can't undefine the macro being expanded");
/*
* Do not release the macro instance to avoid using the freed
Expand All @@ -4644,6 +4647,7 @@ static int do_directive(Token *tline, Token **output)
}
}
free_tlist(spec.dlist);
nasm_free(spec.name);
break;
}

Expand Down

0 comments on commit 49030a2

Please sign in to comment.