Skip to content

Commit

Permalink
socket: don't truncate a datagram in recv/recvfrom
Browse files Browse the repository at this point in the history
We set MSG_TRUNC for recv / recvfrom calls to receive the real length of
the datagram. When the datagram length is larger then the buffer size we
set the EMSGSIZE errno, but still return `from` table (in case of
recvfrom).

Fixes #3619.
  • Loading branch information
Totktonada committed Aug 23, 2018
1 parent 5214ff6 commit 21fe3d8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/lua/socket.c
Expand Up @@ -950,6 +950,7 @@ lbox_socket_recvfrom(struct lua_State *L)
int fh = lua_tointeger(L, 1);
int size = lua_tointeger(L, 2);
int flags = lua_tointeger(L, 3);
flags |= MSG_TRUNC;

struct sockaddr_storage fa;
socklen_t len = sizeof(fa);
Expand All @@ -968,6 +969,12 @@ lbox_socket_recvfrom(struct lua_State *L)
free(buf);
lua_pushnil(L);
return 1;
} else if (res > size) {
free(buf);
lua_pushnil(L);
lbox_socket_push_addr(L, (struct sockaddr *)&fa, len);
errno = EMSGSIZE;
return 2;
}

lua_pushcfunction(L, lbox_socket_recvfrom_wrapper);
Expand Down
6 changes: 5 additions & 1 deletion src/lua/socket.lua
Expand Up @@ -811,6 +811,7 @@ local function socket_recv(self, size, flags)
if size == nil then
return nil
end
iflags = bit.bor(iflags, internal.SEND_FLAGS['MSG_TRUNC'])
else
size = size or 512
end
Expand All @@ -823,6 +824,9 @@ local function socket_recv(self, size, flags)
if res == -1 then
self._errno = boxerrno()
return nil
elseif res > size then
self._errno = boxerrno.EMSGSIZE
return nil
end
return ffi.string(buf, res)
end
Expand All @@ -848,7 +852,7 @@ local function socket_recvfrom(self, size, flags)
local res, from = internal.recvfrom(fd, size, iflags)
if res == nil then
self._errno = boxerrno()
return nil
return nil, from
end
return res, from
end
Expand Down
56 changes: 55 additions & 1 deletion test/app/socket.result
Expand Up @@ -884,7 +884,7 @@ data, from = s:recvfrom(10)
...
data
---
- Hello, Wor
- null
...
s:sendto(from.host, from.port, 'Hello, hello!')
---
Expand Down Expand Up @@ -2626,6 +2626,60 @@ e
---
- 0
...
-- case: recv, datagram that is larger then the buffer of an explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
---
- 1025
...
received_message = receiving_socket:recv(512)
---
...
e = receiving_socket:errno()
---
...
received_message == nil -- expected true
---
- true
...
received_message
---
- null
...
e == errno.EMSGSIZE -- expected true
---
- true
...
-- case: recvfrom, datagram that is larger then the buffer of an explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
---
- 1025
...
received_message, from = receiving_socket:recvfrom(512)
---
...
e = receiving_socket:errno()
---
...
received_message == nil -- expected true
---
- true
...
received_message
---
- null
...
from ~= nil -- expected true
---
- true
...
from.host == '127.0.0.1' -- expected true
---
- true
...
e == errno.EMSGSIZE -- expected true
---
- true
...
receiving_socket:close()
---
- true
Expand Down
18 changes: 18 additions & 0 deletions test/app/socket.test.lua
Expand Up @@ -887,6 +887,24 @@ from.host == '127.0.0.1' -- expected true
e == 0 -- expected true
e

-- case: recv, datagram that is larger then the buffer of an explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message = receiving_socket:recv(512)
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
e == errno.EMSGSIZE -- expected true

-- case: recvfrom, datagram that is larger then the buffer of an explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message, from = receiving_socket:recvfrom(512)
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
from ~= nil -- expected true
from.host == '127.0.0.1' -- expected true
e == errno.EMSGSIZE -- expected true

receiving_socket:close()
sending_socket:close()

Expand Down

0 comments on commit 21fe3d8

Please sign in to comment.