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

daniel.gutson at tallertechnologies dot com gcc-bugzilla@gcc.gnu.org
Thu Sep 17 19:57:00 GMT 2015


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.



More information about the Gcc-bugs mailing list