diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-30 13:46:46 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-30 13:51:57 +0200 |
commit | 4c22e20dcce870551ae7e59385f2a964a6fe3588 (patch) | |
tree | c3ead920e5154335ddea29af63b5a100a1dd50b4 /src/net.c | |
parent | net: more portable (diff) | |
download | cimple-4c22e20dcce870551ae7e59385f2a964a6fe3588.tar.gz cimple-4c22e20dcce870551ae7e59385f2a964a6fe3588.zip |
net: error out unless net_recv_all receives all bytes
Diffstat (limited to 'src/net.c')
-rw-r--r-- | src/net.c | 27 |
1 files changed, 13 insertions, 14 deletions
@@ -166,7 +166,7 @@ int net_send_all(int fd, const void *buf, size_t len) return 0; } -ssize_t net_recv_all(int fd, void *buf, size_t len) +int net_recv_all(int fd, void *buf, size_t len) { ssize_t read_total = 0; @@ -183,7 +183,12 @@ ssize_t net_recv_all(int fd, void *buf, size_t len) read_total += read_now; } - return read_total; + if ((size_t)read_total < len) { + print_error("Received only %zd bytes out of %zu\n", read_total, len); + return -1; + } + + return 0; } int net_send_buf(int fd, const void *buf, uint32_t len) @@ -205,13 +210,10 @@ int net_send_buf(int fd, const void *buf, uint32_t len) int net_recv_buf(int fd, void **buf, uint32_t *len) { - ssize_t nb = 0; - - nb = net_recv_all(fd, len, sizeof(*len)); - if (nb < 0) - goto fail; + int ret = 0; - if (nb != sizeof(*len)) { + ret = net_recv_all(fd, len, sizeof(*len)); + if (ret < 0) { print_error("Couldn't read buffer length\n"); goto fail; } @@ -224,12 +226,9 @@ int net_recv_buf(int fd, void **buf, uint32_t *len) goto fail; } - nb = net_recv_all(fd, *buf, *len); - if (nb < 0) - goto free_buf; - - if ((size_t)nb != *len) { - print_error("Couldn't read the entire buffer\n"); + ret = net_recv_all(fd, *buf, *len); + if (ret < 0) { + print_error("Couldn't read buffer\n"); goto free_buf; } |