public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/101791] New: missing warning on a mismatch between scalar and array forms of new and delete
Date: Thu, 05 Aug 2021 15:56:57 +0000	[thread overview]
Message-ID: <bug-101791-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 101791
           Summary: missing warning on a mismatch between scalar and array
                    forms of new and delete
           Product: gcc
           Version: 12.0
            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: ---

The -Wmismatched-new-delete warning introduced in GCC 11 is meant to diagnose
calls to operator delete with pointers obtained from a mismatched form or
overload of operator new.  A poster child for such a mismatch is the scalar
form of a delete expression with an argument obtained from array new.  GCC 11
diagnoses such mismatches when they involve member operators new and delete (as
in g() below) but it fails to do the same for the default non-member operators
(as in h()).

$ cat t.C && gcc -S -Wall t.Ctypedef __SIZE_TYPE__ size_t;

struct A
{
  void* operator new (size_t);
  void operator delete (void*);

  void* operator new[] (size_t);
  void operator delete[] (void*);
};

void f (void*);

void g ()
{
  A *p = new A[7];
  f (p);
  delete p;        // -Wmismatched-new-delete (good)
}

void h ()
{
  int *p = new int[7];
  f (p);
  delete p;        // missing warning
}
t.C: In function ‘void g()’:
t.C:18:10: warning: ‘static void A::operator delete(void*)’ called on pointer
returned from a mismatched allocation function [-Wmismatched-new-delete]
   18 |   delete p;        // -Wmismatched-new-delete (good)
      |          ^
t.C:16:17: note: returned from ‘static void* A::operator new [](size_t)’
   16 |   A *p = new A[7];
      |                 ^

             reply	other threads:[~2021-08-05 15:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-05 15:56 msebor at gcc dot gnu.org [this message]
2021-08-05 15:57 ` [Bug middle-end/101791] " msebor at gcc dot gnu.org
2021-08-14 19:27 ` cvs-commit at gcc dot gnu.org
2021-08-14 19:28 ` msebor at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-101791-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).