Skip to content

Commit 329eaaa

Browse files
committed
d4util.h: make swapinlineXX more robust against type punning
Since the type of ip is not known in a macro definition, use memcpy() to copy from and to memory. Signed-off-by: Egbert Eich <eich@suse.com>
1 parent 9c319b1 commit 329eaaa

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

libdap4/d4util.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,39 @@ typedef struct D4blob {d4size_t size; void* memory;} D4blob;
2525
/* signature: void swapinline16(void* ip) */
2626
#define swapinline16(ip) \
2727
{ \
28-
union {char b[2]; unsigned short i;} u; \
28+
char b[2]; \
2929
char* src = (char*)(ip); \
30-
u.b[0] = src[1]; \
31-
u.b[1] = src[0]; \
32-
*((unsigned short*)ip) = u.i; \
30+
b[0] = src[1]; \
31+
b[1] = src[0]; \
32+
memcpy(ip, b, 2); \
3333
}
3434

3535
/* signature: void swapinline32(void* ip) */
3636
#define swapinline32(ip) \
3737
{ \
38-
union {char b[4]; unsigned int i;} u; \
38+
char b[4]; \
3939
char* src = (char*)(ip); \
40-
u.b[0] = src[3]; \
41-
u.b[1] = src[2]; \
42-
u.b[2] = src[1]; \
43-
u.b[3] = src[0]; \
44-
*((unsigned int*)ip) = u.i; \
40+
b[0] = src[3]; \
41+
b[1] = src[2]; \
42+
b[2] = src[1]; \
43+
b[3] = src[0]; \
44+
memcpy(ip, b, 4); \
4545
}
4646

4747
/* signature: void swapinline64(void* ip) */
4848
#define swapinline64(ip) \
4949
{ \
50-
union {char b[8]; unsigned long long i;} u; \
50+
char b[8]; \
5151
char* src = (char*)(ip); \
52-
u.b[0] = src[7]; \
53-
u.b[1] = src[6]; \
54-
u.b[2] = src[5]; \
55-
u.b[3] = src[4]; \
56-
u.b[4] = src[3]; \
57-
u.b[5] = src[2]; \
58-
u.b[6] = src[1]; \
59-
u.b[7] = src[0]; \
60-
*((unsigned long long*)ip) = u.i; \
52+
b[0] = src[7]; \
53+
b[1] = src[6]; \
54+
b[2] = src[5]; \
55+
b[3] = src[4]; \
56+
b[4] = src[3]; \
57+
b[5] = src[2]; \
58+
b[6] = src[1]; \
59+
b[7] = src[0]; \
60+
memcpy(ip, b, 8); \
6161
}
6262

6363
/***************************************************/

0 commit comments

Comments
 (0)