From 37803148123640c662cdb385be2c64ce97b40c21 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 18 Jan 2025 17:58:49 +0100 Subject: [PATCH 1/2] test-signalhandler: test SIGFPE --- test/signal/test-signalhandler.cpp | 29 ++++++++++++++++++++--------- test/signal/test-signalhandler.py | 15 +++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/test/signal/test-signalhandler.cpp b/test/signal/test-signalhandler.cpp index 07a94290a44..a3e77b08e3c 100644 --- a/test/signal/test-signalhandler.cpp +++ b/test/signal/test-signalhandler.cpp @@ -16,17 +16,24 @@ * along with this program. If not, see . */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE // required to have feenableexcept() +#endif + #include "config.h" #if defined(USE_UNIX_SIGNAL_HANDLING) #include "signalhandler.h" #include -#include #include #include #include +#if !defined(__APPLE__) +#include +#endif + // static functions are omitted from trace /*static*/ NORETURN void my_assert() // NOLINT(misc-use-internal-linkage) @@ -45,15 +52,17 @@ ++*(int*)nullptr; } -/*static*/ void my_fpe() // NOLINT(misc-use-internal-linkage) -{ #if !defined(__APPLE__) - feenableexcept(FE_ALL_EXCEPT); // TODO: check result -#endif - std::feraiseexcept(FE_UNDERFLOW | FE_DIVBYZERO); // TODO: check result - // TODO: to generate this via code +/*static*/ int my_fpe() // NOLINT(misc-use-internal-linkage) +{ + if (feenableexcept(FE_ALL_EXCEPT) == -1) + return 2; + if (std::feraiseexcept(FE_ALL_EXCEPT) != 0) + return 3; + return 1 % -1; } #endif +#endif int main(int argc, const char * const argv[]) { @@ -67,10 +76,12 @@ int main(int argc, const char * const argv[]) my_assert(); else if (strcmp(argv[1], "abort") == 0) my_abort(); - else if (strcmp(argv[1], "fpe") == 0) - my_fpe(); else if (strcmp(argv[1], "segv") == 0) my_segv(); +#if !defined(__APPLE__) + else if (strcmp(argv[1], "fpe") == 0) + return my_fpe(); +#endif return 0; #else diff --git a/test/signal/test-signalhandler.py b/test/signal/test-signalhandler.py index f1261278816..c4f21bf9447 100644 --- a/test/signal/test-signalhandler.py +++ b/test/signal/test-signalhandler.py @@ -36,10 +36,10 @@ def test_assert(): if sys.platform == "darwin": assert stderr.startswith("Assertion failed: (false), function my_assert, file test-signalhandler.cpp, line "), stderr else: - assert stderr.endswith("test-signalhandler.cpp:34: void my_assert(): Assertion `false' failed.\n"), stderr + assert stderr.endswith("test-signalhandler.cpp:41: void my_assert(): Assertion `false' failed.\n"), stderr lines = stdout.splitlines() assert lines[0] == 'Internal error: cppcheck received signal SIGABRT - abort or assertion' - # no stacktrace of MacOs + # no stacktrace of macOS if sys.platform != "darwin": assert lines[1] == 'Callstack:' assert lines[2].endswith('my_abort()'), lines[2] # TODO: wrong function @@ -50,7 +50,7 @@ def test_abort(): _, stdout, _ = __call_process('abort') lines = stdout.splitlines() assert lines[0] == 'Internal error: cppcheck received signal SIGABRT - abort or assertion' - # no stacktrace on MaCos + # no stacktrace on macOS if sys.platform != "darwin": assert lines[1] == 'Callstack:' assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function @@ -65,21 +65,20 @@ def test_segv(): assert lines[0] == 'Internal error: cppcheck received signal SIGSEGV - SEGV_MAPERR (at 0x0).' else: assert lines[0] == 'Internal error: cppcheck received signal SIGSEGV - SEGV_MAPERR (reading at 0x0).' - # no stacktrace on MacOS + # no stacktrace on macOS if sys.platform != "darwin": assert lines[1] == 'Callstack:' assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' -# TODO: make this work -@pytest.mark.skip +@pytest.mark.skipif(sys.platform == 'darwin', reason='Cannot raise FPE on macOS') def test_fpe(): _, stdout, stderr = __call_process('fpe') assert stderr == '' lines = stdout.splitlines() - assert lines[0].startswith('Internal error: cppcheck received signal SIGFPE - FPE_FLTDIV (at 0x7f'), lines[0] + assert lines[0].startswith('Internal error: cppcheck received signal SIGFPE - FPE_FLTINV (at 0x'), lines[0] assert lines[0].endswith(').'), lines[0] assert lines[1] == 'Callstack:' - assert lines[2].endswith('my_fpe()'), lines[2] + assert lines[3].endswith('my_fpe()'), lines[2] assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' From ef72a3c6fe34f1c0cb75cf768e5dc291fc4e244d Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 18 Jan 2025 18:12:25 +0100 Subject: [PATCH 2/2] test-signalhandler: assert exitcode --- test/signal/test-signalhandler.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/signal/test-signalhandler.py b/test/signal/test-signalhandler.py index c4f21bf9447..62ab6d79c02 100644 --- a/test/signal/test-signalhandler.py +++ b/test/signal/test-signalhandler.py @@ -32,7 +32,7 @@ def __call_process(arg): def test_assert(): - _, stdout, stderr = __call_process('assert') + exitcode, stdout, stderr = __call_process('assert') if sys.platform == "darwin": assert stderr.startswith("Assertion failed: (false), function my_assert, file test-signalhandler.cpp, line "), stderr else: @@ -44,10 +44,11 @@ def test_assert(): assert lines[1] == 'Callstack:' assert lines[2].endswith('my_abort()'), lines[2] # TODO: wrong function assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' + assert exitcode == -6 def test_abort(): - _, stdout, _ = __call_process('abort') + exitcode, stdout, _ = __call_process('abort') lines = stdout.splitlines() assert lines[0] == 'Internal error: cppcheck received signal SIGABRT - abort or assertion' # no stacktrace on macOS @@ -55,10 +56,11 @@ def test_abort(): assert lines[1] == 'Callstack:' assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' + assert exitcode == -6 def test_segv(): - _, stdout, stderr = __call_process('segv') + exitcode, stdout, stderr = __call_process('segv') assert stderr == '' lines = stdout.splitlines() if sys.platform == "darwin": @@ -70,11 +72,12 @@ def test_segv(): assert lines[1] == 'Callstack:' assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' + assert exitcode == -11 @pytest.mark.skipif(sys.platform == 'darwin', reason='Cannot raise FPE on macOS') def test_fpe(): - _, stdout, stderr = __call_process('fpe') + exitcode, stdout, stderr = __call_process('fpe') assert stderr == '' lines = stdout.splitlines() assert lines[0].startswith('Internal error: cppcheck received signal SIGFPE - FPE_FLTINV (at 0x'), lines[0] @@ -82,3 +85,4 @@ def test_fpe(): assert lines[1] == 'Callstack:' assert lines[3].endswith('my_fpe()'), lines[2] assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!' + assert exitcode == -8