https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80936
Bug ID: 80936 Summary: bcmp, bcopy, and bzero not declared nonnull Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The -Wnonnull option is expected to trigger a warning when a null pointer is being passed to a function known to require a non-null argument. GCC issues this warning for functions like memcmp, memcpy, and memset but fails to issue it for calls to the similar bcmp, bcopy, and bzero functions. It looks like the latter three built-ins are not declared with the nonnull attribute in builtins.def. $ cat z.c && gcc -O2 -S -Wall -Wextra z.c void zero0 (void *p, unsigned n) { if (p == 0) __builtin_memset (p, 0, n); // warning, good } void zero1 (void *p, unsigned n) { if (p == 0) __builtin_bzero (p, n); // missing warning } void copy0 (void *p, const void *q, unsigned n) { if (p == 0) __builtin_memcpy (p, q, n); // warning, good } void copy1 (void *p, const void *q, unsigned n) { if (q == 0) __builtin_memcpy (p, q, n); // warning, good } void copy2 (void *p, const void *q, unsigned n) { if (p == 0) __builtin_bcopy (q, p, n); // missing warning } void copy3 (void *p, const void *q, unsigned n) { if (q == 0) __builtin_bcopy (q, p, n); // missing warning } int cmp0 (const void *p, const void *q, unsigned n) { if (p == 0) return __builtin_memcmp (p, q, n); // warning, good return 0; } int cmp1 (const void *p, const void *q, unsigned n) { if (q == 0) return __builtin_memcmp (p, q, n); // warning, good return 0; } int cmp2 (const void *p, const void *q, unsigned n) { if (p == 0) return __builtin_bcmp (p, q, n); // missing warning return 0; } int cmp3 (const void *p, const void *q, unsigned n) { if (q == 0) return __builtin_bcmp (p, q, n); // missing warning return 0; } z.c: In function ‘zero0’: z.c:4:5: warning: argument 1 null where non-null expected [-Wnonnull] __builtin_memset (p, 0, n); // warning, good ^~~~~~~~~~~~~~~~~~~~~~~~~~ z.c:4:5: note: in a call to built-in function ‘__builtin_memset’ z.c: In function ‘copy0’: z.c:16:5: warning: argument 1 null where non-null expected [-Wnonnull] __builtin_memcpy (p, q, n); // warning, good ^~~~~~~~~~~~~~~~~~~~~~~~~~ z.c:16:5: note: in a call to built-in function ‘__builtin_memcpy’ z.c: In function ‘copy1’: z.c:22:5: warning: argument 2 null where non-null expected [-Wnonnull] __builtin_memcpy (p, q, n); // warning, good ^~~~~~~~~~~~~~~~~~~~~~~~~~ z.c:22:5: note: in a call to built-in function ‘__builtin_memcpy’ z.c: In function ‘cmp0’: z.c:40:12: warning: argument 1 null where non-null expected [-Wnonnull] return __builtin_memcmp (p, q, n); // warning, good ^~~~~~~~~~~~~~~~~~~~~~~~~~ z.c:40:12: note: in a call to built-in function ‘__builtin_memcmp’ z.c: In function ‘cmp1’: z.c:47:12: warning: argument 2 null where non-null expected [-Wnonnull] return __builtin_memcmp (p, q, n); // warning, good ^~~~~~~~~~~~~~~~~~~~~~~~~~ z.c:47:12: note: in a call to built-in function ‘__builtin_memcmp’