c-family: Allow arguments with NULLPTR_TYPE as sentinels [PR114780]
commit2afdecccbaf5c5b1c7a235509b37092540906c02
authorJakub Jelinek <jakub@redhat.com>
Fri, 19 Apr 2024 22:12:36 +0000 (20 00:12 +0200)
committerJakub Jelinek <jakub@redhat.com>
Fri, 19 Apr 2024 22:12:36 +0000 (20 00:12 +0200)
treec1d0dff23d1adc5d89b985ffc70bd5919e6dadb7
parenta39983bf58d3097c472252f6989d19b60909dd9a
c-family: Allow arguments with NULLPTR_TYPE as sentinels [PR114780]

While in C++ the ellipsis argument conversions include
"An argument that has type cv std::nullptr_t is converted to type void*"
in C23 a nullptr_t argument is not promoted in any way, but va_arg
description says:
"the type of the next argument is nullptr_t and type is a pointer type that has the same
representation and alignment requirements as a pointer to a character type."
So, while in C++ check_function_sentinel will never see NULLPTR_TYPE, for
C23 it can see that and currently we incorrectly warn about those.

The only question is whether we should warn on any argument with
nullptr_t type or just about nullptr (nullptr_t argument with integer_zerop
value).  Through undefined behavior guess one could pass non-NULL pointer
that way, say by union { void *p; nullptr_t q; } u; u.p = &whatever;
and pass u.q to ..., but valid code should always pass something that will
read as (char *) 0 when read using va_arg (ap, char *), so I think it is
better not to warn rather than warn in those cases.

Note, clang seems to pass (void *)0 rather than expression of nullptr_t
type to ellipsis in C23 mode as if it did the C++ ellipsis argument
conversions, in that case guess not warning about that would be even safer,
but what GCC does I think follows the spec more closely, even when in a
valid program one shouldn't be able to observe the difference.

2024-04-20  Jakub Jelinek  <jakub@redhat.com>

PR c/114780
* c-common.cc (check_function_sentinel): Allow as sentinel any
argument of NULLPTR_TYPE.

* gcc.dg/format/sentinel-2.c: New test.
gcc/c-family/c-common.cc
gcc/testsuite/gcc.dg/format/sentinel-2.c [new file with mode: 0644]