Bug 32024 - sys/un.h includes header files within extern "C++" { ... }
Summary: sys/un.h includes header files within extern "C++" { ... }
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.40
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-07-26 11:22 UTC by Bruno Haible
Modified: 2024-09-13 21:00 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
proposed fix (707 bytes, patch)
2024-07-26 11:25 UTC, Bruno Haible
Details | Diff
test case 1st file: test.cc (76 bytes, text/x-c++src)
2024-09-13 21:00 UTC, Bruno Haible
Details
test case 2nd file: string.h (231 bytes, text/x-chdr)
2024-09-13 21:00 UTC, Bruno Haible
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Bruno Haible 2024-07-26 11:22:01 UTC
The <sys/un.h> header file includes other header files (<stddef.h> and <string.h>) from within an extern "C++" { ... } block (written as __BEGIN_DECLS and __END_DECLS).

While this has no ill effects with the glibc definitions of <stddef.h> and <string.h>, it causes compilation errors when used with GNU Gnulib. Gnulib augments <stddef.h> and <string.h> with additional declarations, by use of #include_next. When these overridden header files are included from within an extern "C++" { ... } block, with clang++, this leads to errors ("error: declaration of 'memchr' has a different language linkage" etc.). See <https://lists.gnu.org/archive/html/bug-gnulib/2024-07/msg00261.html>.

Find attached a fix, that moves the includes outside the extern "C++" { ... } block.
Comment 1 Bruno Haible 2024-07-26 11:25:42 UTC
Created attachment 15642 [details]
proposed fix
Comment 2 Eric Blake 2024-07-26 19:06:38 UTC
Unrelated to the patch, but while we're in the area:

# define SUN_LEN(ptr) (offsetof (struct sockaddr_un, sun_path)		      \
   strlen ((ptr)->sun_path))

But at least Linux allows the use of ALL bytes in the length-limited sun_path[] to be non-NULL (while not portable to all implementations, it does let GNU/Linux systems use one extra byte for the name of a Unix socket).  Therefore, shouldn't this macro be using strnlen() instead of strlen()?
Comment 3 Andreas Schwab 2024-09-11 14:15:11 UTC
__BEGIN_DECLS is defined to extern "C", not extern "C++", so I don't see how that can result in misbehaviour.
Comment 4 Bruno Haible 2024-09-13 20:59:12 UTC
(In reply to Andreas Schwab from comment #3)
> __BEGIN_DECLS is defined to extern "C", not extern "C++"

Indeed, I should have written extern "C" instead of extern "C++" in the bug description.

> so I don't see how that can result in misbehaviour.

Here's a minified test case:
1. Store the attached test.cc and string.h in the same directory.
2. Compile test.cc with clang (I use clang 18):

$ clang++ -I. -c test.cc
In file included from test.cc:5:
In file included from /usr/include/x86_64-linux-gnu/sys/un.h:37:
./string.h:6:15: error: declaration of 'memchr' has a different language linkage
    6 | extern void * memchr (void *__s, int __c, size_t __n) noexcept (true)
      |               ^
/usr/include/string.h:87:14: note: previous declaration is here
   87 | extern void *memchr (void *__s, int __c, size_t __n)
      |              ^
In file included from test.cc:5:
In file included from /usr/include/x86_64-linux-gnu/sys/un.h:37:
./string.h:8:21: error: declaration of 'memchr' has a different language linkage
    8 | extern void const * memchr (void const *__s, int __c, size_t __n) noexcept (true)
      |                     ^
/usr/include/string.h:89:20: note: previous declaration is here
   89 | extern const void *memchr (const void *__s, int __c, size_t __n)
      |                    ^
2 errors generated.

3. Compile test.cc with clang, this time defining WORKAROUND to 1:

$ clang++ -I. -c test.cc -DWORKAROUND
(no errors)
Comment 5 Bruno Haible 2024-09-13 21:00:20 UTC
Created attachment 15702 [details]
test case 1st file: test.cc
Comment 6 Bruno Haible 2024-09-13 21:00:57 UTC
Created attachment 15703 [details]
test case 2nd file: string.h