From e6902ee41fbd05a7af188e87dab7e97b39f03a83 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sat, 29 Sep 2018 12:58:51 -0700 Subject: [PATCH] avoid UB in decodeAsIPv6Address() Order of evaluation within an expression is undefined behavior. --- src/utils/inetAddressUtil.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/utils/inetAddressUtil.cpp b/src/utils/inetAddressUtil.cpp index 378cf27..873c819 100644 --- a/src/utils/inetAddressUtil.cpp +++ b/src/utils/inetAddressUtil.cpp @@ -57,11 +57,13 @@ bool decodeAsIPv6Address(ByteBuffer* buffer, osiSockAddr* address) { // allow all zeros address //if (ffff != (int16)0xFFFF) return false; - uint32_t ipv4Addr = - ((uint32_t)(buffer->getByte()&0xFF))<<24 | - ((uint32_t)(buffer->getByte()&0xFF))<<16 | - ((uint32_t)(buffer->getByte()&0xFF))<<8 | - ((uint32_t)(buffer->getByte()&0xFF)); + uint32 ipv4Addr = uint8(buffer->getByte()); + ipv4Addr <<= 8; + ipv4Addr |= uint8(buffer->getByte()); + ipv4Addr <<= 8; + ipv4Addr |= uint8(buffer->getByte()); + ipv4Addr <<= 8; + ipv4Addr |= uint8(buffer->getByte()); if (ffff != (int16)0xFFFF && ipv4Addr != (uint32_t)0) return false;