This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/67618] malloc+memset optimization breaks code


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

--- Comment #4 from Daniel Gutson <daniel.gutson at tallertechnologies dot com> ---
(In reply to Andreas Schwab from comment #3)
> Trying to read the (uninitialized) contents of the allocated memory for x <=
> 12 would be undefined behaviour, so calling calloc instead does not change
> the defined behaviour.

Why do you assert that the program will read the memory?
The optimization ignores the 'if' statement (just comment that line and see),
so if malloc() returns NULL, memset is called unconditionally and will try to
write to address NULL. The "x > 12" was just an example that this is arbitrary.
Replace it with something else. The 'if' shall not be ignored.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]