brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.5 KiB · a8f6f3b Raw
188 lines · c
1// RUN: %check_clang_tidy -std=c99,c11,c17 -check-suffixes=,BEFORE-23 %s bugprone-signal-handler %t -- -- -isystem %clang_tidy_headers2// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-signal-handler %t -- -- -isystem %clang_tidy_headers3 4#include "signal.h"5#include "stdlib.h"6#include "stdio.h"7#include "system-other.h"8 9// The function should be classified as standard function even if there is10// declaration the in source file.11// FIXME: The detection works only if the first declaration is in system12// header.13int printf(const char *, ...);14typedef void (*sighandler_t)(int);15sighandler_t signal(int signum, sighandler_t handler);16 17void f_extern(void);18void f_extern_handler(int);19 20void handler_printf(int) {21  printf("1234");22  // CHECK-NOTES: :[[@LINE-1]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]23  // CHECK-NOTES: :[[@LINE+4]]:18: note: function 'handler_printf' registered here as signal handler24}25 26void test_printf(void) {27  signal(SIGINT, handler_printf);28}29 30void handler_extern(int) {31  f_extern();32  // CHECK-NOTES: :[[@LINE-1]]:3: warning: cannot verify that external function 'f_extern' is asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]33  // CHECK-NOTES: :[[@LINE+4]]:18: note: function 'handler_extern' registered here as signal handler34}35 36void test_extern(void) {37  signal(SIGINT, handler_extern);38}39 40void f_ok(void) {41  abort();42}43 44void handler_ok(int) {45  f_ok();46}47 48void test_ok(void) {49  signal(SIGINT, handler_ok);50}51 52void f_bad(void) {53  printf("1234");54  // CHECK-NOTES: :[[@LINE-1]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]55  // CHECK-NOTES: :[[@LINE+5]]:3: note: function 'f_bad' called here from 'handler_bad'56  // CHECK-NOTES: :[[@LINE+8]]:18: note: function 'handler_bad' registered here as signal handler57}58 59void handler_bad(int) {60  f_bad();61}62 63void test_bad(void) {64  signal(SIGINT, handler_bad);65}66 67void f_bad1(void) {68  printf("1234");69  // CHECK-NOTES: :[[@LINE-1]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]70  // CHECK-NOTES: :[[@LINE+6]]:3: note: function 'f_bad1' called here from 'f_bad2'71  // CHECK-NOTES: :[[@LINE+9]]:3: note: function 'f_bad2' called here from 'handler_bad1'72  // CHECK-NOTES: :[[@LINE+13]]:18: note: function 'handler_bad1' registered here as signal handler73}74 75void f_bad2(void) {76  f_bad1();77}78 79void handler_bad1(int) {80  f_bad2();81  f_bad1();82}83 84void test_bad1(void) {85  signal(SIGINT, handler_bad1);86}87 88void handler_abort(int) {89  abort();90}91 92void handler_signal(int) {93  // FIXME: It is only OK to call signal with the current signal number.94  signal(0, SIG_DFL);95}96 97void handler_false_condition(int) {98  if (0)99    printf("1234");100  // CHECK-NOTES: :[[@LINE-1]]:5: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]101  // CHECK-NOTES: :[[@LINE+4]]:18: note: function 'handler_false_condition' registered here as signal handler102}103 104void test_false_condition(void) {105  signal(SIGINT, handler_false_condition);106}107 108void handler_multiple_calls(int) {109  f_extern();110  // CHECK-NOTES: :[[@LINE-1]]:3: warning: cannot verify that external function 'f_extern' is asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]111  // CHECK-NOTES: :[[@LINE+9]]:18: note: function 'handler_multiple_calls' registered here as signal handler112  printf("1234");113  // CHECK-NOTES: :[[@LINE-1]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]114  // CHECK-NOTES: :[[@LINE+6]]:18: note: function 'handler_multiple_calls' registered here as signal handler115  f_extern();116  // first 'f_extern' call found only117}118 119void test_multiple_calls(void) {120  signal(SIGINT, handler_multiple_calls);121}122 123void f_recursive(void);124 125void handler_recursive(int) {126  f_recursive();127  printf("");128  // first 'printf' call (in f_recursive) found only129}130 131void f_recursive(void) {132  f_extern();133  // CHECK-NOTES: :[[@LINE-1]]:3: warning: cannot verify that external function 'f_extern' is asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]134  // CHECK-NOTES: :[[@LINE-8]]:3: note: function 'f_recursive' called here from 'handler_recursive'135  // CHECK-NOTES: :[[@LINE+9]]:18: note: function 'handler_recursive' registered here as signal handler136  printf("");137  // CHECK-NOTES: :[[@LINE-1]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]138  // CHECK-NOTES: :[[@LINE-12]]:3: note: function 'f_recursive' called here from 'handler_recursive'139  // CHECK-NOTES: :[[@LINE+5]]:18: note: function 'handler_recursive' registered here as signal handler140  handler_recursive(2);141}142 143void test_recursive(void) {144  signal(SIGINT, handler_recursive);145}146 147void f_multiple_paths(void) {148  printf("");149  // CHECK-NOTES: :[[@LINE-1]]:3: warning: standard function 'printf' may not be asynchronous-safe; calling it from a signal handler may be dangerous [bugprone-signal-handler]150  // CHECK-NOTES: :[[@LINE+5]]:3: note: function 'f_multiple_paths' called here from 'handler_multiple_paths'151  // CHECK-NOTES: :[[@LINE+9]]:18: note: function 'handler_multiple_paths' registered here as signal handler152}153 154void handler_multiple_paths(int) {155  f_multiple_paths();156  f_multiple_paths();157}158 159void test_multiple_paths(void) {160  signal(SIGINT, handler_multiple_paths);161}162 163void handler_function_pointer(int) {164  void (*fp)(void) = f_extern;165  // Call with function pointer is not evalauted by the check.166  (*fp)();167}168 169void test_function_pointer(void) {170  signal(SIGINT, handler_function_pointer);171}172 173void test_other(void) {174  signal(SIGINT, handler_abort);175  signal(SIGINT, handler_signal);176 177  signal(SIGINT, _Exit);178#if __STDC_VERSION__ < 202311L179  signal(SIGINT, other_call);180  // CHECK-NOTES-BEFORE-23: :[[@LINE-1]]:18: warning: standard function 'other_call' may not be asynchronous-safe; using it as a signal handler may be dangerous [bugprone-signal-handler]181#endif182  signal(SIGINT, f_extern_handler);183  // CHECK-NOTES: :[[@LINE-1]]:18: warning: cannot verify that external function 'f_extern_handler' is asynchronous-safe; using it as a signal handler may be dangerous [bugprone-signal-handler]184 185  signal(SIGINT, SIG_IGN);186  signal(SIGINT, SIG_DFL);187}188