From 6fbd4b99bc50cf9cd54a1b6e2b2c96c4ba4a9b19 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 12 Jun 2011 22:37:24 +0100 Subject: [PATCH] integer arithmetic types: make get_uint32, get_uint16 return the correct type Previously get_uint32 and get_uint16 would return whatever the usual arithmetic conversions produced. (The previous code was not in fact even guaranteed to work properly on a machine with 16-bit ints.) Now we cast the individual bytes before shifting. Signed-off-by: Ian Jackson --- unaligned.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/unaligned.h b/unaligned.h index 45b0d3c..a15043e 100644 --- a/unaligned.h +++ b/unaligned.h @@ -1,6 +1,8 @@ #ifndef unaligned_h #define unaligned_h +#include + /* Parts of the secnet key-exchange protocol require access to unaligned big-endian quantities in buffers. These macros provide convenient access, even on architectures that don't support unaligned @@ -11,9 +13,11 @@ #define put_uint16(a,v) do {(a)[0]=((v)&0xff00)>>8; (a)[1]=(v)&0xff;} while(0) -#define get_uint32(a) (((a)[0]<<24)|((a)[1]<<16)|((a)[2])<<8|(a)[3]) +#define get_uint32(a) \ + (((uint32_t)(a)[0]<<24) | ((uint32_t)(a)[1]<<16) | \ + ((uint32_t)(a)[2]<<8) | (uint32_t)(a)[3]) -#define get_uint16(a) (((a)[0]<<8)|(a)[1]) +#define get_uint16(a) (((uint16_t)(a)[0]<<8)|(uint16_t)(a)[1]) #define buf_append_uint32(buf,v) do { uint8_t *c=buf_append((buf),4); \ put_uint32(c,(v)); } while(0) -- 2.30.2