From 3b91f4c117003a9f42717fe88257b6025790169e Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin (Intel)" Date: Thu, 13 Dec 2018 13:55:25 -0800 Subject: malloc: handle potential infinite loop in nasm_alloc_failed() It is possible on memory exhaustion that nasm_fatal() might cause another allocation error, thus calling nasm_alloc_failed() again. If we find us in nasm_alloc_failed() for a second time, try to get a message out and then call abort(). Signed-off-by: H. Peter Anvin (Intel) --- Makefile.in | 2 +- Mkfiles/msvc.mak | 2 +- Mkfiles/openwcom.mak | 2 +- asm/nasm.c | 6 +++--- include/error.h | 5 +++++ nasmlib/malloc.c | 18 +++++++++++++++++- rdoff/ldrdf.c | 3 --- 7 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Makefile.in b/Makefile.in index 949d0131..32ef3d91 100644 --- a/Makefile.in +++ b/Makefile.in @@ -96,7 +96,7 @@ LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \ stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) \ \ nasmlib/ver.$(O) \ - nasmlib/crc64.$(O) nasmlib/malloc.$(O) \ + nasmlib/crc64.$(O) nasmlib/malloc.$(O) nasmlib/errfile.$(O) \ nasmlib/md5c.$(O) nasmlib/string.$(O) \ nasmlib/file.$(O) nasmlib/mmap.$(O) nasmlib/ilog2.$(O) \ nasmlib/realpath.$(O) nasmlib/path.$(O) \ diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak index 9aec80fc..c7bd9109 100644 --- a/Mkfiles/msvc.mak +++ b/Mkfiles/msvc.mak @@ -68,7 +68,7 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) \ stdlib\strnlen.$(O) stdlib\strrchrnul.$(O) \ \ nasmlib\ver.$(O) \ - nasmlib\crc64.$(O) nasmlib\malloc.$(O) \ + nasmlib\crc64.$(O) nasmlib\malloc.$(O) nasmlib\errfile.$(O) \ nasmlib\md5c.$(O) nasmlib\string.$(O) \ nasmlib\file.$(O) nasmlib\mmap.$(O) nasmlib\ilog2.$(O) \ nasmlib\realpath.$(O) nasmlib\path.$(O) \ diff --git a/Mkfiles/openwcom.mak b/Mkfiles/openwcom.mak index 084014bb..11582ecf 100644 --- a/Mkfiles/openwcom.mak +++ b/Mkfiles/openwcom.mak @@ -57,7 +57,7 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) & stdlib\strnlen.$(O) stdlib\strrchrnul.$(O) & & nasmlib\ver.$(O) & - nasmlib\crc64.$(O) nasmlib\malloc.$(O) & + nasmlib\crc64.$(O) nasmlib\malloc.$(O) nasmlib\errfile.$(O) & nasmlib\md5c.$(O) nasmlib\string.$(O) & nasmlib\file.$(O) nasmlib\mmap.$(O) nasmlib\ilog2.$(O) & nasmlib\realpath.$(O) nasmlib\path.$(O) & diff --git a/asm/nasm.c b/asm/nasm.c index ddc3404e..1825aa35 100644 --- a/asm/nasm.c +++ b/asm/nasm.c @@ -121,7 +121,7 @@ const struct ofmt *ofmt = &OF_DEFAULT; const struct ofmt_alias *ofmt_alias = NULL; const struct dfmt *dfmt; -static FILE *error_file; /* Where to write error messages */ +FILE *error_file; /* Where to write error messages */ FILE *ofile = NULL; struct optimization optimizing = @@ -455,6 +455,8 @@ int main(int argc, char **argv) timestamp(); + error_file = stderr; + iflag_set_default_cpu(&cpu); iflag_set_default_cpu(&cmd_cpu); @@ -462,8 +464,6 @@ int main(int argc, char **argv) want_usage = terminate_after_phase = false; nasm_set_verror(nasm_verror_asm); - error_file = stderr; - tolower_init(); src_init(); diff --git a/include/error.h b/include/error.h index 5a676e67..477a26d7 100644 --- a/include/error.h +++ b/include/error.h @@ -40,6 +40,11 @@ #include "compiler.h" +/* + * File pointer for error messages + */ +extern FILE *error_file; /* Error file descriptor */ + /* * An error reporting function should look like this. */ diff --git a/nasmlib/malloc.c b/nasmlib/malloc.c index ccbc0c75..dbb7384a 100644 --- a/nasmlib/malloc.c +++ b/nasmlib/malloc.c @@ -44,7 +44,23 @@ static no_return nasm_alloc_failed(void) { - nasm_fatal(0, "out of memory"); + /* If nasm_fatal() gets us back here, then croak hard */ + static bool already_here = false; + FILE *errfile; + + if (likely(!already_here)) { + already_here = true; + nasm_fatal(0, "out of memory!"); + } + + errfile = error_file; + if (!errfile) + error_file = stderr; + + fprintf(error_file, "nasm: out of memory!\n"); + fflush(error_file); + fflush(NULL); + abort(); } static inline void *validate_ptr(void *p) diff --git a/rdoff/ldrdf.c b/rdoff/ldrdf.c index dd80d70e..49729b4f 100644 --- a/rdoff/ldrdf.c +++ b/rdoff/ldrdf.c @@ -126,9 +126,6 @@ char *generic_rec_file = NULL; /* module name to be added at the beginning of output file */ char *modname_specified = NULL; -/* error file */ -static FILE *error_file; - /* the header of the output file, built up stage by stage */ rdf_headerbuf *newheader = NULL; -- cgit v1.2.1 From 26572c6e37279dd947df8d85b8d553c512f42467 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin (Intel)" Date: Thu, 13 Dec 2018 16:51:45 -0800 Subject: warnings: change WARN_MNP -> WARN_MACRO_PARAMS This one got missed during constant name conversion. Make the constants match the options. Signed-off-by: H. Peter Anvin (Intel) --- asm/preproc.c | 4 ++-- include/error.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/asm/preproc.c b/asm/preproc.c index af77e14b..af990e13 100644 --- a/asm/preproc.c +++ b/asm/preproc.c @@ -4285,7 +4285,7 @@ again: m->casesense))) m = m->next; if (!m) - nasm_warnf(ERR_PASS1|WARN_MNP, + nasm_warnf(ERR_PASS1|WARN_MACRO_PARAMS, "macro `%s' exists, " "but not taking %d parameters", mstart->text, nparam); @@ -4585,7 +4585,7 @@ static MMacro *is_mmacro(Token * tline, Token *** params_array) * After all that, we didn't find one with the right number of * parameters. Issue a warning, and fail to expand the macro. */ - nasm_warnf(ERR_PASS1|WARN_MNP, + nasm_warnf(ERR_PASS1|WARN_MACRO_PARAMS, "macro `%s' exists, but not taking %d parameters", tline->text, nparam); nasm_free(params); diff --git a/include/error.h b/include/error.h index a2146e97..d5b4f852 100644 --- a/include/error.h +++ b/include/error.h @@ -108,7 +108,7 @@ static inline vefunc nasm_set_verror(vefunc ve) #define WARN_MASK WARN(~0) #define WARN_IDX(x) ((x) >> WARN_SHR) -#define WARN_MNP WARN( 1) /* macro-num-parameters warning */ +#define WARN_MACRO_PARAMS WARN( 1) /* macro-num-parameters warning */ #define WARN_MACRO_SELFREF WARN( 2) /* macro self-reference */ #define WARN_MACRO_DEFAULTS WARN( 3) /* macro default parameters check */ #define WARN_ORPHAN_LABELS WARN( 4) /* orphan label (no colon, and alone on line) */ -- cgit v1.2.1