https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119104
Bug ID: 119104
Summary: Unclear documentation for [[gnu::nonnull_if_nonzero]]
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: alx at kernel dot org
Target Milestone: ---
The documentation about [[gnu::nonnull]] says:
nonnull_if_nonzero
nonnull_if_nonzero (arg-index, arg2-index)
The nonnull_if_nonzero attribute is a conditional version of the nonnull
attribute. It has two arguments, the first argument shall be argument index of
a pointer argument which must be in some cases non-null and the second argument
shall be argument index of an integral argument (other than boolean). If the
integral argument is zero, the pointer argument can be null, if it is non-zero,
the pointer argument must not be null.
extern void *
my_memcpy (void *dest, const void *src, size_t len)
__attribute__((nonnull (1, 2)));
extern void *
my_memcpy2 (void *dest, const void *src, size_t len)
__attribute__((nonnull_if_nonzero (1, 3),
nonnull_if_nonzero (2, 3)));
With these declarations, it is invalid to call my_memcpy (NULL, NULL, 0);
or to call my_memcpy2 (NULL, NULL, 4); but it is valid to call my_memcpy2
(NULL, NULL, 0);. This attribute should be used on declarations which have e.g.
an exception for zero sizes, in which case null may be passed.
It says what happens when the value is 0. It says what happens when the value
is nonzero. But these are rarely passed as constant expressions, so the
compiler will most of the time not be able to determine if it is zero or
nonzero.
What's the behavior when a variable that the compiler cannot know if it's zero
or not is passed? Does it trigger the diagnostics documented for
[[gnu::nonnull]] or not?