From 3d76fbeedc86a982008787d3ce95ede8ee5eeb24 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sun, 6 Apr 2014 06:24:13 -0400 Subject: [PATCH] Make sure that all safe_* return values use ssize_t. --- ndhc/arp.c | 4 ++-- ndhc/dhcp.c | 36 +++++++++++++++++++----------------- ndhc/dhcp.h | 12 ++++++------ ndhc/duiaid.c | 14 ++++++++------ ndhc/ifchange.c | 2 +- ndhc/leasefile.c | 2 +- ndhc/ndhc.c | 2 +- 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/ndhc/arp.c b/ndhc/arp.c index ac866f1..8e65ce8 100644 --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -709,7 +709,7 @@ static const arp_state_fn_t arp_states[] = { void handle_arp_response(struct client_state_t *cs) { - int r = 0; + ssize_t r = 0; if (arpreply_offset < sizeof arpreply) { r = safe_read(cs->arpFd, (char *)&arpreply + arpreply_offset, sizeof arpreply - arpreply_offset); @@ -721,7 +721,7 @@ void handle_arp_response(struct client_state_t *cs) default: arp_reopen_fd(cs); break; } } else - arpreply_offset += r; + arpreply_offset += (size_t)r; } if (r <= 0) { diff --git a/ndhc/dhcp.c b/ndhc/dhcp.c index 97c0169..44df7cb 100644 --- a/ndhc/dhcp.c +++ b/ndhc/dhcp.c @@ -86,9 +86,10 @@ static int get_raw_listen_socket(struct client_state_t *cs) } // Broadcast a DHCP message using a UDP socket. -static int send_dhcp_cooked(struct client_state_t *cs, struct dhcpmsg *payload) +static ssize_t send_dhcp_cooked(struct client_state_t *cs, + struct dhcpmsg *payload) { - int ret = -1; + ssize_t ret = -1; int fd = get_udp_unicast_socket(); if (fd == -1) goto out; @@ -125,11 +126,11 @@ static int send_dhcp_cooked(struct client_state_t *cs, struct dhcpmsg *payload) // Read a packet from a cooked socket. Returns -1 on fatal error, -2 on // transient error. -static int get_cooked_packet(struct dhcpmsg *packet, int fd) +static ssize_t get_cooked_packet(struct dhcpmsg *packet, int fd) { memset(packet, 0, sizeof *packet); - int bytes = safe_read(fd, (char *)packet, sizeof *packet); - if (bytes == -1) { + ssize_t bytes = safe_read(fd, (char *)packet, sizeof *packet); + if (bytes < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) return -2; log_line("%s: Read on listen socket failed: %s", @@ -237,13 +238,14 @@ static int get_raw_packet_validate_bpf(struct ip_udp_dhcp_packet *packet) // Read a packet from a raw socket. Returns -1 on fatal error, -2 on // transient error. -static int get_raw_packet(struct client_state_t *cs, struct dhcpmsg *payload) +static ssize_t get_raw_packet(struct client_state_t *cs, + struct dhcpmsg *payload) { struct ip_udp_dhcp_packet packet; memset(&packet, 0, sizeof packet); ssize_t inc = safe_read(cs->listenFd, (char *)&packet, sizeof packet); - if (inc == -1) { + if (inc < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) return -2; log_warning("%s: (%s) read error %s", client_config.interface, @@ -277,9 +279,9 @@ static int get_raw_packet(struct client_state_t *cs, struct dhcpmsg *payload) } // Broadcast a DHCP message using a raw socket. -static int send_dhcp_raw(struct dhcpmsg *payload) +static ssize_t send_dhcp_raw(struct dhcpmsg *payload) { - int ret = -1; + ssize_t ret = -1; int fd = get_raw_broadcast_socket(); if (fd == -1) return ret; @@ -333,7 +335,7 @@ static int send_dhcp_raw(struct dhcpmsg *payload) memcpy(da.sll_addr, "\xff\xff\xff\xff\xff\xff", 6); ret = safe_sendto(fd, (const char *)&iudmsg, iud_len, 0, (struct sockaddr *)&da, sizeof da); - if (ret == -1) + if (ret < 0) log_error("%s: (%s) sendto failed: %s", client_config.interface, __func__, strerror(errno)); close(fd); @@ -429,7 +431,7 @@ void handle_packet(struct client_state_t *cs) if (cs->listenMode == LM_NONE) return; - int r = cs->listenMode == LM_RAW ? + ssize_t r = cs->listenMode == LM_RAW ? get_raw_packet(cs, &packet) : get_cooked_packet(&packet, cs->listenFd); if (r < 0) { // Transient issue handled by packet collection functions. @@ -465,7 +467,7 @@ static struct dhcpmsg init_packet(char type, uint32_t xid) return packet; } -int send_discover(struct client_state_t *cs) +ssize_t send_discover(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid); if (cs->clientAddr) @@ -478,7 +480,7 @@ int send_discover(struct client_state_t *cs) return send_dhcp_raw(&packet); } -int send_selecting(struct client_state_t *cs) +ssize_t send_selecting(struct client_state_t *cs) { char clibuf[INET_ADDRSTRLEN]; struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); @@ -495,7 +497,7 @@ int send_selecting(struct client_state_t *cs) return send_dhcp_raw(&packet); } -int send_renew(struct client_state_t *cs) +ssize_t send_renew(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); packet.ciaddr = cs->clientAddr; @@ -507,7 +509,7 @@ int send_renew(struct client_state_t *cs) return send_dhcp_cooked(cs, &packet); } -int send_rebind(struct client_state_t *cs) +ssize_t send_rebind(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); packet.ciaddr = cs->clientAddr; @@ -520,7 +522,7 @@ int send_rebind(struct client_state_t *cs) return send_dhcp_raw(&packet); } -int send_decline(struct client_state_t *cs, uint32_t server) +ssize_t send_decline(struct client_state_t *cs, uint32_t server) { struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid); add_option_reqip(&packet, cs->clientAddr); @@ -529,7 +531,7 @@ int send_decline(struct client_state_t *cs, uint32_t server) return send_dhcp_raw(&packet); } -int send_release(struct client_state_t *cs) +ssize_t send_release(struct client_state_t *cs) { struct dhcpmsg packet = init_packet(DHCPRELEASE, nk_random_u32(&cs->rnd32_state)); diff --git a/ndhc/dhcp.h b/ndhc/dhcp.h index 8bbdbbf..b674386 100644 --- a/ndhc/dhcp.h +++ b/ndhc/dhcp.h @@ -85,11 +85,11 @@ void set_listen_raw(struct client_state_t *cs); void set_listen_cooked(struct client_state_t *cs); void set_listen_none(struct client_state_t *cs); void handle_packet(struct client_state_t *cs); -int send_discover(struct client_state_t *cs); -int send_selecting(struct client_state_t *cs); -int send_renew(struct client_state_t *cs); -int send_rebind(struct client_state_t *cs); -int send_decline(struct client_state_t *cs, uint32_t server); -int send_release(struct client_state_t *cs); +ssize_t send_discover(struct client_state_t *cs); +ssize_t send_selecting(struct client_state_t *cs); +ssize_t send_renew(struct client_state_t *cs); +ssize_t send_rebind(struct client_state_t *cs); +ssize_t send_decline(struct client_state_t *cs, uint32_t server); +ssize_t send_release(struct client_state_t *cs); #endif diff --git a/ndhc/duiaid.c b/ndhc/duiaid.c index 5d37090..4ecbb28 100644 --- a/ndhc/duiaid.c +++ b/ndhc/duiaid.c @@ -169,15 +169,16 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc) if (fd < 0) { iaid_len = generate_iaid(&cs->rnd32_state, iaid, sizeof iaid); fd = open_iaidfile_write(cc->arp, sizeof cc->arp); - int r = safe_write(fd, iaid, iaid_len); + ssize_t r = safe_write(fd, iaid, iaid_len); if (r < 0 || (size_t)r != iaid_len) suicide("%s: (%s) failed to write generated IAID.", cc->interface, __func__); } else { - iaid_len = safe_read(fd, iaid, sizeof iaid); - if (iaid_len < 0) + ssize_t r = safe_read(fd, iaid, sizeof iaid); + if (r < 0) suicide("%s: (%s) failed to read IAID from file", cc->interface, __func__); + iaid_len = (size_t)r; } close(fd); @@ -185,15 +186,16 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc) if (fd < 0) { duid_len = generate_duid(&cs->rnd32_state, duid, sizeof duid); fd = open_duidfile_write(); - int r = safe_write(fd, duid, duid_len); + ssize_t r = safe_write(fd, duid, duid_len); if (r < 0 || (size_t)r != duid_len) suicide("%s: (%s) failed to write generated DUID.", cc->interface, __func__); } else { - duid_len = safe_read(fd, duid, sizeof duid); - if (duid_len < 0) + ssize_t r = safe_read(fd, duid, sizeof duid); + if (r < 0) suicide("%s: (%s) failed to read DUID from file", cc->interface, __func__); + duid_len = (size_t)r; } close(fd); diff --git a/ndhc/ifchange.c b/ndhc/ifchange.c index ea6a8d9..53afd79 100644 --- a/ndhc/ifchange.c +++ b/ndhc/ifchange.c @@ -187,7 +187,7 @@ static int ifchd_cmd(char *b, size_t bl, uint8_t *od, ssize_t ol, uint8_t code) static void pipewrite(struct client_state_t *cs, const char *buf, size_t count) { cs->ifchWorking = 1; - int r = safe_write(pToIfchW, buf, count); + ssize_t r = safe_write(pToIfchW, buf, count); if (r < 0 || (size_t)r != count) { log_error("%s: (%s) write failed: %d", client_config.interface); return; diff --git a/ndhc/leasefile.c b/ndhc/leasefile.c index 45c44ad..a609e2e 100644 --- a/ndhc/leasefile.c +++ b/ndhc/leasefile.c @@ -70,7 +70,7 @@ void write_leasefile(struct in_addr ipnum) { char ip[INET_ADDRSTRLEN]; char out[INET_ADDRSTRLEN*2]; - int ret; + ssize_t ret; if (leasefilefd < 0) { log_error("%s: (%s) leasefile fd < 0; no leasefile will be written", client_config.interface, __func__); diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index feb5fe8..21b5c39 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -241,7 +241,7 @@ static void fail_if_state_dir_dne(void) static void handle_ifch_message(void) { char c; - int r = safe_read(pToNdhcR, &c, sizeof c); + ssize_t r = safe_read(pToNdhcR, &c, sizeof c); if (r == 0) { // Remote end hung up. exit(EXIT_SUCCESS);