From f2a5aa286619cdfead163900f4667d0b7ae04370 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Mon, 12 Dec 2016 10:07:00 +0100 Subject: [PATCH] Fix bug #49827: wrong cast to size_t on 16-bit x86 architecture I hope I caught all of them. TODO: Same for casts to get rid of alignment warnings, these are also casts via size_t --- src/api/sockets.c | 2 +- src/apps/lwiperf/lwiperf.c | 2 +- src/core/def.c | 4 ++-- src/core/pbuf.c | 2 +- src/core/timeouts.c | 2 +- src/include/lwip/arch.h | 5 +++++ 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/api/sockets.c b/src/api/sockets.c index 9cffd519..ae2c7553 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -1279,7 +1279,7 @@ lwip_writev(int s, const struct iovec *iov, int iovcnt) msg.msg_namelen = 0; /* Hack: we have to cast via number to cast from 'const' pointer to non-const. Blame the opengroup standard for this inconsistency. */ - msg.msg_iov = (struct iovec *)(size_t)iov; + msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); msg.msg_iovlen = iovcnt; msg.msg_control = NULL; msg.msg_controllen = 0; diff --git a/src/apps/lwiperf/lwiperf.c b/src/apps/lwiperf/lwiperf.c index 079dd6b1..54bf2bca 100644 --- a/src/apps/lwiperf/lwiperf.c +++ b/src/apps/lwiperf/lwiperf.c @@ -294,7 +294,7 @@ lwiperf_tcp_client_send_more(lwiperf_state_tcp_t* conn) } else { /* transmit data */ /* @todo: every x bytes, transmit the settings again */ - txptr = (void*)(size_t)&lwiperf_txbuf_const[conn->bytes_transferred % 10]; + txptr = LWIP_CONST_CAST(void*, &lwiperf_txbuf_const[conn->bytes_transferred % 10]); txlen_max = TCP_MSS; if (conn->bytes_transferred == 48) { /* @todo: fix this for intermediate settings, too */ txlen_max = TCP_MSS - 24; diff --git a/src/core/def.c b/src/core/def.c index 99f3d62e..bdece2f9 100644 --- a/src/core/def.c +++ b/src/core/def.c @@ -103,11 +103,11 @@ lwip_strnstr(const char* buffer, const char* token, size_t n) const char* p; int tokenlen = (int)strlen(token); if (tokenlen == 0) { - return (char *)(size_t)buffer; + return LWIP_CONST_CAST(char *, buffer); } for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) { if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) { - return (char *)(size_t)p; + return LWIP_CONST_CAST(char *, p); } } return NULL; diff --git a/src/core/pbuf.c b/src/core/pbuf.c index 887dc8aa..5a319421 100644 --- a/src/core/pbuf.c +++ b/src/core/pbuf.c @@ -1119,7 +1119,7 @@ pbuf_skip_const(const struct pbuf* in, u16_t in_offset, u16_t* out_offset) struct pbuf* pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset) { - return (struct pbuf*)(size_t)pbuf_skip_const(in, in_offset, out_offset); + return LWIP_CONST_CAST(struct pbuf*, pbuf_skip_const(in, in_offset, out_offset)); } /** diff --git a/src/core/timeouts.c b/src/core/timeouts.c index e2dc0fc7..227d71fc 100644 --- a/src/core/timeouts.c +++ b/src/core/timeouts.c @@ -179,7 +179,7 @@ void sys_timeouts_init(void) for (i = 1; i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++) { /* we have to cast via size_t to get rid of const warning (this is OK as cyclic_timer() casts back to const* */ - sys_timeout(lwip_cyclic_timers[i].interval_ms, cyclic_timer, (void*)(size_t)&lwip_cyclic_timers[i]); + sys_timeout(lwip_cyclic_timers[i].interval_ms, cyclic_timer, LWIP_CONST_CAST(void*, &lwip_cyclic_timers[i])); } /* Initialise timestamp for sys_check_timeouts */ diff --git a/src/include/lwip/arch.h b/src/include/lwip/arch.h index a9c99988..c1180147 100644 --- a/src/include/lwip/arch.h +++ b/src/include/lwip/arch.h @@ -133,6 +133,11 @@ typedef uintptr_t mem_ptr_t; #endif #endif +/** C++ const_cast(val) equivalent to remove constness from a value */ +#ifndef LWIP_CONST_CAST +#define LWIP_CONST_CAST(target_type, val) ((target_type)((ptrdiff_t)val)) +#endif + /** Allocates a memory buffer of specified size that is of sufficient size to align * its start address using LWIP_MEM_ALIGN. * You can declare your own version here e.g. to enforce alignment without adding