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
This commit is contained in:
Andrew Bettison 2013-11-22 11:54:32 +10:30
parent e92e7cb65e
commit 3805650736
3 changed files with 15 additions and 18 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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<replylen) {
if (replylen == -1)
return WHY("Invalid MDP frame (could not compute length)");
ssize_t r = sendto(sock, (char *)mdpreply, (size_t)replylen, 0, (struct sockaddr *)recvaddr, recvaddrlen);
if (r == -1 || (size_t)r < (size_t)replylen) {
WHYF_perror("sendto(fd=%d,len=%zu,addr=%s)", sock, (size_t)replylen, alloca_sockaddr((struct sockaddr *)recvaddr, recvaddrlen));
return WHYF("sendto() failed when sending MDP reply, sock=%d, r=%d", sock, r);
} else
if (0) DEBUGF("reply of %d bytes sent",r);
}
return 0;
}
@ -450,8 +447,8 @@ static int overlay_saw_mdp_frame(struct overlay_frame *frame, overlay_mdp_frame
if (len < 0)
RETURN(WHY("unsupported MDP packet type"));
socklen_t addrlen = sizeof addr.sun_family + mdp_bindings[match].name_len;
int r = sendto(mdp_sock.poll.fd,mdp,len,0,(struct sockaddr*)&addr, addrlen);
if (r == len)
ssize_t r = sendto(mdp_sock.poll.fd, mdp, (size_t)len, 0, (struct sockaddr*)&addr, addrlen);
if ((size_t)r == (size_t)len)
RETURN(0);
if (r == -1 && errno == ENOENT) {
/* far-end of socket has died, so drop binding */