From 8ca1faa31f396d94ab927b257f3a05236c84e330 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sun, 17 May 2020 20:02:31 +0200 Subject: Don't call unsafe functions from SIGSEGV/SIGBUS signal handler. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GCC10 -fanalyzer notices that we try to call functions that are not signal safe from our fatal signal handler: bzip2.c: In function ‘mySIGSEGVorSIGBUScatcher’: bzip2.c:819:7: warning: call to ‘fprintf’ from within signal handler [CWE-479] [-Wanalyzer-unsafe-call-within-signal-handler] It also notices we then call showFileNames and cleanupAndFail which also call possibly not signal safe functions. Just write out the error message directly to STDERR and exit without trying to clean up any files. --- bzip2.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'bzip2.c') diff --git a/bzip2.c b/bzip2.c index d95d280..d1f2fa8 100644 --- a/bzip2.c +++ b/bzip2.c @@ -815,10 +815,9 @@ void mySignalCatcher ( IntNative n ) static void mySIGSEGVorSIGBUScatcher ( IntNative n ) { + const char *msg; if (opMode == OM_Z) - fprintf ( - stderr, - "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n" + msg = ": Caught a SIGSEGV or SIGBUS whilst compressing.\n" "\n" " Possible causes are (most likely first):\n" " (1) This computer has unreliable memory or cache hardware\n" @@ -834,12 +833,9 @@ void mySIGSEGVorSIGBUScatcher ( IntNative n ) " bug report should have. If the manual is available on your\n" " system, please try and read it before mailing me. If you don't\n" " have the manual or can't be bothered to read it, mail me anyway.\n" - "\n", - progName ); - else - fprintf ( - stderr, - "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n" + "\n"; + else + msg = ": Caught a SIGSEGV or SIGBUS whilst decompressing.\n" "\n" " Possible causes are (most likely first):\n" " (1) The compressed data is corrupted, and bzip2's usual checks\n" @@ -857,13 +853,25 @@ void mySIGSEGVorSIGBUScatcher ( IntNative n ) " bug report should have. If the manual is available on your\n" " system, please try and read it before mailing me. If you don't\n" " have the manual or can't be bothered to read it, mail me anyway.\n" - "\n", - progName ); - - showFileNames(); - if (opMode == OM_Z) - cleanUpAndFail( 3 ); else - { cadvise(); cleanUpAndFail( 2 ); } + "\n"; + write ( STDERR_FILENO, "\n", 1 ); + write ( STDERR_FILENO, progName, strlen ( progName ) ); + write ( STDERR_FILENO, msg, strlen ( msg ) ); + + msg = "\tInput file = "; + write ( STDERR_FILENO, msg, strlen (msg) ); + write ( STDERR_FILENO, inName, strlen (inName) ); + write ( STDERR_FILENO, "\n", 1 ); + msg = "\tOutput file = "; + write ( STDERR_FILENO, msg, strlen (msg) ); + write ( STDERR_FILENO, outName, strlen (outName) ); + write ( STDERR_FILENO, "\n", 1 ); + + /* Don't call cleanupAndFail. If we ended up here something went + terribly wrong. Trying to clean up might fail spectacularly. */ + + if (opMode == OM_Z) setExit(3); else setExit(2); + _exit(exitValue); } -- cgit v1.2.3-55-g6feb