https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98166

            Bug ID: 98166
           Summary: bogus -Wmismatched-dealloc on user-defined allocator
                    and inlining
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Designating using attribute malloc a pair of functions as an allocator and
deallocator and defining one of them inline to call another allocator or
deallocator not associated with the former pair leads to false positive
warnings.

The test case below (inspired by the one in pr98160) shows how this could
happen.  The two pairs of allocators are independent with one another

$ cat a.c && gcc -O2 -S -Wall a.c
void dealloc_shrt (short *p) { __builtin_free (p - 1); }
void dealloc_int (int*);

__attribute__ ((malloc (dealloc_shrt)))
short* alloc_shrt (int);

__attribute__ ((malloc (dealloc_int)))
int* alloc_int (int n) { return (int*)__builtin_malloc (n) + 1; }

void f (int n)
{
  {
    short *p = alloc_shrt (n);
    dealloc_shrt (p);
  }

  {
    int *p = alloc_int (n);
    dealloc_int (p);
  }
}
In function ‘dealloc_shrt’,
    inlined from ‘f’ at a.c:14:5:
a.c:1:32: warning: ‘__builtin_free’ called on pointer returned from a
mismatched allocation function [-Wmismatched-dealloc]
    1 | void dealloc_shrt (short *p) { __builtin_free (p - 1); }
      |                                ^~~~~~~~~~~~~~~~~~~~~~
a.c: In function ‘f’:
a.c:13:16: note: returned from a call to ‘alloc_shrt’
   13 |     short *p = alloc_shrt (n);
      |                ^~~~~~~~~~~~~~
a.c:19:5: warning: ‘dealloc_int’ called on pointer returned from a mismatched
allocation function [-Wmismatched-dealloc]
   19 |     dealloc_int (p);
      |     ^~~~~~~~~~~~~~~
a.c:8:39: note: returned from a call to ‘__builtin_malloc’
    8 | int* alloc_int (int n) { return (int*)__builtin_malloc (n) + 1; }
      |                                       ^~~~~~~~~~~~~~~~~~~~

Reply via email to