(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wmismatched-dealloc-3.c
       1  /* Verify that Glibc <stdlib.h> declarations are handled correctly
       2     { dg-do compile }
       3     { dg-options "-Wall" } */
       4  
       5  #define A(...) __attribute__ ((malloc (__VA_ARGS__), noipa))
       6  
       7  typedef __SIZE_TYPE__ size_t;
       8  
       9  /* All functions with the same standard deallocator are associated
      10     with each other.  */
      11  void free (void*);
      12  void* calloc (size_t, size_t);
      13  void* malloc (size_t);
      14  void* realloc (void*, size_t);
      15  
      16  A (__builtin_free) void* aligned_alloc (size_t, size_t);
      17  
      18  /* Like realloc(), reallocarray() is both an allocator and a deallocator.
      19     It must be associated with both free() and with itself, but nothing
      20     else.  */
      21  A (__builtin_free) void* reallocarray (void*, size_t, size_t);
      22  A (reallocarray) void* reallocarray (void*, size_t, size_t);
      23  
      24  A (__builtin_free) extern char *canonicalize_file_name (const char*);
      25  
      26  
      27  void dealloc (void*);
      28  A (dealloc) void* alloc (size_t);
      29  
      30  
      31  void sink (void*);
      32  void* source (void);
      33  
      34  
      35  void test_builtin_aligned_alloc (void *p)
      36  {
      37    {
      38      void *q = __builtin_aligned_alloc (1, 2);
      39      sink (q);
      40      __builtin_free (q);
      41    }
      42  
      43    {
      44      void *q = __builtin_aligned_alloc (1, 2);
      45      sink (q);
      46      free (q);
      47    }
      48  
      49    {
      50      void *q = __builtin_aligned_alloc (1, 2);
      51      q = __builtin_realloc (q, 3);
      52      sink (q);
      53      free (q);
      54    }
      55  
      56    {
      57      void *q = __builtin_aligned_alloc (1, 2);
      58      q = realloc (q, 3);
      59      sink (q);
      60      free (q);
      61    }
      62  
      63    {
      64      void *q;
      65      q = __builtin_aligned_alloc (1, 2); // { dg-message "returned from '__builtin_aligned_alloc'" }
      66      sink (q);
      67      dealloc (q);                        // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
      68    }
      69  }
      70  
      71  
      72  void test_aligned_alloc (void *p)
      73  {
      74    {
      75      void *q = aligned_alloc (1, 2);
      76      sink (q);
      77      __builtin_free (q);
      78    }
      79  
      80    {
      81      void *q = aligned_alloc (1, 2);
      82      sink (q);
      83      free (q);
      84    }
      85  
      86    {
      87      void *q = aligned_alloc (1, 2);
      88      q = __builtin_realloc (q, 3);
      89      sink (q);
      90      free (q);
      91    }
      92  
      93    {
      94      void *q = aligned_alloc (1, 2);
      95      q = realloc (q, 3);
      96      sink (q);
      97      free (q);
      98    }
      99  
     100    {
     101      void *q = aligned_alloc (1, 2);     // { dg-message "returned from 'aligned_alloc'" }
     102      sink (q);
     103      dealloc (q);                        // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
     104    }
     105  }
     106  
     107  
     108  void test_reallocarray (void *p)
     109  {
     110    {
     111      void *q = __builtin_aligned_alloc (1, 2);
     112      q = reallocarray (q, 2, 3);
     113      sink (q);
     114      free (q);
     115    }
     116  
     117    {
     118      void *q = aligned_alloc (1, 2);
     119      q = reallocarray (q, 2, 3);
     120      sink (q);
     121      free (q);
     122    }
     123  
     124    {
     125      void *q = __builtin_calloc (1, 2);
     126      q = reallocarray (q, 2, 3);
     127      sink (q);
     128      free (q);
     129    }
     130  
     131    {
     132      void *q = calloc (1, 2);
     133      q = reallocarray (q, 2, 3);
     134      sink (q);
     135      free (q);
     136    }
     137  
     138    {
     139      void *q = __builtin_malloc (1);
     140      q = reallocarray (q, 2, 3);
     141      sink (q);
     142      free (q);
     143    }
     144  
     145    {
     146      void *q = malloc (1);
     147      q = reallocarray (q, 2, 3);
     148      sink (q);
     149      free (q);
     150    }
     151  
     152    {
     153      void *q = __builtin_realloc (p, 1);
     154      q = reallocarray (q, 2, 3);
     155      sink (q);
     156      free (q);
     157    }
     158  
     159    {
     160      p = source ();
     161      void *q = realloc (p, 1);
     162      q = reallocarray (q, 2, 3);
     163      sink (q);
     164      free (q);
     165    }
     166  
     167    {
     168      void *q = __builtin_strdup ("abc");
     169      q = reallocarray (q, 3, 4);
     170      sink (q);
     171      free (q);
     172    }
     173  
     174    {
     175      void *q = __builtin_strndup ("abcd", 3);
     176      q = reallocarray (q, 4, 5);
     177      sink (q);
     178      free (q);
     179    }
     180  
     181    {
     182      void *q = source ();
     183      q = reallocarray (q, 5, 6);
     184      sink (q);
     185      free (q);
     186    }
     187  
     188    {
     189      void *q = alloc (1);                // { dg-message "returned from 'alloc'" }
     190      q = reallocarray (q, 6, 7);         // { dg-warning "'reallocarray' called on pointer returned from a mismatched allocation function" }
     191      sink (q);
     192      free (q);
     193    }
     194  
     195    {
     196      p = source ();
     197      void *q = reallocarray (p, 7, 8);
     198      q = __builtin_realloc (q, 9);
     199      sink (q);
     200      free (q);
     201    }
     202  
     203    {
     204      p = source ();
     205      void *q = reallocarray (p, 7, 8);
     206      q = realloc (q, 9);
     207      sink (q);
     208      free (q);
     209    }
     210  
     211    {
     212      p = source ();
     213      void *q = reallocarray (p, 8, 9);
     214      q = reallocarray (q, 3, 4);
     215      sink (q);
     216      free (q);
     217    }
     218  
     219    {
     220      p = source ();
     221      void *q = reallocarray (p, 9, 10);
     222      q = reallocarray (q, 3, 4);
     223      sink (q);
     224      dealloc (q);                        // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
     225    }
     226  }
     227  
     228  
     229  void test_canonicalize_filename (void *p)
     230  {
     231    {
     232      void *q = canonicalize_file_name ("a");
     233      sink (q);
     234      __builtin_free (q);
     235    }
     236  
     237    {
     238      void *q = canonicalize_file_name ("b");
     239      sink (q);
     240      free (q);
     241    }
     242  
     243    {
     244      void *q = canonicalize_file_name ("c");
     245      q = __builtin_realloc (q, 2);
     246      sink (q);
     247      free (q);
     248    }
     249  
     250    {
     251      void *q = canonicalize_file_name ("d");
     252      q = realloc (q, 3);
     253      sink (q);
     254      free (q);
     255    }
     256  
     257    {
     258      void *q = canonicalize_file_name ("e");
     259      q = reallocarray (q, 4, 5);
     260      sink (q);
     261      free (q);
     262    }
     263  
     264    {
     265      void *q;
     266      q = canonicalize_file_name ("f");   // { dg-message "returned from 'canonicalize_file_name'" }
     267      sink (q);
     268      dealloc (q);                        // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
     269    }
     270  }