# HG changeset patch # User jbe # Date 1548098630 -3600 # Node ID fe1a134a92a40b0423ed26b80212410199b794f1 # Parent 9509f1e5e31ac9705f46174a4f7d38bc45ebcea8 Use offsetof macro at compile time to determine maximum length of local socket paths diff -r 9509f1e5e31a -r fe1a134a92a4 moonbridge_io.c --- a/moonbridge_io.c Sat Dec 22 18:42:18 2018 +0100 +++ b/moonbridge_io.c Mon Jan 21 20:23:50 2019 +0100 @@ -55,6 +55,8 @@ moonbr_io_return_prepared_errmsg(); \ } while (0) +#define MOONBR_SUN_PATH_MAXLEN (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path) - 1) + #define MOONBR_IO_MODULE_REGKEY "moonbridge_io_module" #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle" #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" @@ -1143,12 +1145,9 @@ static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) { const char *path; struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL }; - const int path_maxlen = sizeof(struct sockaddr_un) - ( - (void *)sockaddr.sun_path - (void *)&sockaddr - ) - 1; /* one byte for termination */ int sock; path = luaL_checkstring(L, 1); - if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen); + if (strlen(path) > MOONBR_SUN_PATH_MAXLEN) luaL_error(L, "Path too long; only %i characters allowed", MOONBR_SUN_PATH_MAXLEN); strcpy(sockaddr.sun_path, path); sock = socket( PF_LOCAL,