[llvm-dev] [cfe-dev] Clang doesn't warn about wrong sized mallocs

Joerg Sonnenberger via llvm-dev llvm-dev at lists.llvm.org
Mon Aug 16 14:46:12 PDT 2021


On Mon, Aug 16, 2021 at 05:11:28PM +0000, Chris Tetreault wrote:
> A valid use for `malloc(sizeof(Foo) + c)` could be if one were writing
> a custom allocation function. They might be cramming metadata in that
> trailing area after the space for `Foo`.

Sure, but that is better written by introducing a local Foo_wrap type
that starts with a Foo member. That way it is both clearer to the reader
what is intended and ensures correct alignment etc.

Joerg


More information about the llvm-dev mailing list