diff options
| author | Caleb Maclennan <caleb@alerque.com> | 2026-08-31 11:00:36 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-31 11:00:36 +0300 |
| commit | b836466de5a78e9b4f29ca6c85a2bdecd7c69da9 (patch) | |
| tree | 465b3b3a8121b1625b7ee3c9fba2e1f3cdce8a3c /src/wsocket.c | |
| parent | 8f18ce95bb38c7f5c4bef5b3684cf1b0df1fc266 (diff) | |
| parent | 75d638ac9cc613f39ec4513d3dc2e43a6f909da1 (diff) | |
| download | luasocket-b836466de5a78e9b4f29ca6c85a2bdecd7c69da9.tar.gz luasocket-b836466de5a78e9b4f29ca6c85a2bdecd7c69da9.tar.bz2 luasocket-b836466de5a78e9b4f29ca6c85a2bdecd7c69da9.zip | |
Merge pull request #471 from lunarmodules/fix/zero-read
fix(receive): a receive 0 should immediately return, not block
Diffstat (limited to 'src/wsocket.c')
| -rw-r--r-- | src/wsocket.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/wsocket.c b/src/wsocket.c index d3af9d4..b2b668c 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
| @@ -247,6 +247,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, | |||
| 247 | int err, prev = IO_DONE; | 247 | int err, prev = IO_DONE; |
| 248 | *got = 0; | 248 | *got = 0; |
| 249 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 249 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| 250 | if (count == 0) return IO_DONE; | ||
| 250 | for ( ;; ) { | 251 | for ( ;; ) { |
| 251 | int taken = recv(*ps, data, (int) count, 0); | 252 | int taken = recv(*ps, data, (int) count, 0); |
| 252 | if (taken > 0) { | 253 | if (taken > 0) { |
| @@ -285,6 +286,14 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, | |||
| 285 | } | 286 | } |
| 286 | if (taken == 0) return IO_CLOSED; | 287 | if (taken == 0) return IO_CLOSED; |
| 287 | err = WSAGetLastError(); | 288 | err = WSAGetLastError(); |
| 289 | /* a zero-length request is trivially "too small" for any | ||
| 290 | * non-empty datagram; unlike POSIX, which truncates and succeeds | ||
| 291 | * silently, Windows reports this as WSAEMSGSIZE even though the | ||
| 292 | * datagram -- and its sender's address, already written to addr/ | ||
| 293 | * len above -- was still consumed. Normalize it to match POSIX's | ||
| 294 | * silent-truncation instead of surfacing a platform-specific | ||
| 295 | * error for what is otherwise a successful, if empty, receive. */ | ||
| 296 | if (count == 0 && err == WSAEMSGSIZE) return IO_DONE; | ||
| 288 | /* On UDP, a connreset simply means the previous send failed. | 297 | /* On UDP, a connreset simply means the previous send failed. |
| 289 | * So we try again. | 298 | * So we try again. |
| 290 | * On TCP, it means our socket is now useless, so the error passes. | 299 | * On TCP, it means our socket is now useless, so the error passes. |
