public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory
@ 2022-12-11 16:31 jo.rickert at protonmail dot ch
  2022-12-11 16:34 ` [Bug c++/108063] " jo.rickert at protonmail dot ch
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jo.rickert at protonmail dot ch @ 2022-12-11 16:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108063
           Summary: push_back on std::vector allocates wrong amount of
                    memory
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jo.rickert at protonmail dot ch
  Target Milestone: ---

Created attachment 54067
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54067&action=edit
Source Code with a main.cpp and additional .cpp / .h files with all the .o and
.ii Files

When you have two classes with the same name (in different .cpp Files), the two
classes have different size, for instance one with one integer member and the
other with two integer members. Then if you do a push_back of each class on a
vector the second instance does not allocate the right amount of memory and
undefined actions occur.

The system the files are from is a manjaro Linux with a gcc Version 12.2.0

I tried it on a windows 10 with gcc 12.2.0, Fedora 35 with gcc 11.3.1-3 and a
centos7 with gcc 4.8.5 the bug occured on all versions.

The second instance of this pushing on the vector does not even need to be
called only to be compiled.

Setup in the attached code:

File dummy.cpp (code in this file never gets called, but if this file is linked
then the generated source code creates undefined behavior):
contains a class Item with two integer members. It has an unused functions that
creates a std::vector<Item> uses push_back with an Item.

File undefined_behavior.cpp:
Has also a class Item but only with one Integer Member.
Here we also have a function that creates a std::vector<Item> but when we use
push_back here the allocated memory does strange things. If emplace_back is
used instead it throws an allocation error.
This function also prints the content of the vector, to show that it makes no
sense.

File undefined_behavior.h:
contains the declaration of the function in undefined_behavior.cpp

File main.cpp
only includes undefined_behavior.h and calls the function.

The expected output of my program is:
Value of 0 is: 0
Value of 1 is: 1
Value of 2 is: 2
Value of 3 is: 3
Value of 4 is: 4
Value of 5 is: 5
Value of 6 is: 6
Value of 7 is: 7
Value of 8 is: 8
Value of 9 is: 9

Actual output varies from system to system:
Value of 0 is: 0
Value of 1 is: 0
Value of 2 is: 1
Value of 3 is: 0
Value of 4 is: 2
Value of 5 is: 0
Value of 6 is: 3
Value of 7 is: 0
Value of 8 is: 4
Value of 9 is: 0

or:
Value of 0 is: 0
Value of 1 is: 0
Value of 2 is: 1
Value of 3 is: 29499056
Value of 4 is: 2
Value of 5 is: 29499088
Value of 6 is: 3
Value of 7 is: 29499120
Value of 8 is: 4
Value of 9 is: 29499120

When push_back is replaced with emplace_back it most of the time throws
bac_alloc and crashes.

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
@ 2022-12-11 16:34 ` jo.rickert at protonmail dot ch
  2022-12-11 16:36 ` jo.rickert at protonmail dot ch
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jo.rickert at protonmail dot ch @ 2022-12-11 16:34 UTC (permalink / raw)
  To: gcc-bugs

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

jo.rickert at protonmail dot ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |11.3.1, 12.2.0, 4.8.5
             Target|                            |linux / windows
              Build|                            |linux / windows
               Host|                            |linux / windows

--- Comment #1 from jo.rickert at protonmail dot ch ---
tried to compile with clang, then program outputs as expected.

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
  2022-12-11 16:34 ` [Bug c++/108063] " jo.rickert at protonmail dot ch
@ 2022-12-11 16:36 ` jo.rickert at protonmail dot ch
  2022-12-11 17:10 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jo.rickert at protonmail dot ch @ 2022-12-11 16:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from jo.rickert at protonmail dot ch ---
The forgotten command line:

g++ -o main main.cpp dummy.cpp undefined_behavior.cpp

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
  2022-12-11 16:34 ` [Bug c++/108063] " jo.rickert at protonmail dot ch
  2022-12-11 16:36 ` jo.rickert at protonmail dot ch
@ 2022-12-11 17:10 ` pinskia at gcc dot gnu.org
  2022-12-11 18:18 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-11 17:10 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is called a violation of the one definition rule. In c++ classes even have
