Commit be567b3a authored by Nikias Bassen's avatar Nikias Bassen

bplist: Prevent store to misaligned address when writing real/date nodes

ASAN reported possible undefined behaviour when writing float/double
values to misaligned addresses.
parent ebd80838
......@@ -988,23 +988,23 @@ static void write_uint(bytearray_t * bplist, uint64_t val)
static void write_real(bytearray_t * bplist, double val)
{
int size = get_real_bytes(val); //cheat to know used space
uint8_t buff[9];
buff[0] = BPLIST_REAL | Log2(size);
uint8_t buff[16];
buff[7] = BPLIST_REAL | Log2(size);
if (size == sizeof(float)) {
float floatval = (float)val;
*(uint32_t*)(buff+1) = float_bswap32(*(uint32_t*)&floatval);
*(uint32_t*)(buff+8) = float_bswap32(*(uint32_t*)&floatval);
} else {
*(uint64_t*)(buff+1) = float_bswap64(*(uint64_t*)&val);
*(uint64_t*)(buff+8) = float_bswap64(*(uint64_t*)&val);
}
byte_array_append(bplist, buff, size+1);
byte_array_append(bplist, buff+7, size+1);
}
static void write_date(bytearray_t * bplist, double val)
{
uint8_t buff[9];
buff[0] = BPLIST_DATE | 3;
*(uint64_t*)(buff+1) = float_bswap64(*(uint64_t*)&val);
byte_array_append(bplist, buff, sizeof(buff));
uint8_t buff[16];
buff[7] = BPLIST_DATE | 3;
*(uint64_t*)(buff+8) = float_bswap64(*(uint64_t*)&val);
byte_array_append(bplist, buff+7, 9);
}
static void write_raw_data(bytearray_t * bplist, uint8_t mark, uint8_t * val, uint64_t size)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment