diff --git a/src/core/def.c b/src/core/def.c index 35f142c7..d3004d4d 100644 --- a/src/core/def.c +++ b/src/core/def.c @@ -75,7 +75,7 @@ u16_t lwip_htons(u16_t n) { - return (u16_t)PP_HTONS(n); + return PP_HTONS(n); } #endif /* lwip_htons */ @@ -89,7 +89,7 @@ lwip_htons(u16_t n) u32_t lwip_htonl(u32_t n) { - return (u32_t)PP_HTONL(n); + return PP_HTONL(n); } #endif /* lwip_htonl */ diff --git a/src/core/ipv4/icmp.c b/src/core/ipv4/icmp.c index 5ee24eed..c866663b 100644 --- a/src/core/ipv4/icmp.c +++ b/src/core/ipv4/icmp.c @@ -92,7 +92,7 @@ icmp_input(struct pbuf *p, struct netif *inp) MIB2_STATS_INC(mib2.icmpinmsgs); iphdr_in = ip4_current_header(); - hlen = IPH_HL(iphdr_in) * 4; + hlen = IPH_HL_BYTES(iphdr_in); if (hlen < IP_HLEN) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short IP header (%"S16_F" bytes) received\n", hlen)); goto lenerr; @@ -157,8 +157,13 @@ icmp_input(struct pbuf *p, struct netif *inp) * allocate a new one and copy p into it */ struct pbuf *r; + u16_t alloc_len = (u16_t)(p->tot_len + hlen); + if (alloc_len < p->tot_len) { + LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed (tot_len overflow)\n")); + goto icmperr; + } /* allocate new packet buffer with space for link headers */ - r = pbuf_alloc(PBUF_LINK, p->tot_len + hlen, PBUF_RAM); + r = pbuf_alloc(PBUF_LINK, alloc_len, PBUF_RAM); if (r == NULL) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: allocating new pbuf failed\n")); goto icmperr; @@ -188,7 +193,7 @@ icmp_input(struct pbuf *p, struct netif *inp) p = r; } else { /* restore p->payload to point to icmp header (cannot fail) */ - if (pbuf_header(p, -(s16_t)(hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) { + if (pbuf_header(p, (s16_t)-(s16_t)(hlen + PBUF_LINK_HLEN + PBUF_LINK_ENCAPSULATION_HLEN))) { LWIP_ASSERT("icmp_input: restoring original p->payload failed\n", 0); goto icmperr; } @@ -210,9 +215,9 @@ icmp_input(struct pbuf *p, struct netif *inp) IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_GEN_ICMP) { /* adjust the checksum */ if (iecho->chksum > PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { - iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1; + iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS((u16_t)(ICMP_ECHO << 8)) + 1); } else { - iecho->chksum += PP_HTONS(ICMP_ECHO << 8); + iecho->chksum = (u16_t)(iecho->chksum + PP_HTONS(ICMP_ECHO << 8)); } } #if LWIP_CHECKSUM_CTRL_PER_NETIF diff --git a/src/core/ipv4/igmp.c b/src/core/ipv4/igmp.c index ee98b062..a77a3c32 100644 --- a/src/core/ipv4/igmp.c +++ b/src/core/ipv4/igmp.c @@ -684,7 +684,7 @@ static void igmp_start_timer(struct igmp_group *group, u8_t max_time) { #ifdef LWIP_RAND - group->timer = max_time > 2 ? (LWIP_RAND() % max_time) : 1; + group->timer = (u16_t)(max_time > 2 ? (LWIP_RAND() % max_time) : 1); #else /* LWIP_RAND */ /* ATTENTION: use this only if absolutely necessary! */ group->timer = max_time / 2; diff --git a/src/core/netif.c b/src/core/netif.c index e642a950..19348ec6 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -1353,7 +1353,7 @@ netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit) ((u32_t)(netif->hwaddr[1]) << 16) | ((u32_t)(netif->hwaddr[2]) << 8) | (0xff)); - ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((0xfeul << 24) | + ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((u32_t)(0xfeul << 24) | ((u32_t)(netif->hwaddr[3]) << 16) | ((u32_t)(netif->hwaddr[4]) << 8) | (netif->hwaddr[5])); diff --git a/src/include/lwip/def.h b/src/include/lwip/def.h index 82a9d896..f836ba90 100644 --- a/src/include/lwip/def.h +++ b/src/include/lwip/def.h @@ -94,12 +94,12 @@ u32_t lwip_htonl(u32_t x); /* These macros should be calculated by the preprocessor and are used with compile-time constants only (so that there is no little-endian overhead at runtime). */ -#define PP_HTONS(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8)) +#define PP_HTONS(x) ((u16_t)((((x) & (u16_t)0x00ffU) << 8) | (((x) & (u16_t)0xff00U) >> 8))) #define PP_NTOHS(x) PP_HTONS(x) -#define PP_HTONL(x) ((((x) & 0x000000ffUL) << 24) | \ - (((x) & 0x0000ff00UL) << 8) | \ - (((x) & 0x00ff0000UL) >> 8) | \ - (((x) & 0xff000000UL) >> 24)) +#define PP_HTONL(x) ((((x) & (u32_t)0x000000ffUL) << 24) | \ + (((x) & (u32_t)0x0000ff00UL) << 8) | \ + (((x) & (u32_t)0x00ff0000UL) >> 8) | \ + (((x) & (u32_t)0xff000000UL) >> 24)) #define PP_NTOHL(x) PP_HTONL(x) #endif /* BYTE_ORDER == BIG_ENDIAN */ diff --git a/src/include/lwip/prot/ip4.h b/src/include/lwip/prot/ip4.h index bd442c68..7eb4201d 100644 --- a/src/include/lwip/prot/ip4.h +++ b/src/include/lwip/prot/ip4.h @@ -101,6 +101,7 @@ PACK_STRUCT_END /* Macros to get struct ip_hdr fields: */ #define IPH_V(hdr) ((hdr)->_v_hl >> 4) #define IPH_HL(hdr) ((hdr)->_v_hl & 0x0f) +#define IPH_HL_BYTES(hdr) ((u8_t)(IPH_HL(hdr) * 4)) #define IPH_TOS(hdr) ((hdr)->_tos) #define IPH_LEN(hdr) ((hdr)->_len) #define IPH_ID(hdr) ((hdr)->_id)