public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cdfrey at foursquare dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions
Date: Sat, 29 Apr 2023 19:30:59 +0000	[thread overview]
Message-ID: <bug-103993-4-3nsdSoSKZH@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103993-4@http.gcc.gnu.org/bugzilla/>

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

cdfrey at foursquare dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cdfrey at foursquare dot net

--- Comment #5 from cdfrey at foursquare dot net ---
This happens for me.  Here is a test case, for gcc 13.1.0, which gives the
warning with this command:

/home/gcc/rootdir/gcc-13.1.0/bin/g++ -Wall -O2 --std=c++17 -Wall -Wextra
-Werror -fmax-errors=4   -c -o gcc-test-case.o gcc-test-case.cc
gcc-test-case.cc: In function ‘int main()’:
gcc-test-case.cc:49:16: error: ‘static void Test::operator delete(void*)’
called on pointer returned from a mismatched allocation function
[-Werror=mismatched-new-delete]
   49 |         delete t;
      |                ^
In static member function ‘static void* Test::operator new(size_t)’,
    inlined from ‘int main()’ at gcc-test-case.cc:48:10:
gcc-test-case.cc:17:30: note: returned from ‘void* malloc(size_t)’
   17 |                 return malloc(size);
      |                        ~~~~~~^~~~~~
gcc-test-case.cc: In function ‘int main()’:
gcc-test-case.cc:48:17: error: ‘static void Test::operator delete(void*)’
called on pointer returned from a mismatched allocation function
[-Werror=mismatched-new-delete]
   48 |         t = new Test;
      |                 ^~~~
In static member function ‘static void* Test::operator new(size_t)’,
    inlined from ‘int main()’ at gcc-test-case.cc:48:10:
gcc-test-case.cc:17:30: note: returned from ‘void* malloc(size_t)’
   17 |                 return malloc(size);
      |                        ~~~~~~^~~~~~
cc1plus: all warnings being treated as errors
make: *** [<builtin>: gcc-test-case.o] Error 1


The code:

#include <iostream>     // cout statements needed to trigger warning
#include <new>
#include <stdlib.h>
using namespace std;

struct Test
{
        double a, b, c;

        Test() {
                cout << "Test::Test()\n";
        }

        void* operator new(size_t size)
        {
                cout << "Test::operator new(" << size << ")\n";
                return malloc(size);
        }

        void* operator new(size_t size, align_val_t al)
        {
                cout << "Test::operator new(" << size << ", align: " <<
(size_t)al << ")\n";
                return aligned_alloc((size_t)al, size);
        }

        void* operator new(size_t size, const std::nothrow_t &) throw()
        {
                cout << "Test::operator new(" << size << ", nothrow)\n";
                return malloc(size);
        }

        void operator delete(void *p)
        {
                cout << "Test::operator delete()\n";
                free(p);
        }

        void operator delete(void *p, align_val_t al)
        {
                cout << "Test::operator delete(p, align: " << (size_t)al <<
")\n";
                free(p);
        }
};

int main(void)
{
        Test *t;
        t = new Test;
        delete t;
}

      parent reply	other threads:[~2023-04-29 19:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-12 16:39 [Bug c++/103993] New: Incorrect error generated by mismatched-new-delete jjlindal at gmail dot com
2022-01-12 16:41 ` [Bug c++/103993] " jjlindal at gmail dot com
2022-01-12 18:31 ` [Bug middle-end/103993] -Wismatched-new-delete due to difference in inlining decisions msebor at gcc dot gnu.org
2022-04-25 21:13 ` andre at kostur dot net
2022-06-29 18:08 ` ed at catmur dot uk
2023-04-29 19:30 ` cdfrey at foursquare dot net [this message]

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-103993-4-3nsdSoSKZH@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).