(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
torture/
boxed-ptr-1.c
       1  /* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */
       2  
       3  #include <stdlib.h>
       4  #include "../analyzer-decls.h"
       5  
       6  typedef struct boxed_ptr { void *value; } boxed_ptr;
       7  
       8  boxed_ptr __attribute__((noinline))
       9  boxed_malloc (size_t sz)
      10  {
      11    boxed_ptr result;
      12    result.value = malloc (sz);
      13    return result;
      14  }
      15  
      16  boxed_ptr __attribute__((noinline))
      17  boxed_free (boxed_ptr ptr)
      18  {
      19    free (ptr.value);
      20  }
      21  
      22  const boxed_ptr boxed_null = {NULL};
      23  
      24  boxed_ptr test_1 (int flag)
      25  {
      26    boxed_ptr ptr = boxed_malloc (sizeof (int));
      27  
      28    if (flag) /* { dg-message "following 'false' branch" } */
      29      if (!ptr.value)
      30        return boxed_null;
      31  
      32    *((int *)ptr.value) = 42; /* { dg-warning "dereference of possibly-NULL '\[^\n\r\]*'" } */
      33  
      34    return ptr;
      35  }
      36  
      37  void test_2 (int flag)
      38  {
      39    boxed_ptr ptr;
      40  
      41    if (flag)
      42      ptr = boxed_malloc (4096);
      43    else
      44      ptr = boxed_malloc (1024);
      45  
      46    __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
      47  
      48    boxed_free (ptr);  
      49  
      50    __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
      51  }
      52  
      53  void test_3 (int kind)
      54  {
      55    boxed_ptr ptr;
      56  
      57    switch (kind)
      58      {
      59      default:
      60        ptr = boxed_malloc (4096);
      61        break;
      62      case 0:
      63        ptr = boxed_malloc (128);
      64        break;
      65      case 1:
      66        ptr = boxed_malloc (1024);
      67        break;
      68      case 2:
      69        ptr = boxed_malloc (65536);
      70        break;
      71      }
      72  
      73    __analyzer_dump_exploded_nodes (0); /* { dg-warning "4 processed enodes" } */
      74  
      75    boxed_free (ptr);  
      76  
      77    __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
      78  }