1 // RUN: %check_clang_tidy %s bugprone-signal-handler %t -- -- -isystem %S/Inputs/Headers
2
3 #include "signal.h"
4 #include "stdlib.h"
5 #include "stdio.h"
6 #include "system-other.h"
7
8 // The function should be classified as system call even if there is
9 // declaration the in source file.
10 // FIXME: The detection works only if the first declaration is in system
11 // header.
12 int printf(const char *, ...);
13 typedef void (*sighandler_t)(int);
14 sighandler_t signal(int signum, sighandler_t handler);
15
handler_abort(int)16 void handler_abort(int) {
17 abort();
18 }
19
handler_other(int)20 void handler_other(int) {
21 printf("1234");
22 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]
23 }
24
handler_signal(int)25 void handler_signal(int) {
26 // FIXME: It is only OK to call signal with the current signal number.
27 signal(0, SIG_DFL);
28 }
29
f_ok()30 void f_ok() {
31 abort();
32 }
33
f_bad()34 void f_bad() {
35 printf("1234");
36 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]
37 }
38
39 void f_extern();
40
handler_ok(int)41 void handler_ok(int) {
42 f_ok();
43 }
44
handler_bad(int)45 void handler_bad(int) {
46 f_bad();
47 }
48
handler_extern(int)49 void handler_extern(int) {
50 f_extern();
51 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'f_extern' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]
52 }
53
test()54 void test() {
55 signal(SIGINT, handler_abort);
56 signal(SIGINT, handler_signal);
57 signal(SIGINT, handler_other);
58
59 signal(SIGINT, handler_ok);
60 signal(SIGINT, handler_bad);
61 signal(SIGINT, handler_extern);
62
63 signal(SIGINT, _Exit);
64 signal(SIGINT, other_call);
65 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 'other_call' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]
66
67 signal(SIGINT, SIG_IGN);
68 signal(SIGINT, SIG_DFL);
69 }
70