Skip to content

Commit 5e22484

Browse files
committed
Avoid UB from socket.h macros
Only when building with UBSAN for now.
1 parent 1137699 commit 5e22484

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

ext/sockets/conversions.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ struct _WSAMSG {
6060
#define MAX_USER_BUFF_SIZE ((size_t)(100*1024*1024))
6161
#define DEFAULT_BUFF_SIZE 8192
6262

63+
/* The CMSG_DATA macro does pointer arithmetics on NULL which triggers errors in the Clang UBSAN build */
64+
#ifdef __linux__
65+
# if __has_feature(address_sanitizer)
66+
# undef CMSG_DATA
67+
# define CMSG_DATA(cmsg) ((unsigned char *) ((uintptr_t) (cmsg) + sizeof(struct cmsghdr)))
68+
# endif
69+
#endif
70+
6371
struct _ser_context {
6472
HashTable params; /* stores pointers; has to be first */
6573
struct err_s err;

ext/sockets/sockets.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@
7676

7777
ZEND_DECLARE_MODULE_GLOBALS(sockets)
7878

79-
#ifndef SUN_LEN
80-
#define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
79+
/* The SUN_LEN macro does pointer arithmetics on NULL which triggers errors in the Clang UBSAN build */
80+
#ifdef __linux__
81+
# if __has_feature(address_sanitizer)
82+
# undef SUN_LEN
83+
# define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
84+
# endif
8185
#endif
8286

8387
#ifndef PF_INET

0 commit comments

Comments
 (0)