| 1 |
/* Verify how paths are printed for signal-handler diagnostics. */ |
| 2 |
|
| 3 |
/* C only; attempting to generalize it for C++ leads |
| 4 |
to an explosion of possibilities for the multiline output. */ |
| 5 |
|
| 6 |
/* { dg-options "-fanalyzer -fdiagnostics-show-line-numbers -fdiagnostics-path-format=inline-events -fdiagnostics-show-caret" } */ |
| 7 |
/* { dg-enable-nn-line-numbers "" } */ |
| 8 |
/* { dg-require-effective-target signal } */ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <signal.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
extern void body_of_program(void); |
| 15 |
|
| 16 |
void custom_logger(const char *msg) |
| 17 |
{ |
| 18 |
fprintf(stderr, "LOG: %s", msg); /* { dg-warning "call to 'fprintf' from within signal handler" } */ |
| 19 |
} |
| 20 |
|
| 21 |
static void int_handler(int signum) |
| 22 |
{ |
| 23 |
custom_logger("got signal"); |
| 24 |
} |
| 25 |
|
| 26 |
static void __analyzer_register_handler () |
| 27 |
{ |
| 28 |
signal(SIGINT, int_handler); |
| 29 |
} |
| 30 |
|
| 31 |
void test (void) |
| 32 |
{ |
| 33 |
__analyzer_register_handler (); |
| 34 |
body_of_program(); |
| 35 |
} |
| 36 |
|
| 37 |
/* "call to 'fprintf' from within signal handler [CWE-479]". */ |
| 38 |
/* { dg-begin-multiline-output "" } |
| 39 |
NN | fprintf(stderr, "LOG: %s", msg); |
| 40 |
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 41 |
'test': events 1-2 |
| 42 |
| |
| 43 |
| NN | void test (void) |
| 44 |
| | ^~~~ |
| 45 |
| | | |
| 46 |
| | (1) entry to 'test' |
| 47 |
| NN | { |
| 48 |
| NN | __analyzer_register_handler (); |
| 49 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 50 |
| | | |
| 51 |
| | (2) calling '__analyzer_register_handler' from 'test' |
| 52 |
| |
| 53 |
+--> '__analyzer_register_handler': events 3-4 |
| 54 |
| |
| 55 |
| NN | static void __analyzer_register_handler () |
| 56 |
| | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 57 |
| | | |
| 58 |
| | (3) entry to '__analyzer_register_handler' |
| 59 |
| NN | { |
| 60 |
| NN | signal(SIGINT, int_handler); |
| 61 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 62 |
| | | |
| 63 |
| | (4) registering 'int_handler' as signal handler |
| 64 |
| |
| 65 |
event 5 |
| 66 |
| |
| 67 |
|cc1: |
| 68 |
| (5): later on, when the signal is delivered to the process |
| 69 |
| |
| 70 |
+--> 'int_handler': events 6-7 |
| 71 |
| |
| 72 |
| NN | static void int_handler(int signum) |
| 73 |
| | ^~~~~~~~~~~ |
| 74 |
| | | |
| 75 |
| | (6) entry to 'int_handler' |
| 76 |
| NN | { |
| 77 |
| NN | custom_logger("got signal"); |
| 78 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 79 |
| | | |
| 80 |
| | (7) calling 'custom_logger' from 'int_handler' |
| 81 |
| |
| 82 |
+--> 'custom_logger': events 8-9 |
| 83 |
| |
| 84 |
| NN | void custom_logger(const char *msg) |
| 85 |
| | ^~~~~~~~~~~~~ |
| 86 |
| | | |
| 87 |
| | (8) entry to 'custom_logger' |
| 88 |
| NN | { |
| 89 |
| NN | fprintf(stderr, "LOG: %s", msg); |
| 90 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 91 |
| | | |
| 92 |
| | (9) call to 'fprintf' from within signal handler |
| 93 |
| |
| 94 |
{ dg-end-multiline-output "" } */ |