public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/98166] New: bogus -Wmismatched-dealloc on user-defined allocator and inlining
@ 2020-12-07  1:03 msebor at gcc dot gnu.org
  2020-12-07  1:04 ` [Bug middle-end/98166] " msebor at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-12-07  1:03 UTC (permalink / raw)
  To: gcc-bugs

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; }
      |                                       ^~~~~~~~~~~~~~~~~~~~

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-12-15 15:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-07  1:03 [Bug middle-end/98166] New: bogus -Wmismatched-dealloc on user-defined allocator and inlining msebor at gcc dot gnu.org
2020-12-07  1:04 ` [Bug middle-end/98166] " msebor at gcc dot gnu.org
2020-12-08 20:48 ` msebor at gcc dot gnu.org
2020-12-14 20:31 ` cvs-commit at gcc dot gnu.org
2021-04-27 11:39 ` jakub at gcc dot gnu.org
2021-07-28  7:05 ` rguenth at gcc dot gnu.org
2021-12-15 15:50 ` msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).