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