Skip to content

Commit 962dd8b

Browse files
committed
Silence GCC warnings in dhcp.c
The first was a potential out of range index read in dhcp_handle_ack(). The (n < DNS_MAX_SERVERS) check should occur first. There is also a documented lwIP bug for this issue here: http://savannah.nongnu.org/bugs/?36170 In dhcp_bind() there is no need to perform the NULL check in ip_addr_isany() for &gw_addr. Just check (gw_addr.addr == IPADDR_ANY) instead. I refactored the chaddr[] copy in dhcp_create_msg() to first copy all of the valid bytes in hwaddr and then pad the rest of the bytes with 0. Before it used to check on every destination byte if it should copy or pad. GCC originally complained about an index out of range read from the hwaddr[] array even though it was protected by a conditional operator. The refactor makes the intent a bit clearer and saves the extra comparison per loop iteration. It also stops GCC from complaining :)
1 parent 1cf5243 commit 962dd8b

File tree

1 file changed

+9
-5
lines changed
  • libraries/net/lwip/lwip/core

1 file changed

+9
-5
lines changed

libraries/net/lwip/lwip/core/dhcp.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ dhcp_handle_ack(struct netif *netif)
564564
#if LWIP_DNS
565565
/* DNS servers */
566566
n = 0;
567-
while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n < DNS_MAX_SERVERS)) {
567+
while((n < DNS_MAX_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)) {
568568
ip_addr_t dns_addr;
569569
ip4_addr_set_u32(&dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
570570
dns_setserver(n, &dns_addr);
@@ -975,7 +975,7 @@ dhcp_bind(struct netif *netif)
975975

976976
ip_addr_copy(gw_addr, dhcp->offered_gw_addr);
977977
/* gateway address not given? */
978-
if (ip_addr_isany(&gw_addr)) {
978+
if (gw_addr.addr == IPADDR_ANY) {
979979
/* copy network address */
980980
ip_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
981981
/* use first host address on network as gateway */
@@ -1678,9 +1678,13 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type)
16781678
ip_addr_set_zero(&dhcp->msg_out->yiaddr);
16791679
ip_addr_set_zero(&dhcp->msg_out->siaddr);
16801680
ip_addr_set_zero(&dhcp->msg_out->giaddr);
1681-
for (i = 0; i < DHCP_CHADDR_LEN; i++) {
1682-
/* copy netif hardware address, pad with zeroes */
1683-
dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len) ? netif->hwaddr[i] : 0/* pad byte*/;
1681+
for (i = 0; i < netif->hwaddr_len; i++) {
1682+
/* copy netif hardware address */
1683+
dhcp->msg_out->chaddr[i] = netif->hwaddr[i];
1684+
}
1685+
for ( ; i < DHCP_CHADDR_LEN; i++) {
1686+
/* ... pad rest with zeroes */
1687+
dhcp->msg_out->chaddr[i] = 0;
16841688
}
16851689
for (i = 0; i < DHCP_SNAME_LEN; i++) {
16861690
dhcp->msg_out->sname[i] = 0;

0 commit comments

Comments
 (0)