moonbridge

changeset 322:fe1a134a92a4 tip

Use offsetof macro at compile time to determine maximum length of local socket paths
author jbe
date Mon Jan 21 20:23:50 2019 +0100 (7 months ago)
parents 9509f1e5e31a
children
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Sat Dec 22 18:42:18 2018 +0100
     1.2 +++ b/moonbridge_io.c	Mon Jan 21 20:23:50 2019 +0100
     1.3 @@ -55,6 +55,8 @@
     1.4      moonbr_io_return_prepared_errmsg(); \
     1.5    } while (0)
     1.6  
     1.7 +#define MOONBR_SUN_PATH_MAXLEN (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path) - 1)
     1.8 +
     1.9  #define MOONBR_IO_MODULE_REGKEY "moonbridge_io_module"
    1.10  #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
    1.11  #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
    1.12 @@ -1143,12 +1145,9 @@
    1.13  static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
    1.14    const char *path;
    1.15    struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
    1.16 -  const int path_maxlen = sizeof(struct sockaddr_un) - (
    1.17 -    (void *)sockaddr.sun_path - (void *)&sockaddr
    1.18 -  ) - 1;  /* one byte for termination */
    1.19    int sock;
    1.20    path = luaL_checkstring(L, 1);
    1.21 -  if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
    1.22 +  if (strlen(path) > MOONBR_SUN_PATH_MAXLEN) luaL_error(L, "Path too long; only %i characters allowed", MOONBR_SUN_PATH_MAXLEN);
    1.23    strcpy(sockaddr.sun_path, path);
    1.24    sock = socket(
    1.25      PF_LOCAL,

Impressum / About Us