Commit 63c75dcd669d011f438421980b4379827da4bb1c
1 parent
4c621805
Avoid calling qemu_mallocz with zero size
Currently qemu_mallocz calls malloc and handling of zero by malloc is implementation defined behaviour: http://www.opengroup.org/onlinepubs/7990989775/xsh/malloc.html malloc(0) on AIX returns NULL[1] and qcow2 images without snapshots are thus unusable [1] Unless special Linux compatibility define is used when compiling git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6359 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
10 additions
and
2 deletions
block-qcow2.c
@@ -1809,6 +1809,12 @@ static int qcow_read_snapshots(BlockDriverState *bs) | @@ -1809,6 +1809,12 @@ static int qcow_read_snapshots(BlockDriverState *bs) | ||
1809 | int64_t offset; | 1809 | int64_t offset; |
1810 | uint32_t extra_data_size; | 1810 | uint32_t extra_data_size; |
1811 | 1811 | ||
1812 | + if (!s->nb_snapshots) { | ||
1813 | + s->snapshots = NULL; | ||
1814 | + s->snapshots_size = 0; | ||
1815 | + return 0; | ||
1816 | + } | ||
1817 | + | ||
1812 | offset = s->snapshots_offset; | 1818 | offset = s->snapshots_offset; |
1813 | s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot)); | 1819 | s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot)); |
1814 | if (!s->snapshots) | 1820 | if (!s->snapshots) |
@@ -2023,8 +2029,10 @@ static int qcow_snapshot_create(BlockDriverState *bs, | @@ -2023,8 +2029,10 @@ static int qcow_snapshot_create(BlockDriverState *bs, | ||
2023 | snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot)); | 2029 | snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot)); |
2024 | if (!snapshots1) | 2030 | if (!snapshots1) |
2025 | goto fail; | 2031 | goto fail; |
2026 | - memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot)); | ||
2027 | - qemu_free(s->snapshots); | 2032 | + if (s->snapshots) { |
2033 | + memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot)); | ||
2034 | + qemu_free(s->snapshots); | ||
2035 | + } | ||
2028 | s->snapshots = snapshots1; | 2036 | s->snapshots = snapshots1; |
2029 | s->snapshots[s->nb_snapshots++] = *sn; | 2037 | s->snapshots[s->nb_snapshots++] = *sn; |
2030 | 2038 |