diff options
| author | Kim Alvefur <zash@zash.se> | 2024-05-24 12:28:51 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-24 13:28:51 +0300 |
| commit | e3ca4a767a68d127df548d82669aba3689bd84f4 (patch) | |
| tree | 35ddba3aa60d47a9400291b5db8d6eeabc231c2d /src/unixstream.c | |
| parent | 93eef5015e0dfe8d24071dd036f9a7f02160abeb (diff) | |
| download | luasocket-e3ca4a767a68d127df548d82669aba3689bd84f4.tar.gz luasocket-e3ca4a767a68d127df548d82669aba3689bd84f4.tar.bz2 luasocket-e3ca4a767a68d127df548d82669aba3689bd84f4.zip | |
fix(unix): Pass correct path length for abstract sockets (#430)
Diffstat (limited to 'src/unixstream.c')
| -rw-r--r-- | src/unixstream.c | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/unixstream.c b/src/unixstream.c index 02aced9..89a8776 100644 --- a/src/unixstream.c +++ b/src/unixstream.c | |||
| @@ -33,8 +33,8 @@ static int meth_getstats(lua_State *L); | |||
| 33 | static int meth_setstats(lua_State *L); | 33 | static int meth_setstats(lua_State *L); |
| 34 | static int meth_getsockname(lua_State *L); | 34 | static int meth_getsockname(lua_State *L); |
| 35 | 35 | ||
| 36 | static const char *unixstream_tryconnect(p_unix un, const char *path); | 36 | static const char *unixstream_tryconnect(p_unix un, const char *path, size_t len); |
| 37 | static const char *unixstream_trybind(p_unix un, const char *path); | 37 | static const char *unixstream_trybind(p_unix un, const char *path, size_t len); |
| 38 | 38 | ||
| 39 | /* unixstream object methods */ | 39 | /* unixstream object methods */ |
| 40 | static luaL_Reg unixstream_methods[] = { | 40 | static luaL_Reg unixstream_methods[] = { |
| @@ -181,13 +181,12 @@ static int meth_accept(lua_State *L) { | |||
| 181 | /*-------------------------------------------------------------------------*\ | 181 | /*-------------------------------------------------------------------------*\ |
| 182 | * Binds an object to an address | 182 | * Binds an object to an address |
| 183 | \*-------------------------------------------------------------------------*/ | 183 | \*-------------------------------------------------------------------------*/ |
| 184 | static const char *unixstream_trybind(p_unix un, const char *path) { | 184 | static const char *unixstream_trybind(p_unix un, const char *path, size_t len) { |
| 185 | struct sockaddr_un local; | 185 | struct sockaddr_un local; |
| 186 | size_t len = strlen(path); | ||
| 187 | int err; | 186 | int err; |
| 188 | if (len >= sizeof(local.sun_path)) return "path too long"; | 187 | if (len >= sizeof(local.sun_path)) return "path too long"; |
| 189 | memset(&local, 0, sizeof(local)); | 188 | memset(&local, 0, sizeof(local)); |
| 190 | strcpy(local.sun_path, path); | 189 | memcpy(local.sun_path, path, len); |
| 191 | local.sun_family = AF_UNIX; | 190 | local.sun_family = AF_UNIX; |
| 192 | #ifdef UNIX_HAS_SUN_LEN | 191 | #ifdef UNIX_HAS_SUN_LEN |
| 193 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 192 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) |
| @@ -204,8 +203,9 @@ static const char *unixstream_trybind(p_unix un, const char *path) { | |||
| 204 | 203 | ||
| 205 | static int meth_bind(lua_State *L) { | 204 | static int meth_bind(lua_State *L) { |
| 206 | p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1); | 205 | p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1); |
| 207 | const char *path = luaL_checkstring(L, 2); | 206 | size_t len; |
| 208 | const char *err = unixstream_trybind(un, path); | 207 | const char *path = luaL_checklstring(L, 2, &len); |
| 208 | const char *err = unixstream_trybind(un, path, len); | ||
| 209 | if (err) { | 209 | if (err) { |
| 210 | lua_pushnil(L); | 210 | lua_pushnil(L); |
| 211 | lua_pushstring(L, err); | 211 | lua_pushstring(L, err); |
| @@ -234,14 +234,13 @@ static int meth_getsockname(lua_State *L) | |||
| 234 | /*-------------------------------------------------------------------------*\ | 234 | /*-------------------------------------------------------------------------*\ |
| 235 | * Turns a master unixstream object into a client object. | 235 | * Turns a master unixstream object into a client object. |
| 236 | \*-------------------------------------------------------------------------*/ | 236 | \*-------------------------------------------------------------------------*/ |
| 237 | static const char *unixstream_tryconnect(p_unix un, const char *path) | 237 | static const char *unixstream_tryconnect(p_unix un, const char *path, size_t len) |
| 238 | { | 238 | { |
| 239 | struct sockaddr_un remote; | 239 | struct sockaddr_un remote; |
| 240 | int err; | 240 | int err; |
| 241 | size_t len = strlen(path); | ||
| 242 | if (len >= sizeof(remote.sun_path)) return "path too long"; | 241 | if (len >= sizeof(remote.sun_path)) return "path too long"; |
| 243 | memset(&remote, 0, sizeof(remote)); | 242 | memset(&remote, 0, sizeof(remote)); |
| 244 | strcpy(remote.sun_path, path); | 243 | memcpy(remote.sun_path, path, len); |
| 245 | remote.sun_family = AF_UNIX; | 244 | remote.sun_family = AF_UNIX; |
| 246 | timeout_markstart(&un->tm); | 245 | timeout_markstart(&un->tm); |
| 247 | #ifdef UNIX_HAS_SUN_LEN | 246 | #ifdef UNIX_HAS_SUN_LEN |
| @@ -259,8 +258,9 @@ static const char *unixstream_tryconnect(p_unix un, const char *path) | |||
| 259 | static int meth_connect(lua_State *L) | 258 | static int meth_connect(lua_State *L) |
| 260 | { | 259 | { |
| 261 | p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1); | 260 | p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1); |
| 262 | const char *path = luaL_checkstring(L, 2); | 261 | size_t len; |
| 263 | const char *err = unixstream_tryconnect(un, path); | 262 | const char *path = luaL_checklstring(L, 2, &len); |
| 263 | const char *err = unixstream_tryconnect(un, path, len); | ||
| 264 | if (err) { | 264 | if (err) { |
| 265 | lua_pushnil(L); | 265 | lua_pushnil(L); |
| 266 | lua_pushstring(L, err); | 266 | lua_pushstring(L, err); |
