[CDRIVER-4233] error: implicit declaration of function 'arc4random_buf' is invalid in C99 Created: 25/Nov/21  Updated: 28/Oct/23  Resolved: 13/Dec/21

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.20.1

Type: Bug Priority: Major - P3
Reporter: Ryan Schmidt Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
is caused by CDRIVER-4227 Use a shared_mutex when locking in sh... Closed
Related
related to CDRIVER-4234 Build failure on Mac OS X 10.6 due to... Closed
is related to PHPC-2019 Remove Solaris checks in m4 build scr... Closed

 Description   

I can't build libbson from mongo-c-driver 1.20.0 on macOS Catalina 10.15.7 with Apple clang version 12.0.0 (clang-1200.0.32.29) from Xcode 12.4. The error is:

src/libbson/src/bson/bson-context.c:234:4: error: implicit declaration of function 'arc4random_buf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
 arc4random_buf (&result, sizeof (result));
 ^
 1 error generated.

src/libbson/src/bson/bson-string.c:554:4: error: implicitly declaring library function 'strlcpy' with type 'unsigned long (char *, const char *, unsigned long)' [-Werror,-Wimplicit-function-declaration]
 strlcpy (dst, src, size);
 ^
src/libbson/src/bson/bson-string.c:554:4: note: include the header <string.h> or explicitly provide a declaration for 'strlcpy'
1 error generated.

arc4random_buf is only defined in stdlib.h if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)).

Defining _DARWIN_C_SOURCE works around the problem but I don't know if that's the best solution.



 Comments   
Comment by Githook User [ 13/Dec/21 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-4233 fix implicit decl of arc4random and strlcpy on macOS (#910)
Branch: r1.20
https://github.com/mongodb/mongo-c-driver/commit/a07dfec5142a90f256eaa746339cffe7a0abde4f

Comment by Githook User [ 13/Dec/21 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-4233 fix implicit decl of arc4random and strlcpy on macOS (#910)
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/1523c42b70e4cbb36c88c912c80ba9e38ebab8f9

Comment by Kevin Albertson [ 11/Dec/21 ]

Thank you for the report ryandesign. I am able to reproduce this on my macOS 11.6.1 with Apple clang version 13.0.0 (clang-1300.0.29.3).

I believe this is a regression introduced in 1.20.0 as part of CDRIVER-4227.

CDRIVER-4227 added -D_XOPEN_SOURCE=700 libbson in src/libbson/CMakeLists.txt here.

cdefs.h on macOS defines _POSIX_C_SOURCE 200809L if -D_XOPEN_SOURCE=700 is present.

_POSIX_C_SOURCE 200809L causes arc4random to be removed from stdlib.h here.

POSIX_C_SOURCE 200809L causes __DARWIN_C_LEVEL to be defined as POSIX_C_SOURCE in cdefs.h here. libbson's CHECK_SYMBOL_EXISTS for strlcpy do not apply -D_XOPEN_SOURCE=700. cmake defines BSON_HAVE_STRLCPY thinking that strlcpy is defined, but compilation fails.

Generated at Wed Feb 07 21:20:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.