Moving this to [email protected] and CC'ing Bruno because this
discussion made me notice something.

Paul Eggert <[email protected]> writes:

> 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?

Well, it depends. It is a GNU extension.

Using the test program:

    $ cat main.c
    #include <stdio.h>
    int
    main (void)
    {
      fprintf (stdout, "%s\n", NULL);
      return 0;
    }

On GNU/Linux:

    $ gcc main.c 
    $ ./a.out 
    (null)

On Solaris 10:

    $ gcc main.c
    $ ./a.out 
    Segmentation Fault (core dumped)

My original message assumed that Gnulib would support it. In
lib/printf-args.c we have the following:

      case TYPE_STRING:
        ap->a.a_string = va_arg (args, const char *);
        /* A null pointer is an invalid argument for "%s", but in practice
           it occurs quite frequently in printf statements that produce
           debug output.  Use a fallback in this case.  */
        if (ap->a.a_string == NULL)
          ap->a.a_string = "(NULL)";
        break;

This is also the case for wide character strings.

However, it appears the m4/*printf.m4 files do not check for the
behavior so we cannot depend on it.

Bruno, do you recall if this was intentional? Or do you think it should
be added?

Collin

Reply via email to