diff options
| author | 2022-07-25 19:06:41 +0100 | |
|---|---|---|
| committer | 2022-08-24 17:18:06 +0200 | |
| commit | 8163aa935e9dcbc15c6c091da7fc42f017fc7ce9 (patch) | |
| tree | e00d083db5ca9a7ffa83ffc1cb656b3b6e49ad1c | |
| parent | 2ca50ad4bc12ad9083897cab381fe22d3d91d49e (diff) | |
| download | libplist-8163aa935e9dcbc15c6c091da7fc42f017fc7ce9.tar.gz libplist-8163aa935e9dcbc15c6c091da7fc42f017fc7ce9.tar.bz2 | |
bplist: Fix strict aliasing violations
Casting a float pointer to an int pointer is a strict aliasing
violation (-Wstrict-aliasing) and is undefined behaviour (although, it
did not seem to cause any real issues).
An optimising compiler should elide the memcopies added by this commit.
| -rw-r--r-- | src/bplist.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/bplist.c b/src/bplist.c index 57ec151..c8f5ebc 100644 --- a/src/bplist.c +++ b/src/bplist.c | |||
| @@ -998,18 +998,24 @@ static void write_real(bytearray_t * bplist, double val) | |||
| 998 | buff[7] = BPLIST_REAL | Log2(size); | 998 | buff[7] = BPLIST_REAL | Log2(size); |
| 999 | if (size == sizeof(float)) { | 999 | if (size == sizeof(float)) { |
| 1000 | float floatval = (float)val; | 1000 | float floatval = (float)val; |
| 1001 | *(uint32_t*)(buff+8) = float_bswap32(*(uint32_t*)&floatval); | 1001 | uint32_t intval; |
| 1002 | memcpy(&intval, &floatval, sizeof(float)); | ||
| 1003 | *(uint32_t*)(buff+8) = float_bswap32(intval); | ||
| 1002 | } else { | 1004 | } else { |
| 1003 | *(uint64_t*)(buff+8) = float_bswap64(*(uint64_t*)&val); | 1005 | uint64_t intval; |
| 1006 | memcpy(&intval, &val, sizeof(double)); | ||
| 1007 | *(uint64_t*)(buff+8) = float_bswap64(intval); | ||
| 1004 | } | 1008 | } |
| 1005 | byte_array_append(bplist, buff+7, size+1); | 1009 | byte_array_append(bplist, buff+7, size+1); |
| 1006 | } | 1010 | } |
| 1007 | 1011 | ||
| 1008 | static void write_date(bytearray_t * bplist, double val) | 1012 | static void write_date(bytearray_t * bplist, double val) |
| 1009 | { | 1013 | { |
| 1014 | uint64_t intval; | ||
| 1015 | memcpy(&intval, &val, sizeof(double)); | ||
| 1010 | uint8_t buff[16]; | 1016 | uint8_t buff[16]; |
| 1011 | buff[7] = BPLIST_DATE | 3; | 1017 | buff[7] = BPLIST_DATE | 3; |
| 1012 | *(uint64_t*)(buff+8) = float_bswap64(*(uint64_t*)&val); | 1018 | *(uint64_t*)(buff+8) = float_bswap64(intval); |
| 1013 | byte_array_append(bplist, buff+7, 9); | 1019 | byte_array_append(bplist, buff+7, 9); |
| 1014 | } | 1020 | } |
| 1015 | 1021 | ||
