(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
malloc-reuse.c
       1  /* { dg-require-effective-target alloca } */
       2  
       3  #include <stdlib.h>
       4  #include "analyzer-decls.h"
       5  
       6  /* Multiple calls to malloc (without a free) should return non-equal pointers.  */
       7  
       8  void test_1 (void)
       9  {
      10    void *p, *q;
      11    p = malloc (1024);
      12    if (!p)
      13      return;
      14    q = malloc (1024);
      15    __analyzer_eval (p == q); /* { dg-warning "FALSE" } */
      16    free (p);
      17    free (q);
      18  }
      19  
      20  /* Multiple calls to malloc with a free might or might not
      21     return the same pointer.  */
      22  
      23  void test_2 (void)
      24  {
      25    void *p, *q;
      26    p = malloc (1024);
      27    if (!p)
      28      return;
      29    free (p);
      30    
      31    q = malloc (1024);
      32    __analyzer_eval (p == q); /* { dg-warning "UNKNOWN" "ideal" { xfail *-*-* } } */
      33    /* { dg-bogus "FALSE" "status quo" { xfail *-*-* } .-1 } */
      34    // TODO: ideally this should be UNKNOWN
      35    free (q);
      36  }
      37  
      38  void test_two_malloc_sites_same_size (int flag)
      39  {
      40    void *p;
      41    if (flag)
      42      p = malloc (1024);
      43    else
      44      p = malloc (1024);
      45    __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
      46    free (p);
      47  }
      48  
      49  void test_two_malloc_sites_different_sizes (int flag)
      50  {
      51    void *p;
      52    if (flag)
      53      p = malloc (4096);
      54    else
      55      p = malloc (1024);
      56    __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
      57    free (p);
      58  }