From 271d96e4fcae7228e460cba558041f4656588f22 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Mon, 24 Jun 2019 16:36:47 +1000 Subject: [PATCH] ctdb-common: Fix error handling According to the documentation, sendto() should either send the packet as given or return with an error. However, given that it can return the number of bytes sent, treat the theoretical error of a short packet send separately, since errno would not be set in this case. Similarly, treat a short packet recv() separately from an error where errno is set. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/common/system_socket.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/ctdb/common/system_socket.c b/ctdb/common/system_socket.c index c6800431112..86cbdaab6ad 100644 --- a/ctdb/common/system_socket.c +++ b/ctdb/common/system_socket.c @@ -681,10 +681,14 @@ int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, sizeof(dest->ip)); saved_errno = errno; close(s); - if (ret != len) { + if (ret == -1) { D_ERR("Failed sendto (%s)\n", strerror(saved_errno)); return -1; } + if ((size_t)ret != len) { + DBG_ERR("Failed sendto - didn't send full packet\n"); + return -1; + } break; case AF_INET6: @@ -722,11 +726,14 @@ int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, sizeof(tmpdest)); saved_errno = errno; close(s); - - if (ret != len) { + if (ret == -1) { D_ERR("Failed sendto (%s)\n", strerror(saved_errno)); return -1; } + if ((size_t)ret != len) { + DBG_ERR("Failed sendto - didn't send full packet\n"); + return -1; + } break; default: @@ -914,7 +921,10 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, int ret; nread = recv(s, pkt, sizeof(pkt), MSG_TRUNC); - if (nread < sizeof(*eth)) { + if (nread == -1) { + return errno; + } + if ((size_t)nread < sizeof(*eth)) { return EMSGSIZE; } -- 2.34.1