bitwiseworks / libc

LIBC Next (kLIBC fork)
9 stars 4 forks source link

strndup() is not declared when defining _GNU_SOURCE and including features.h #128

Closed komh closed 1 year ago

komh commented 1 year ago

Hi/2.

On kLIBC, strndup() is declared if __USE_GNU, which is defined by _GNU_SOURCE, is defined. However, on LIBCn, it is declared when __POSIX_VISIBLE >= 200809.

BTW, if defining _GNU_SOURCE and including features.h, __POSIX_VISIBLE is defined to 200112 because _XOPEN_SOURCE is defined to 600 not 700.

In addition, features.h says _GNU_SOURCE turns on all the other features. Therefore, defining_XOPEN_SOURCE to 700 is correct if _GNU_SOURCE is defined.

Here is the test program:

strndup.c

#define _GNU_SOURCE
#include <features.h>
#include <string.h>

int main( void )
{
    const char *s1 = "String";
    char *s2 = strndup( s1, 2 );

    return s2 - s1;
}

And here is the patch:

diff -uNr  features.h.org features.h
--- features.h.org      2021-08-26 23:26:16.000000000 +0000
+++ features.h  2023-02-07 21:00:08.000000000 +0000
@@ -171,7 +171,7 @@
 # define __USE_POSIX199506     1       /* bird */
 #endif                                  /* bird */
 # undef  _XOPEN_SOURCE
-# define _XOPEN_SOURCE 600
+# define _XOPEN_SOURCE 700
 # undef  _XOPEN_SOURCE_EXTENDED
 # define _XOPEN_SOURCE_EXTENDED        1
 # undef         _LARGEFILE64_SOURCE
dmik commented 1 year ago

The LIBCn change c9814d3bdcc8796b2c139ad33ae19427874b5625 responsible for this difference comes from #57.

However, seems that bumping _XOPEN_SOURCE to 700 is the right thing indeed, this is what GLIBC also does https://sourceware.org/git/?p=glibc.git;a=blob;f=include/features.h;h=9eae86a2d845d68cae0a543a4fa87034a8f019fd;hb=HEAD.

I actually did some more modifications (in particular, made _GNU_SOURCE also define _POSIX_SOURCE and _POSIX_C_SOURCE as they should).

There is a great chance that it will break building something else but at least LIBC itself builds fine with these changes (and that's already something). We will fix new problems as we find them — we are way behind the mainstream in terms of LIBC/POSIX compatibility so it's all expected.