From 3805650736846c4e46cad85107dd81a70515ec11 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 22 Nov 2013 11:54:32 +1030 Subject: [PATCH] Use ssize_t for sendto(2) return value, not int Also improve error checking on ssize_t values (use ==-1 not <0) test and cast ssize_t to size_t for comparisons --- mdp_client.c | 4 ++-- overlay_interface.c | 8 ++++---- overlay_mdp.c | 21 +++++++++------------ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/mdp_client.c b/mdp_client.c index b8ef160e..c7231903 100644 --- a/mdp_client.c +++ b/mdp_client.c @@ -122,7 +122,7 @@ int overlay_mdp_send(int mdp_sockfd, overlay_mdp_frame *mdp, int flags, int time return -1; // Minimise frame length to save work and prevent accidental disclosure of memory contents. ssize_t len = overlay_mdp_relevant_bytes(mdp); - if (len < 0) + if (len == -1) return WHY("MDP frame invalid (could not compute length)"); /* Construct name of socket to send to. */ struct sockaddr_un addr; @@ -131,7 +131,7 @@ int overlay_mdp_send(int mdp_sockfd, overlay_mdp_frame *mdp, int flags, int time return -1; // Send to that socket set_nonblock(mdp_sockfd); - int result = sendto(mdp_sockfd, mdp, len, 0, (struct sockaddr *)&addr, addrlen); + ssize_t result = sendto(mdp_sockfd, mdp, (size_t)len, 0, (struct sockaddr *)&addr, addrlen); set_block(mdp_sockfd); if (result == -1) { mdp->packetTypeAndFlags=MDP_ERROR; diff --git a/overlay_interface.c b/overlay_interface.c index 55b547de..f71f4208 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -976,12 +976,12 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o case SOCK_DGRAM: { if (config.debug.overlayinterfaces) - DEBUGF("Sending %d byte overlay frame on %s to %s",len,interface->name,inet_ntoa(destination->address.sin_addr)); - int sent=sendto(interface->alarm.poll.fd, - bytes, len, 0, + DEBUGF("Sending %zu byte overlay frame on %s to %s", (size_t)len, interface->name, inet_ntoa(destination->address.sin_addr)); + ssize_t sent = sendto(interface->alarm.poll.fd, + bytes, (size_t)len, 0, (struct sockaddr *)&destination->address, sizeof(destination->address)); ob_free(buffer); - if (sent!= len){ + if (sent == -1 || (size_t)sent != (size_t)len) { WHYF_perror("sendto(fd=%d,len=%zu,addr=%s)", interface->alarm.poll.fd, (size_t)len, diff --git a/overlay_mdp.c b/overlay_mdp.c index 03c0a794..9ad5dfdd 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -154,19 +154,16 @@ int overlay_mdp_reply_error(int sock, int overlay_mdp_reply(int sock,struct sockaddr_un *recvaddr, socklen_t recvaddrlen, overlay_mdp_frame *mdpreply) { - if (!recvaddr) return WHY("No reply address"); - + if (!recvaddr) + return WHY("No reply address"); ssize_t replylen = overlay_mdp_relevant_bytes(mdpreply); - if (replylen<0) return WHY("Invalid MDP frame (could not compute length)"); - - errno=0; - int r=sendto(sock,(char *)mdpreply,replylen,0, - (struct sockaddr *)recvaddr,recvaddrlen); - if (r