vdi: Fix warning from clang

ccc-analyzer reports these warnings:

block/vdi.c:704:13: warning: Dereference of null pointer
            bmap[i] = VDI_UNALLOCATED;
            ^
block/vdi.c:702:13: warning: Dereference of null pointer
            bmap[i] = i;
            ^

Moving some code into the if block fixes this.
It also avoids calling function write with 0 bytes of data.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Stefan Weil 2012-08-17 15:23:24 +02:00 committed by Kevin Wolf
parent 45724d6d02
commit 514f21a5d4

View File

@ -628,7 +628,6 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options)
VdiHeader header; VdiHeader header;
size_t i; size_t i;
size_t bmap_size; size_t bmap_size;
uint32_t *bmap;
logout("\n"); logout("\n");
@ -693,21 +692,21 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options)
result = -errno; result = -errno;
} }
bmap = NULL;
if (bmap_size > 0) { if (bmap_size > 0) {
bmap = (uint32_t *)g_malloc0(bmap_size); uint32_t *bmap = g_malloc0(bmap_size);
} for (i = 0; i < blocks; i++) {
for (i = 0; i < blocks; i++) { if (image_type == VDI_TYPE_STATIC) {
if (image_type == VDI_TYPE_STATIC) { bmap[i] = i;
bmap[i] = i; } else {
} else { bmap[i] = VDI_UNALLOCATED;
bmap[i] = VDI_UNALLOCATED; }
} }
if (write(fd, bmap, bmap_size) < 0) {
result = -errno;
}
g_free(bmap);
} }
if (write(fd, bmap, bmap_size) < 0) {
result = -errno;
}
g_free(bmap);
if (image_type == VDI_TYPE_STATIC) { if (image_type == VDI_TYPE_STATIC) {
if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) { if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
result = -errno; result = -errno;