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 tree-optimization/78902] Missed malloc optimizations: malloc w/o LHS and zero argument


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

--- Comment #7 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #6)
> Warning on malloc with an unused return value sounds like a good idea to me
> (in fact, it seems that all allocation functions to be declared with
> warn_unused_result; i.e., all those declared with attribute alloc_size).

Good, I can do that in next stage1.

> 
> I also think warning on malloc(0) can be useful.  GCC 7 has -Walloc-zero
> that warns on all zero-size allocations.  Unfortunately, it's not in -Wall
> or -Wextra and has to be explicitly enabled.

That's a pity, why was the option not enabled in either -Wall or -Wextra?

> 
> Unconditionally turning malloc(0) into NULL wouldn't be safe since the call
> is allowed to return a unique non-null pointer and there are implementations
> (e.g., Glibc) that do return one.  But doing that under an option might be
> useful on targets where the system malloc returns null (though it could
> break with superimposition).

Or when an application does not rely on result being non-null.

> 
> I'm not sure that eliminating calls to malloc whose return value is unused
> is a safe optimization.  Malloc can be superimposed and the replacement
> version might have important side-effects that the optimization would
> prevent.

Both clang and gcc (in 037t.cddce1) can optimize out:
int main()
{
  __builtin_malloc(123);
  return 0;
}

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