| 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 |
void test (void) |
| 27 |
{ |
| 28 |
void *ptr = malloc (1024); |
| 29 |
signal(SIGINT, int_handler); |
| 30 |
body_of_program(); |
| 31 |
free (ptr); |
| 32 |
} |
| 33 |
|
| 34 |
/* "call to 'fprintf' from within signal handler [CWE-479]". */ |
| 35 |
/* { dg-begin-multiline-output "" } |
| 36 |
NN | fprintf(stderr, "LOG: %s", msg); |
| 37 |
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 |
'test': events 1-2 |
| 39 |
| |
| 40 |
| NN | void test (void) |
| 41 |
| | ^~~~ |
| 42 |
| | | |
| 43 |
| | (1) entry to 'test' |
| 44 |
|...... |
| 45 |
| NN | signal(SIGINT, int_handler); |
| 46 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 |
| | | |
| 48 |
| | (2) registering 'int_handler' as signal handler |
| 49 |
| |
| 50 |
event 3 |
| 51 |
| |
| 52 |
|cc1: |
| 53 |
| (3): later on, when the signal is delivered to the process |
| 54 |
| |
| 55 |
+--> 'int_handler': events 4-5 |
| 56 |
| |
| 57 |
| NN | static void int_handler(int signum) |
| 58 |
| | ^~~~~~~~~~~ |
| 59 |
| | | |
| 60 |
| | (4) entry to 'int_handler' |
| 61 |
| NN | { |
| 62 |
| NN | custom_logger("got signal"); |
| 63 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 |
| | | |
| 65 |
| | (5) calling 'custom_logger' from 'int_handler' |
| 66 |
| |
| 67 |
+--> 'custom_logger': events 6-7 |
| 68 |
| |
| 69 |
| NN | void custom_logger(const char *msg) |
| 70 |
| | ^~~~~~~~~~~~~ |
| 71 |
| | | |
| 72 |
| | (6) entry to 'custom_logger' |
| 73 |
| NN | { |
| 74 |
| NN | fprintf(stderr, "LOG: %s", msg); |
| 75 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 76 |
| | | |
| 77 |
| | (7) call to 'fprintf' from within signal handler |
| 78 |
| |
| 79 |
{ dg-end-multiline-output "" } */ |