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

Leander Besting via cfe-dev cfe-dev at lists.llvm.org
Sat Aug 14 06:59:56 PDT 2021


The following code does not produce a warning when compiled with clang 12.0.1 and -Weverything despite there being an obvious error. It should be pretty trivial to detect that the argument to malloc does not match the size that s expects to point to, similar to how printf arguments are checked.

#include <stdlib.h>
struct S {
    int x, y, z;
};
int main() {
    // sizeof (struct S) == 12
    // sizeof (s) == 8
    struct S *s = malloc(sizeof(s));
}

$ clang --version
clang version 12.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

--
Leander Besting


More information about the cfe-dev mailing list