Skip to content

fix: guard fdopen() NULL macro with !defined(__APPLE__) in zutil.h#3

Open
pgarciagon wants to merge 8 commits intocpp-pm:hunter-1.3.1from
pgarciagon:fix/macos-fdopen-conflict
Open

fix: guard fdopen() NULL macro with !defined(__APPLE__) in zutil.h#3
pgarciagon wants to merge 8 commits intocpp-pm:hunter-1.3.1from
pgarciagon:fix/macos-fdopen-conflict

Conversation

@pgarciagon
Copy link
Copy Markdown

Problem

zutil.h defines:

#if (defined(MACOS) || defined(TARGET_OS_MAC)) && !defined(__APPLE__)
#  define fdopen(fd,mode) NULL
#endif

Wait — the original code is:

#if (defined(MACOS) || defined(TARGET_OS_MAC))
#  define fdopen(fd,mode) NULL
#endif

On modern Apple platforms TARGET_OS_MAC is always defined (it is set by
<TargetConditionals.h>). macOS also provides a real fdopen() declaration
in <stdio.h>. When both are visible the preprocessor replaces the function
call site with NULL, causing compilation errors such as:

error: expected expression
    NULL (fd, type)
    ^

Fix

Add && !defined(__APPLE__) to the guard so that on genuine Apple/macOS builds
the real fdopen() from <stdio.h> is used. The macro remains in place for
non-Apple platforms where it was originally needed.

Testing

Built as a Hunter dependency of koinos-chain on macOS 15 (Apple M-series, arm64)
with Xcode 16.3.

ruslo and others added 8 commits October 11, 2023 10:30
To be consistent with the remaining file replace the two occurences of
tabs with 4 spaces.
On macOS, TARGET_OS_MAC is defined by the SDK headers. zutil.h defines
fdopen(fd,mode) as NULL under this condition, but macOS stdio.h already
declares fdopen() as a proper function — causing a redeclaration conflict
when both headers are included in the same translation unit (e.g. when
gRPC or other C++ libraries pull in both zlib and stdio headers).

Add && !defined(__APPLE__) so the fdopen NULL macro only applies to
legacy Mac OS (pre-macOS) targets, not modern macOS where the libc
provides a real fdopen implementation.

Fixes: compile error with Apple Clang on macOS 10.15+ (Catalina and later)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants