Make sure that all safe_* return values use ssize_t.

This commit is contained in:
Nicholas J. Kain 2014-04-06 06:24:13 -04:00
parent 745e9e8923
commit 3d76fbeedc
7 changed files with 38 additions and 34 deletions

View File

@ -709,7 +709,7 @@ static const arp_state_fn_t arp_states[] = {
void handle_arp_response(struct client_state_t *cs) void handle_arp_response(struct client_state_t *cs)
{ {
int r = 0; ssize_t r = 0;
if (arpreply_offset < sizeof arpreply) { if (arpreply_offset < sizeof arpreply) {
r = safe_read(cs->arpFd, (char *)&arpreply + arpreply_offset, r = safe_read(cs->arpFd, (char *)&arpreply + arpreply_offset,
sizeof 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; default: arp_reopen_fd(cs); break;
} }
} else } else
arpreply_offset += r; arpreply_offset += (size_t)r;
} }
if (r <= 0) { if (r <= 0) {

View File

@ -86,9 +86,10 @@ static int get_raw_listen_socket(struct client_state_t *cs)
} }
// Broadcast a DHCP message using a UDP socket. // 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(); int fd = get_udp_unicast_socket();
if (fd == -1) if (fd == -1)
goto out; 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 // Read a packet from a cooked socket. Returns -1 on fatal error, -2 on
// transient error. // 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); memset(packet, 0, sizeof *packet);
int bytes = safe_read(fd, (char *)packet, sizeof *packet); ssize_t bytes = safe_read(fd, (char *)packet, sizeof *packet);
if (bytes == -1) { if (bytes < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
return -2; return -2;
log_line("%s: Read on listen socket failed: %s", 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 // Read a packet from a raw socket. Returns -1 on fatal error, -2 on
// transient error. // 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; struct ip_udp_dhcp_packet packet;
memset(&packet, 0, sizeof packet); memset(&packet, 0, sizeof packet);
ssize_t inc = safe_read(cs->listenFd, (char *)&packet, sizeof packet); ssize_t inc = safe_read(cs->listenFd, (char *)&packet, sizeof packet);
if (inc == -1) { if (inc < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
return -2; return -2;
log_warning("%s: (%s) read error %s", client_config.interface, 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. // 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(); int fd = get_raw_broadcast_socket();
if (fd == -1) if (fd == -1)
return ret; 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); memcpy(da.sll_addr, "\xff\xff\xff\xff\xff\xff", 6);
ret = safe_sendto(fd, (const char *)&iudmsg, iud_len, 0, ret = safe_sendto(fd, (const char *)&iudmsg, iud_len, 0,
(struct sockaddr *)&da, sizeof da); (struct sockaddr *)&da, sizeof da);
if (ret == -1) if (ret < 0)
log_error("%s: (%s) sendto failed: %s", client_config.interface, log_error("%s: (%s) sendto failed: %s", client_config.interface,
__func__, strerror(errno)); __func__, strerror(errno));
close(fd); close(fd);
@ -429,7 +431,7 @@ void handle_packet(struct client_state_t *cs)
if (cs->listenMode == LM_NONE) if (cs->listenMode == LM_NONE)
return; 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); get_raw_packet(cs, &packet) : get_cooked_packet(&packet, cs->listenFd);
if (r < 0) { if (r < 0) {
// Transient issue handled by packet collection functions. // Transient issue handled by packet collection functions.
@ -465,7 +467,7 @@ static struct dhcpmsg init_packet(char type, uint32_t xid)
return packet; 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); struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid);
if (cs->clientAddr) if (cs->clientAddr)
@ -478,7 +480,7 @@ int send_discover(struct client_state_t *cs)
return send_dhcp_raw(&packet); 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]; char clibuf[INET_ADDRSTRLEN];
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid); 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); 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); struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
packet.ciaddr = cs->clientAddr; packet.ciaddr = cs->clientAddr;
@ -507,7 +509,7 @@ int send_renew(struct client_state_t *cs)
return send_dhcp_cooked(cs, &packet); 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); struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
packet.ciaddr = cs->clientAddr; packet.ciaddr = cs->clientAddr;
@ -520,7 +522,7 @@ int send_rebind(struct client_state_t *cs)
return send_dhcp_raw(&packet); 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); struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid);
add_option_reqip(&packet, cs->clientAddr); 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); 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, struct dhcpmsg packet = init_packet(DHCPRELEASE,
nk_random_u32(&cs->rnd32_state)); nk_random_u32(&cs->rnd32_state));