linkage.

No diagnostic is required as it is undefined behavior ar runtime.

Note using  -flto and you would catch these.

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
                   ` (2 preceding siblings ...)
  2022-12-11 17:10 ` pinskia at gcc dot gnu.org
@ 2022-12-11 18:18 ` pinskia at gcc dot gnu.org
  2022-12-11 21:06 ` eric41293 at comcast dot net
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-11 18:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You can read up more on the one definition rule here:
https://en.cppreference.com/w/cpp/language/definition

It is a good summary of what the standard says.

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
                   ` (3 preceding siblings ...)
  2022-12-11 18:18 ` pinskia at gcc dot gnu.org
@ 2022-12-11 21:06 ` eric41293 at comcast dot net
  2022-12-11 21:09 ` jo.rickert at protonmail dot ch
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: eric41293 at comcast dot net @ 2022-12-11 21:06 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Schmidt <eric41293 at comcast dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eric41293 at comcast dot net

--- Comment #5 from Eric Schmidt <eric41293 at comcast dot net> ---
Surely this should have been resolved as INVALID?

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
                   ` (4 preceding siblings ...)
  2022-12-11 21:06 ` eric41293 at comcast dot net
@ 2022-12-11 21:09 ` jo.rickert at protonmail dot ch
  2022-12-11 21:10 ` jo.rickert at protonmail dot ch
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jo.rickert at protonmail dot ch @ 2022-12-11 21:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from jo.rickert at protonmail dot ch ---
(In reply to Andrew Pinski from comment #4)
> You can read up more on the one definition rule here:
> https://en.cppreference.com/w/cpp/language/definition
> 
> It is a good summary of what the standard says.

Thanks, I thought the name had to be unique per translation unit.

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
                   ` (5 preceding siblings ...)
  2022-12-11 21:09 ` jo.rickert at protonmail dot ch
@ 2022-12-11 21:10 ` jo.rickert at protonmail dot ch
  2022-12-11 21:15 ` pinskia at gcc dot gnu.org
  2022-12-12 11:07 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jo.rickert at protonmail dot ch @ 2022-12-11 21:10 UTC (permalink / raw)
  To: gcc-bugs

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

jo.rickert at protonmail dot ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WONTFIX                     |INVALID

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
                   ` (6 preceding siblings ...)
  2022-12-11 21:10 ` jo.rickert at protonmail dot ch
@ 2022-12-11 21:15 ` pinskia at gcc dot gnu.org
  2022-12-12 11:07 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-11 21:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Eric Schmidt from comment #5)
> Surely this should have been resolved as INVALID?

It was marked as won't fix as I am saying we won't fix (change) the compiler to
reject it (even though the code is invalid and can be rejected as we do with
-flto).

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

* [Bug c++/108063] push_back on std::vector allocates wrong amount of memory
  2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
                   ` (7 preceding siblings ...)
  2022-12-11 21:15 ` pinskia at gcc dot gnu.org
@ 2022-12-12 11:07 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-12-12 11:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to jo.rickert from comment #6)
> Thanks, I thought the name had to be unique per translation unit.

That can't possibly work. You would have to define the entire program in one
translation unit, because otherwise you could never declare:

struct X { void func(); };

and define X::func() in one translation unit and call it from a different
translation unit.  Under your mental model, it would be a different X, and the
linker could never find the definition of X::func.

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

end of thread, other threads:[~2022-12-12 11:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-11 16:31 [Bug c++/108063] New: push_back on std::vector allocates wrong amount of memory jo.rickert at protonmail dot ch
2022-12-11 16:34 ` [Bug c++/108063] " jo.rickert at protonmail dot ch
2022-12-11 16:36 ` jo.rickert at protonmail dot ch
2022-12-11 17:10 ` pinskia at gcc dot gnu.org
2022-12-11 18:18 ` pinskia at gcc dot gnu.org
2022-12-11 21:06 ` eric41293 at comcast dot net
2022-12-11 21:09 ` jo.rickert at protonmail dot ch
2022-12-11 21:10 ` jo.rickert at protonmail dot ch
2022-12-11 21:15 ` pinskia at gcc dot gnu.org
2022-12-12 11:07 ` redi 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).