chiark / gitweb /
integer arithmetic types: make get_uint32, get_uint16 return the correct type
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jun 2011 21:37:24 +0000 (22:37 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 26 Jun 2011 11:07:26 +0000 (12:07 +0100)
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 <ijackson@chiark.greenend.org.uk>
unaligned.h

index 45b0d3c079319e5e31b7c5c5da43b98a7ba9d1cf..a15043e56597eb4ad85ffb01391006c0ffaf5902 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef unaligned_h
 #define unaligned_h
 
+#include <stdint.h>
+
 /* 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
 
 #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)