Skip to content

Commit 3780314

Browse files
committed
test-signalhandler: test SIGFPE
1 parent 7002b45 commit 3780314

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

test/signal/test-signalhandler.cpp

Lines changed: 20 additions & 9 deletions
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

test/signal/test-signalhandler.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def test_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
@@ -50,7 +50,7 @@ def test_abort():
5050
_, stdout, _ = __call_process('abort')
5151
lines = stdout.splitlines()
5252
assert lines[0] == 'Internal error: cppcheck received signal SIGABRT - abort or assertion'
53-
# no stacktrace on MaCos
53+
# no stacktrace on macOS
5454
if sys.platform != "darwin":
5555
assert lines[1] == 'Callstack:'
5656
assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function
@@ -65,21 +65,20 @@ def test_segv():
6565
assert lines[0] == 'Internal error: cppcheck received signal SIGSEGV - SEGV_MAPERR (at 0x0).'
6666
else:
6767
assert lines[0] == 'Internal error: cppcheck received signal SIGSEGV - SEGV_MAPERR (reading at 0x0).'
68-
# no stacktrace on MacOS
68+
# no stacktrace on macOS
6969
if sys.platform != "darwin":
7070
assert lines[1] == 'Callstack:'
7171
assert lines[2].endswith('my_segv()'), lines[2] # TODO: wrong function
7272
assert lines[len(lines)-1] == 'Please report this to the cppcheck developers!'
7373

7474

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

0 commit comments

Comments
 (0)