Skip to content

Commit ceafb41

Browse files
authored
test-signalhandler: test SIGFPE / assert exitcodes (#7238)
1 parent 0608ef7 commit ceafb41

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

Diff for: test/signal/test-signalhandler.cpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#ifndef _GNU_SOURCE
20+
#define _GNU_SOURCE // required to have feenableexcept()
21+
#endif
22+
1923
#include "config.h"
2024

2125
#if defined(USE_UNIX_SIGNAL_HANDLING)
2226
#include "signalhandler.h"
2327

2428
#include <cassert>
25-
#include <cfenv>
2629
#include <cstdio>
2730
#include <cstdlib>
2831
#include <cstring>
2932

33+
#if !defined(__APPLE__)
34+
#include <cfenv>
35+
#endif
36+
3037
// static functions are omitted from trace
3138

3239
/*static*/ NORETURN void my_assert() // NOLINT(misc-use-internal-linkage)
@@ -45,15 +52,17 @@
4552
++*(int*)nullptr;
4653
}
4754

48-
/*static*/ void my_fpe() // NOLINT(misc-use-internal-linkage)
49-
{
5055
#if !defined(__APPLE__)
51-
feenableexcept(FE_ALL_EXCEPT); // TODO: check result
52-
#endif
53-
std::feraiseexcept(FE_UNDERFLOW | FE_DIVBYZERO); // TODO: check result
54-
// TODO: to generate this via code
56+
/*static*/ int my_fpe() // NOLINT(misc-use-internal-linkage)
57+
{
58+
if (feenableexcept(FE_ALL_EXCEPT) == -1)
59+
return 2;
60+
if (std::feraiseexcept(FE_ALL_EXCEPT) != 0)
61+
return 3;
62+
return 1 % -1;
5563
}
5664
#endif
65+
#endif
5766

5867
int main(int argc, const char * const argv[])
5968
{
@@ -67,10 +76,12 @@ int main(int argc, const char * const argv[])
6776
my_assert();
6877
else if (strcmp(argv[1], "abort") == 0)
6978
my_abort();
70-
else if (strcmp(argv[1], "fpe") == 0)
71-
my_fpe();
7279
else if (strcmp(argv[1], "segv") == 0)
7380
my_segv();
81+
#if !defined(__APPLE__)
82+
else if (strcmp(argv[1], "fpe") == 0)
83+
return my_fpe();
84+
#endif
7485

7586
return 0;
7687
#else

Diff for: test/signal/test-signalhandler.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -32,54 +32,57 @@ def __call_process(arg):
3232

3333

3434
def test_assert():
35-
_, stdout, stderr = __call_process('assert')
35+
exitcode, stdout, stderr = __call_process('assert')
3636
if sys.platform == "darwin":
3737
assert stderr.startswith("Assertion failed: (false), function my_assert, file test-signalhandler.cpp, line "), stderr
3838
else:
39-
assert stderr.endswith("test-signalhandler.cpp:34: void my_assert(): Assertion `false' failed.\n"), stderr
39+
assert stderr.endswith("test-signalhandler.cpp:41: void my_assert(): Assertion `false' failed.\n"), stderr
4040
lines = stdout.splitlines()
4141
assert lines[0] == 'Internal error: cppcheck received signal SIGABRT - abort or assertion'
42-
# no stacktrace of MacOs
42+
# no stacktrace of macOS
4343
if sys.platform != "darwin":
4444
assert lines[1] == 'Callstack:'
4545
assert lines[2].endswith('my_abort()'), lines[2] # TODO: wrong function
4646
assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!'
47+
assert exitcode == -6
4748

4849

4950
def test_abort():
50-
_, stdout, _ = __call_process('abort')
51+
exitcode, stdout, _ = __call_process('abort')
5152
lines = stdout.splitlines()
5253
assert lines[0] == 'Internal error: cppcheck received signal SIGABRT - abort or assertion'
53-
# no stacktrace on MaCos
54+
# no stacktrace on macOS
5455
if sys.platform != "darwin":
5556
assert lines[1] == 'Callstack:'
5657
assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function
5758
assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!'
59+
assert exitcode == -6
5860

5961

6062
def test_segv():
61-
_, stdout, stderr = __call_process('segv')
63+
exitcode, stdout, stderr = __call_process('segv')
6264
assert stderr == ''
6365
lines = stdout.splitlines()
6466
if sys.platform == "darwin":
6567
assert lines[0] == 'Internal error: cppcheck received signal SIGSEGV - SEGV_MAPERR (at 0x0).'
6668
else:
6769
assert lines[0] == 'Internal error: cppcheck received signal SIGSEGV - SEGV_MAPERR (reading at 0x0).'
68-
# no stacktrace on MacOS
70+
# no stacktrace on macOS
6971
if sys.platform != "darwin":
7072
assert lines[1] == 'Callstack:'
7173
assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function
7274
assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!'
75+
assert exitcode == -11
7376

7477

75-
# TODO: make this work
76-
@pytest.mark.skip
78+
@pytest.mark.skipif(sys.platform == 'darwin', reason='Cannot raise FPE on macOS')
7779
def test_fpe():
78-
_, stdout, stderr = __call_process('fpe')
80+
exitcode, stdout, stderr = __call_process('fpe')
7981
assert stderr == ''
8082
lines = stdout.splitlines()
81-
assert lines[0].startswith('Internal error: cppcheck received signal SIGFPE - FPE_FLTDIV (at 0x7f'), lines[0]
83+
assert lines[0].startswith('Internal error: cppcheck received signal SIGFPE - FPE_FLTINV (at 0x'), lines[0]
8284
assert lines[0].endswith(').'), lines[0]
8385
assert lines[1] == 'Callstack:'
84-
assert lines[2].endswith('my_fpe()'), lines[2]
86+
assert lines[3].endswith('my_fpe()'), lines[2]
8587
assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!'
88+
assert exitcode == -8

0 commit comments

Comments
 (0)