View File

@ -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_cooked(struct client_state_t *cs);
void set_listen_none(struct client_state_t *cs); void set_listen_none(struct client_state_t *cs);
void handle_packet(struct client_state_t *cs); void handle_packet(struct client_state_t *cs);
int send_discover(struct client_state_t *cs); ssize_t send_discover(struct client_state_t *cs);
int send_selecting(struct client_state_t *cs); ssize_t send_selecting(struct client_state_t *cs);
int send_renew(struct client_state_t *cs); ssize_t send_renew(struct client_state_t *cs);
int send_rebind(struct client_state_t *cs); ssize_t send_rebind(struct client_state_t *cs);
int send_decline(struct client_state_t *cs, uint32_t server); ssize_t send_decline(struct client_state_t *cs, uint32_t server);
int send_release(struct client_state_t *cs); ssize_t send_release(struct client_state_t *cs);
#endif #endif

View File

@ -169,15 +169,16 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc)
if (fd < 0) { if (fd < 0) {
iaid_len = generate_iaid(&cs->rnd32_state, iaid, sizeof iaid); iaid_len = generate_iaid(&cs->rnd32_state, iaid, sizeof iaid);
fd = open_iaidfile_write(cc->arp, sizeof cc->arp); 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) if (r < 0 || (size_t)r != iaid_len)
suicide("%s: (%s) failed to write generated IAID.", suicide("%s: (%s) failed to write generated IAID.",
cc->interface, __func__); cc->interface, __func__);
} else { } else {
iaid_len = safe_read(fd, iaid, sizeof iaid); ssize_t r = safe_read(fd, iaid, sizeof iaid);
if (iaid_len < 0) if (r < 0)
suicide("%s: (%s) failed to read IAID from file", suicide("%s: (%s) failed to read IAID from file",
cc->interface, __func__); cc->interface, __func__);
iaid_len = (size_t)r;
} }
close(fd); close(fd);
@ -185,15 +186,16 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc)
if (fd < 0) { if (fd < 0) {
duid_len = generate_duid(&cs->rnd32_state, duid, sizeof duid); duid_len = generate_duid(&cs->rnd32_state, duid, sizeof duid);
fd = open_duidfile_write(); 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) if (r < 0 || (size_t)r != duid_len)
suicide("%s: (%s) failed to write generated DUID.", suicide("%s: (%s) failed to write generated DUID.",
cc->interface, __func__); cc->interface, __func__);
} else { } else {
duid_len = safe_read(fd, duid, sizeof duid); ssize_t r = safe_read(fd, duid, sizeof duid);
if (duid_len < 0) if (r < 0)
suicide("%s: (%s) failed to read DUID from file", suicide("%s: (%s) failed to read DUID from file",
cc->interface, __func__); cc->interface, __func__);
duid_len = (size_t)r;
} }
close(fd); close(fd);

View File

@ -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) static void pipewrite(struct client_state_t *cs, const char *buf, size_t count)
{ {
cs->ifchWorking = 1; 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) { if (r < 0 || (size_t)r != count) {
log_error("%s: (%s) write failed: %d", client_config.interface); log_error("%s: (%s) write failed: %d", client_config.interface);
return; return;

View File

@ -70,7 +70,7 @@ void write_leasefile(struct in_addr ipnum)
{ {
char ip[INET_ADDRSTRLEN]; char ip[INET_ADDRSTRLEN];
char out[INET_ADDRSTRLEN*2]; char out[INET_ADDRSTRLEN*2];
int ret; ssize_t ret;
if (leasefilefd < 0) { if (leasefilefd < 0) {
log_error("%s: (%s) leasefile fd < 0; no leasefile will be written", log_error("%s: (%s) leasefile fd < 0; no leasefile will be written",
client_config.interface, __func__); client_config.interface, __func__);

View File

@ -241,7 +241,7 @@ static void fail_if_state_dir_dne(void)
static void handle_ifch_message(void) static void handle_ifch_message(void)
{ {
char c; char c;
int r = safe_read(pToNdhcR, &c, sizeof c); ssize_t r = safe_read(pToNdhcR, &c, sizeof c);
if (r == 0) { if (r == 0) {
// Remote end hung up. // Remote end hung up.
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);