File: signal-handler.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (228 lines) | stat: -rw-r--r-- 11,647 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// RUN: %check_clang_tidy -std=c++14 %s bugprone-signal-handler %t -- -- -isystem %clang_tidy_headers -isystem %S/Inputs/signal-handler -target x86_64-unknown-unknown
// FIXME: Fix the checker to work in C++17 or later mode.
#include "stdcpp.h"
#include "stdio.h"

// Functions called "signal" that are different from the system version.
typedef void (*callback_t)(int);
void signal(int, callback_t, int);
namespace ns {
void signal(int, callback_t);
}

extern "C" void handler_unsafe(int) {
  printf("xxx");
}

extern "C" void handler_unsafe_1(int) {
  printf("xxx");
}

namespace test_invalid_handler {

void handler_non_extern_c(int) {
  printf("xxx");
}

struct A {
  static void handler_member(int) {
    printf("xxx");
  }
};

void test() {
  std::signal(SIGINT, handler_unsafe_1);
  // CHECK-MESSAGES: :[[@LINE-17]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:23: note: function 'handler_unsafe_1' registered here as signal handler

  std::signal(SIGINT, handler_non_extern_c);
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: functions without C linkage are not allowed as signal handler (until C++17) [bugprone-signal-handler]
  std::signal(SIGINT, A::handler_member);
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: functions without C linkage are not allowed as signal handler (until C++17) [bugprone-signal-handler]
  std::signal(SIGINT, [](int) { printf("xxx"); });
  // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: lambda function is not allowed as signal handler (until C++17) [bugprone-signal-handler]

  // This case is (deliberately) not found by the checker.
  std::signal(SIGINT, [](int) -> callback_t { return &handler_unsafe; }(1));
}

} // namespace test_invalid_handler

namespace test_non_standard_signal_call {

struct Signal {
  static void signal(int, callback_t);
};

void test() {
  // No diagnostics here. All these signal calls differ from the standard system one.
  signal(SIGINT, handler_unsafe, 1);
  ns::signal(SIGINT, handler_unsafe);
  Signal::signal(SIGINT, handler_unsafe);
  system_other::signal(SIGINT, handler_unsafe);
}

} // namespace test_non_standard_signal_call

namespace test_cpp_construct_in_handler {

struct Struct {
  virtual ~Struct() {}
  void f1();
  int *begin();
  int *end();
  static void f2();
};
struct Derived : public Struct {
};

struct X {
  X(int, float);
};

Struct *S_Global;
const Struct *S_GlobalConst;

void f_non_extern_c() {
}

void f_default_arg(int P1 = 0) {
}

extern "C" void handler_cpp(int) {
  using namespace ::test_cpp_construct_in_handler;

  // These calls are not found as problems.
  // (Called functions are not analyzed if the current function has already
  // other problems.)
  f_non_extern_c();
  Struct::f2();
  // 'auto' is not disallowed
  auto Auto = 28u;

  Struct S;
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:10: remark: internally, the statement is parsed as a 'CXXConstructExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  S_Global->f1();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXMemberCallExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  const Struct &SRef = Struct();
  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:24: remark: internally, the statement is parsed as a 'CXXBindTemporaryExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  X(3, 4.4);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXTemporaryObjectExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler

  auto L = [](int i) { printf("%d", i); };
  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:12: remark: internally, the statement is parsed as a 'CXXConstructExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  L(2);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXOperatorCallExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler

  try {
    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
    // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXTryStmt'
    // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
    int A;
  } catch (int) {
  };
  // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-3]]:5: remark: internally, the statement is parsed as a 'CXXCatchStmt'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler

  throw(12);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXThrowExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler

  for (int I : S) {
  }
  // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-3]]:3: remark: internally, the statement is parsed as a 'CXXForRangeStmt'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  // CHECK-MESSAGES: :[[@LINE-5]]:14: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-6]]:14: remark: internally, the statement is parsed as a 'CXXMemberCallExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler

  int Int = *(reinterpret_cast<int *>(&S));
  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:15: remark: internally, the statement is parsed as a 'CXXReinterpretCastExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  Int = static_cast<int>(12.34);
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:9: remark: internally, the statement is parsed as a 'CXXStaticCastExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  Derived *Der = dynamic_cast<Derived *>(S_Global);
  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:18: remark: internally, the statement is parsed as a 'CXXDynamicCastExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  Struct *SPtr = const_cast<Struct *>(S_GlobalConst);
  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:18: remark: internally, the statement is parsed as a 'CXXConstCastExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  Int = int(12.34);
  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:9: remark: internally, the statement is parsed as a 'CXXFunctionalCastExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler

  int *IPtr = new int[10];
  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:15: remark: internally, the statement is parsed as a 'CXXNewExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  delete[] IPtr;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXDeleteExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  IPtr = nullptr;
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:10: remark: internally, the statement is parsed as a 'CXXNullPtrLiteralExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  bool Bool = true;
  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:15: remark: internally, the statement is parsed as a 'CXXBoolLiteralExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
  f_default_arg();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:3: remark: internally, the statement is parsed as a 'CXXDefaultArgExpr'
  // CHECK-MESSAGES: :198:23: note: function 'handler_cpp' registered here as signal handler
}

void test() {
  std::signal(SIGINT, handler_cpp);
}

} // namespace test_cpp_construct_in_handler

namespace test_cpp_indirect {

void non_extern_c() {
  int *P = nullptr;
}

extern "C" void call_cpp_indirect() {
  int *P = nullptr;
  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C++-only construct is not allowed in signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE-2]]:12: remark: internally, the statement is parsed as a 'CXXNullPtrLiteralExpr'
  // CHECK-MESSAGES: :[[@LINE+8]]:3: note: function 'call_cpp_indirect' called here from 'handler_cpp_indirect'
  // CHECK-MESSAGES: :[[@LINE+11]]:23: note: function 'handler_cpp_indirect' registered here as signal handler
}

extern "C" void handler_cpp_indirect(int) {
  non_extern_c();
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: functions without C linkage are not allowed as signal handler (until C++17) [bugprone-signal-handler]
  // CHECK-MESSAGES: :[[@LINE+5]]:23: note: function 'handler_cpp_indirect' registered here as signal handler
  call_cpp_indirect();
}

void test() {
  std::signal(SIGINT, handler_cpp_indirect);
}

} // namespace test_cpp_indirect