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