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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |middle-end

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Daniel Gutson from comment #4)
> (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?

It does not.  It just optimizes the code.

> 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.

yes that just undefined behavior when invoking memset with a NULL value, so
replacing it is still fine.

Also calloc is sometimes faster than a malloc/memset due to knowing if the
kernel will zero out the memory, etc.

Reply via email to