Bug 240398 - -D_XOPEN_SOURCE=500 incorrectly warns about missing snprintf()
Summary: -D_XOPEN_SOURCE=500 incorrectly warns about missing snprintf()
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-standards mailing list
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2019-09-07 22:35 UTC by Jan Beich
Modified: 2019-09-09 20:16 UTC (History)
2 users (show)

See Also:


Attachments
Make snprintf/vscanf visible under appropriate XSI visibility. (1001 bytes, patch)
2019-09-08 04:17 UTC, Konstantin Belousov
no flags Details | Diff
Make snprintf/vscanf visible under appropriate POSIX visibility. (982 bytes, patch)
2019-09-08 12:59 UTC, Konstantin Belousov
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Beich freebsd_committer 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 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 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 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 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 2019-09-08 22:12:08 UTC
Attachment 207287 [details] looks good to me.