https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100404
Bug ID: 100404
Summary: Unable to disable removal of null pointer checks for
nonnull function arguments
Product: gcc
Version: 10.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: gccgc at bithub dot de
Target Milestone: ---
Based on the documentation of the "nonnull" function attribute
"-fno-delete-null-pointer-checks" may be used to disable compiler optimizations
based on the knowledge that a function argument cannot be null ([1]).
However, on GCC 10.2 with optimization level 1 (and also on earlier versions
with optimization level 2) the NULL check is eliminated from the following
sample program:
int __attribute__ ((nonnull)) isnull(char *ptr) {
if (ptr == 0)
return 0;
return 1;
}
int main(void) {
return isnull(0);
}
Compiling this sample without optimizations and "-fdelete-null-pointer-checks"
does not eliminate the check so the optimization seems to be triggered by
another optimization.
[1]
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-nonnull-function-attribute