Florian Weimer <fwei...@redhat.com> writes:

> * Paul Eggert:
>
>> On 2025-09-05 20:14, Collin Funk wrote:
>>> This patch works fine, with some formatting adjustments.
>>> But maybe it is just better to use fprintf, which can handle NULL
>>> pointers.
>>
>> fprintf can handle null pointers?
>
> The glibc implementation does.  It's not portable.

Hi Florian, we moved this discussion to bug-gnulib since I thought
Gnulib let us do it portably. However, Bruno mentioned he did not add
this check because GCC will optimize it to fputs and still crash. He wrote:

> Using the modified test program:
>
> #include <stdio.h>
> int
> main (void)
> {
>   fprintf (stdout, "%s", NULL);
>   fprintf (stdout, "\n");
>   return 0;
> }
> 
> On GNU/Linux:
> 
> $ gcc foo.c
> $ ./a.out 
> Segmentation fault (core dumped)
> $ nm a.out | grep ' U '
>                  U fputc@GLIBC_2.2.5
>                  U fputs@GLIBC_2.2.5
>                  U __libc_start_main@GLIBC_2.34
> 
> So, due to GCC optimizations, using NULL as argument for %s is unreliable
> even on systems with glibc.

Collin

Reply via email to