summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Matthew Smith2022-07-25 19:06:41 +0100
committerGravatar Nikias Bassen2022-08-24 17:18:06 +0200
commit8163aa935e9dcbc15c6c091da7fc42f017fc7ce9 (patch)
treee00d083db5ca9a7ffa83ffc1cb656b3b6e49ad1c
parent2ca50ad4bc12ad9083897cab381fe22d3d91d49e (diff)
downloadlibplist-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.c12
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)
buff[7] = BPLIST_REAL | Log2(size);
if (size == sizeof(float)) {
float floatval = (float)val;
- *(uint32_t*)(buff+8) = float_bswap32(*(uint32_t*)&floatval);
+ uint32_t intval;
+ memcpy(&intval, &floatval, sizeof(float));
+ *(uint32_t*)(buff+8) = float_bswap32(intval);
} else {
- *(uint64_t*)(buff+8) = float_bswap64(*(uint64_t*)&val);
+ uint64_t intval;
+ memcpy(&intval, &val, sizeof(double));
+ *(uint64_t*)(buff+8) = float_bswap64(intval);
}
byte_array_append(bplist, buff+7, size+1);
}
static void write_date(bytearray_t * bplist, double val)
{
+ uint64_t intval;
+ memcpy(&intval, &val, sizeof(double));
uint8_t buff[16];
buff[7] = BPLIST_DATE | 3;
- *(uint64_t*)(buff+8) = float_bswap64(*(uint64_t*)&val);
+ *(uint64_t*)(buff+8) = float_bswap64(intval);
byte_array_append(bplist, buff+7, 9);
}