Skip to content

Commit

Permalink
fix(unix): Pass correct path length for abstract sockets
Browse files Browse the repository at this point in the history
Since they start with NUL byte, strlen() does the wrong thing.
The Lua API can already pass the real string length, so passing that on
seems the sensible thing to do.

Fixes #216
  • Loading branch information
Zash committed Apr 27, 2024
1 parent 93eef50 commit 81a3d0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/unixdgram.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ static int meth_receivefrom(lua_State *L);
static int meth_sendto(lua_State *L);
static int meth_getsockname(lua_State *L);

static const char *unixdgram_tryconnect(p_unix un, const char *path);
static const char *unixdgram_trybind(p_unix un, const char *path);
static const char *unixdgram_tryconnect(p_unix un, const char *path, size_t len);
static const char *unixdgram_trybind(p_unix un, const char *path, size_t len);

/* unixdgram object methods */
static luaL_Reg unixdgram_methods[] = {
Expand Down Expand Up @@ -264,9 +264,8 @@ static int meth_dirty(lua_State *L) {
/*-------------------------------------------------------------------------*\
* Binds an object to an address
\*-------------------------------------------------------------------------*/
static const char *unixdgram_trybind(p_unix un, const char *path) {
static const char *unixdgram_trybind(p_unix un, const char *path, size_t len) {
struct sockaddr_un local;
size_t len = strlen(path);
if (len >= sizeof(local.sun_path)) return "path too long";
memset(&local, 0, sizeof(local));
strcpy(local.sun_path, path);
Expand All @@ -283,8 +282,9 @@ static const char *unixdgram_trybind(p_unix un, const char *path) {
static int meth_bind(lua_State *L)
{
p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
const char *path = luaL_checkstring(L, 2);
const char *err = unixdgram_trybind(un, path);
size_t len;
const char *path = luaL_checklstring(L, 2, &len);
const char *err = unixdgram_trybind(un, path, len);
if (err) {
lua_pushnil(L);
lua_pushstring(L, err);
Expand Down Expand Up @@ -313,10 +313,9 @@ static int meth_getsockname(lua_State *L)
/*-------------------------------------------------------------------------*\
* Turns a master unixdgram object into a client object.
\*-------------------------------------------------------------------------*/
static const char *unixdgram_tryconnect(p_unix un, const char *path)
static const char *unixdgram_tryconnect(p_unix un, const char *path, size_t len)
{
struct sockaddr_un remote;
size_t len = strlen(path);
if (len >= sizeof(remote.sun_path)) return "path too long";
memset(&remote, 0, sizeof(remote));
strcpy(remote.sun_path, path);
Expand All @@ -334,8 +333,9 @@ static const char *unixdgram_tryconnect(p_unix un, const char *path)
static int meth_connect(lua_State *L)
{
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
const char *path = luaL_checkstring(L, 2);
const char *err = unixdgram_tryconnect(un, path);
size_t len;
const char *path = luaL_checklstring(L, 2, &len);
const char *err = unixdgram_tryconnect(un, path, len);
if (err) {
lua_pushnil(L);
lua_pushstring(L, err);
Expand Down
20 changes: 10 additions & 10 deletions src/unixstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ static int meth_getstats(lua_State *L);
static int meth_setstats(lua_State *L);
static int meth_getsockname(lua_State *L);

static const char *unixstream_tryconnect(p_unix un, const char *path);
static const char *unixstream_trybind(p_unix un, const char *path);
static const char *unixstream_tryconnect(p_unix un, const char *path, size_t len);
static const char *unixstream_trybind(p_unix un, const char *path, size_t len);

/* unixstream object methods */
static luaL_Reg unixstream_methods[] = {
Expand Down Expand Up @@ -181,9 +181,8 @@ static int meth_accept(lua_State *L) {
/*-------------------------------------------------------------------------*\
* Binds an object to an address
\*-------------------------------------------------------------------------*/
static const char *unixstream_trybind(p_unix un, const char *path) {
static const char *unixstream_trybind(p_unix un, const char *path, size_t len) {
struct sockaddr_un local;
size_t len = strlen(path);
int err;
if (len >= sizeof(local.sun_path)) return "path too long";
memset(&local, 0, sizeof(local));
Expand All @@ -204,8 +203,9 @@ static const char *unixstream_trybind(p_unix un, const char *path) {

static int meth_bind(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1);
const char *path = luaL_checkstring(L, 2);
const char *err = unixstream_trybind(un, path);
size_t len;
const char *path = luaL_checklstring(L, 2, &len);
const char *err = unixstream_trybind(un, path, len);
if (err) {
lua_pushnil(L);
lua_pushstring(L, err);
Expand Down Expand Up @@ -234,11 +234,10 @@ static int meth_getsockname(lua_State *L)
/*-------------------------------------------------------------------------*\
* Turns a master unixstream object into a client object.
\*-------------------------------------------------------------------------*/
static const char *unixstream_tryconnect(p_unix un, const char *path)
static const char *unixstream_tryconnect(p_unix un, const char *path, size_t len)
{
struct sockaddr_un remote;
int err;
size_t len = strlen(path);
if (len >= sizeof(remote.sun_path)) return "path too long";
memset(&remote, 0, sizeof(remote));
strcpy(remote.sun_path, path);
Expand All @@ -259,8 +258,9 @@ static const char *unixstream_tryconnect(p_unix un, const char *path)
static int meth_connect(lua_State *L)
{
p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1);
const char *path = luaL_checkstring(L, 2);
const char *err = unixstream_tryconnect(un, path);
size_t len;
const char *path = luaL_checklstring(L, 2, &len);
const char *err = unixstream_tryconnect(un, path, len);
if (err) {
lua_pushnil(L);
lua_pushstring(L, err);
Expand Down

0 comments on commit 81a3d0a

Please sign in to comment.