Skip to content

Commit 958e826

Browse files
pfalcongalak
authored andcommitted
net: socket: Workaround issue with recent GCC and fcntl macro
If we have CONFIG_NET_SOCKETS_POSIX_NAMES enabled (which is now default), and also have CONFIG_NEWLIB_LIBC enabled, latest versions of GCC throw a strange error like: error: conflicting types for 'zsock_fcntl' 692 | #define fcntl zsock_fcntl After enough consideration, it seems that when Newlib is used, its fcntl.h header is used, which declares fcntl() with POSIX prototype: "int fcntl(int fd, int cmd, ...)". It seems that recent GCC, when seeing the #define like above, checks that its right-hand side (zsock_fcntl(int, int, int) above) is compatible with an existing LHS prototype. That doesn't make sense from the point of view of the C preprocessor semantics, and yet that's what apparently happens. Make GCC happy by defining an inline wrapper function with signature compatible with POSIX fcntl prototype, and use it in the define, instead of zsock_fcntl directly. Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent 35f598b commit 958e826

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

include/net/socket.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,22 @@ static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
688688
return zsock_recv(sock, buf, max_len, flags);
689689
}
690690

691-
/* This conflicts with fcntl.h, so code must include fcntl.h before socket.h: */
692-
#define fcntl zsock_fcntl
691+
/*
692+
* Need this wrapper because newer GCC versions got too smart and "typecheck"
693+
* even macros, so '#define fcntl zsock_fcntl' leads to error.
694+
*/
695+
static inline int zsock_fcntl_wrapper(int sock, int cmd, ...)
696+
{
697+
va_list args;
698+
int flags;
699+
700+
va_start(args, cmd);
701+
flags = va_arg(args, int);
702+
va_end(args);
703+
return zsock_fcntl(sock, cmd, flags);
704+
}
705+
706+
#define fcntl zsock_fcntl_wrapper
693707

694708
static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
695709
const struct sockaddr *dest_addr,

0 commit comments

Comments
 (0)