Bug 240398

Summary: -D_XOPEN_SOURCE=500 incorrectly warns about missing snprintf()
Product: Base System Reporter: Jan Beich <jbeich>
Component: standardsAssignee: freebsd-standards (Nobody) <standards>
Status: New ---    
Severity: Affects Only Me CC: jilles, kib
Priority: --- Keywords: patch
Version: CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
Make snprintf/vscanf visible under appropriate XSI visibility.
none
Make snprintf/vscanf visible under appropriate POSIX visibility. none

Description Jan Beich freebsd_committer freebsd_triage 2019-09-07 22:35:27 UTC
$ cat a.c
#define _XOPEN_SOURCE 500
#include <stdio.h>

int main()
{
  char dest[255], src[] = "Hello";
  snprintf(dest, sizeof(dest), "%s", src);
}

$ cc -Werror a.c
a.c:7:3: error: implicitly declaring library function 'snprintf' with type 'int (char *, unsigned long, const char
      *, ...)' [-Werror,-Wimplicit-function-declaration]
  snprintf(dest, sizeof(dest), "%s", src);
  ^
a.c:7:3: note: include the header <stdio.h> or explicitly provide a declaration for 'snprintf'
1 error generated.

According to https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html

  Issue 5

      Aligned with ISO/IEC 9899:1990/Amendment 1:1995 (E). Specifically,
      the l (ell) qualifier can now be used with c and s conversion
      specifiers.

      The snprintf() function is new in Issue 5.

Example fix - https://github.com/dragonflybsd/dragonflybsd/commit/626be21490
Comment 1 Jan Beich freebsd_committer freebsd_triage 2019-09-07 22:41:14 UTC
Real world example from a library bundled with Firefox:

$ git clone https://github.com/kinetiknz/cubeb
$ cd cubeb
$ CFLAGS="-isystem/usr/local/include" LDFLAGS="-L/usr/local/lib" cmake .
$ make
[...]
src/cubeb_alsa.c:564:9: warning: implicitly declaring library function 'snprintf' with type 'int (char *, unsigned long, const char *, ...)' [-Wimplicit-function-declaration]
    r = snprintf(node_name, sizeof(node_name), "pcm.%s", string);
        ^
$ fgrep XOPEN src/cubeb_alsa.c
#define _XOPEN_SOURCE 500

I cannot fix the warning upstream because neither Linux nor other BSDs are affected.
Comment 2 Konstantin Belousov freebsd_committer freebsd_triage 2019-09-08 04:17:39 UTC
Created attachment 207271 [details]
Make snprintf/vscanf visible under appropriate XSI visibility.
Comment 3 Jilles Tjoelker freebsd_committer freebsd_triage 2019-09-08 12:23:17 UTC
I agree with the snprintf/vsnprintf part of https://bugs.freebsd.org/bugzilla/attachment.cgi?id=207271&action=diff except that the test should be for __POSIX_VISIBLE >= 199506 instead of __XSI_VISIBLE >= 500 since this is not an XSI extension.

The change to vfscanf/vscanf/vsscanf is redundant since __XSI_VISIBLE == 600 already implies C99.
Comment 4 Konstantin Belousov freebsd_committer freebsd_triage 2019-09-08 12:59:17 UTC
Created attachment 207287 [details]
Make snprintf/vscanf visible under appropriate POSIX visibility.
Comment 5 Jilles Tjoelker freebsd_committer freebsd_triage 2019-09-08 22:12:08 UTC
Attachment 207287 [details] looks good to me.