diff options
author | John Snow <jsnow@redhat.com> | 2015-11-02 18:32:06 -0500 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2015-11-11 16:25:47 +0100 |
commit | 95334230637cef9fbd199bb79a56271ec73d4732 (patch) | |
tree | 7bdb909de8cdd28e9dca53221c94d3a0889a0543 /block/qcow2-refcount.c | |
parent | bcdce5a73cb28f32b3ca3a51e8fa89879685e015 (diff) | |
download | qemu-95334230637cef9fbd199bb79a56271ec73d4732.tar.gz qemu-95334230637cef9fbd199bb79a56271ec73d4732.tar.bz2 qemu-95334230637cef9fbd199bb79a56271ec73d4732.zip |
qcow2: avoid misaligned 64bit bswap
If we create a buffer directly on the stack by using 12 bytes, there's
no guarantee the 64bit value we want to swap will be aligned, which
could cause errors with undefined behavior.
Spotted with clang -fsanitize=undefined and observed in iotests 15, 26,
44, 115 and 121.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'block/qcow2-refcount.c')
-rw-r--r-- | block/qcow2-refcount.c | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 4b81c8db61..6e0e5bd9ae 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -560,13 +560,16 @@ static int alloc_refcount_block(BlockDriverState *bs, } /* Hook up the new refcount table in the qcow2 header */ - uint8_t data[12]; - cpu_to_be64w((uint64_t*)data, table_offset); - cpu_to_be32w((uint32_t*)(data + 8), table_clusters); + struct QEMU_PACKED { + uint64_t d64; + uint32_t d32; + } data; + cpu_to_be64w(&data.d64, table_offset); + cpu_to_be32w(&data.d32, table_clusters); BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE); ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, refcount_table_offset), - data, sizeof(data)); + &data, sizeof(data)); if (ret < 0) { goto fail_table; } |