block: Simplify a few g_try_malloc() error checks
PATCH 1/1 block: Simplify a few g_try_malloc() error checks
Unlike malloc(), g_try_malloc() & friends return a null pointer only on failure, never for a zero size. Simplify tests for failure accordingly. This helps Coverity see returned null pointers can't be dereferenced. Also makes the code easier to read.
Tracked down with this Coccinelle semantic patch:
@@
type T, TT;
identifier LHS;
expression SZ, SZ2, N, E;
@@
(
T *LHS = g_try_malloc(SZ);
|
T *LHS = g_try_malloc0(SZ);
|
T *LHS = g_try_new(TT, N);
|
T *LHS = g_try_new0(TT, N);
|
T *LHS = g_try_realloc(E, SZ);
)
...
- if (SZ2 && LHS == NULL)
+ if (!LHS)
{
...
}
@@
type TT;
expression LHS, SZ, SZ2, N, E;
@@
(
LHS = g_try_malloc(SZ);
|
LHS = g_try_malloc0(SZ);
|
LHS = g_try_new(TT, N);
|
LHS = g_try_new0(TT, N)
|
LHS = g_try_realloc(E, SZ);
)
...
- if (SZ2 && LHS == NULL)
+ if (!LHS)
{
...
}--- block/bochs.c | 2 +- block/curl.c | 2 +- block/nfs.c | 2 +- block/parallels.c | 2 +- block/qcow2-refcount.c | 6 +++--- block/qcow2-snapshot.c | 4 ++-- block/qed-check.c | 2 +- block/vdi.c | 2 +- block/vhdx.c | 2 +- block/vmdk.c | 4 ++-- 10 files changed, 14 insertions(+), 14 deletions(-)
block/bochs.c +1 -1
@@ -132,7 +132,7 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags, } s->catalog_bitmap =g_try_new (uint32_t, s->catalog_size); -if (s->catalog_size && s->catalog_bitmap ==NULL ) { +if (!s->catalog_bitmap) {error_setg (errp,"Could not allocate memory for catalog" );return -ENOMEM ; }
block/curl.c +1 -1
@@ -666,7 +666,7 @@ static void curl_readv_bh_cb(void *p) state->buf_len = acb->end + s->readahead_size; end =MIN (start + state->buf_len, s->len) -1 ; state->orig_buf =g_try_malloc (state->buf_len); -if (state->buf_len && state->orig_buf ==NULL ) { +if (!state->orig_buf) {curl_clean_state (state); acb->common.cb (acb->common.opaque, -ENOMEM );qemu_aio_unref (acb);
block/nfs.c +1 -1
@@ -173,7 +173,7 @@ static int coroutine_fn nfs_co_writev(BlockDriverState *bs,nfs_co_init_task (client, &task); buf =g_try_malloc (nb_sectors *BDRV_SECTOR_SIZE ); -if (nb_sectors && buf ==NULL ) { +if (!buf) {return -ENOMEM ; }
block/parallels.c +1 -1
@@ -122,7 +122,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } s->catalog_bitmap =g_try_new (uint32_t, s->catalog_size); -if (s->catalog_size && s->catalog_bitmap ==NULL ) { +if (!s->catalog_bitmap) { ret = -ENOMEM ; goto fail; }
block/qcow2-refcount.c +3 -3
@@ -893,7 +893,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,* when changing this! */ if (l1_table_offset != s->l1_table_offset) { l1_table =g_try_malloc0 (align_offset (l1_size2,512 )); -if (l1_size2 && l1_table ==NULL ) { +if (!l1_table) { ret = -ENOMEM ; goto fail; }
@@ -1572,7 +1572,7 @@ static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,if (!*refcount_table) { *refcount_table =g_try_new0 (uint16_t, *nb_clusters); -if (*nb_clusters && *refcount_table ==NULL ) { +if (!*refcount_table) { res->check_errors++;return -ENOMEM ; }
@@ -2183,7 +2183,7 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,uint64_t *l1 =g_try_malloc (l1_sz2);int ret; -if (l1_sz2 && l1 ==NULL ) { +if (!l1) {return -ENOMEM ; }
block/qcow2-snapshot.c +2 -2
@@ -382,7 +382,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) sn->l1_size = s->l1_size; l1_table =g_try_new (uint64_t, s->l1_size); -if (s->l1_size && l1_table ==NULL ) { +if (!l1_table) { ret = -ENOMEM ; goto fail; }
@@ -505,7 +505,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)* table is overwritten. */ sn_l1_table =g_try_malloc0 (cur_l1_bytes); -if (cur_l1_bytes && sn_l1_table ==NULL ) { +if (!sn_l1_table) { ret = -ENOMEM ; goto fail; }
block/qed-check.c +1 -1
@@ -228,7 +228,7 @@ int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix)int ret; check.used_clusters =g_try_new0 (uint32_t, (check.nclusters +31 ) /32 ); -if (check.nclusters && check.used_clusters ==NULL ) { +if (!check.used_clusters) {return -ENOMEM ; }
block/vdi.c +1 -1
@@ -305,7 +305,7 @@ static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res, } bmap =g_try_new (uint32_t, s->header.blocks_in_image); -if (s->header.blocks_in_image && bmap ==NULL ) { +if (!bmap) { res->check_errors++;return -ENOMEM ; }
block/vhdx.c +1 -1
@@ -1597,7 +1597,7 @@ static int vhdx_create_bat(BlockDriverState *bs, BDRVVHDXState *s, bdrv_has_zero_init(bs) == 0) {/* for a fixed file, the default BAT entry is not zero */ s->bat =g_try_malloc0 (length); -if (length && s->bat ==NULL ) { +if (!s->bat) { ret = -ENOMEM ; goto exit; }
block/vmdk.c +2 -2
@@ -456,7 +456,7 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,/* read the L1 table */ l1_size = extent->l1_size *sizeof (uint32_t ); extent->l1_table =g_try_malloc (l1_size); -if (l1_size && extent->l1_table ==NULL ) { +if (!extent->l1_table) {return -ENOMEM ; }
@@ -476,7 +476,7 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,if (extent->l1_backup_table_offset) { extent->l1_backup_table =g_try_malloc (l1_size); -if (l1_size && extent->l1_backup_table ==NULL ) { +if (!extent->l1_backup_table) { ret = -ENOMEM ; goto fail_l1; }
Signature · 1 line
1.9.3
Quoted 4 lines · Markus Armbruster
Unlike
malloc(),g_try_malloc()& friends return a null pointer only on failure, never for a zero size. Simplify tests for failure accordingly. This helps Coverity see returned null pointers can't be dereferenced. Also makes the code easier to read.
Unfortunately that's not what I see from the source:
gpointerg_try_malloc (gsize n_bytes)
{
gpointer mem;
if (G_LIKELY (n_bytes))
mem = glib_mem_vtable.try_malloc (n_bytes);
else
mem = NULL;
TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1)); return mem;
}
Paolo
Quoted 20 lines · Paolo Bonzini
Quoted 4 lines · Markus Armbruster
Unlike
malloc(),g_try_malloc()& friends return a null pointer only on failure, never for a zero size. Simplify tests for failure accordingly. This helps Coverity see returned null pointers can't be dereferenced. Also makes the code easier to read.Unfortunately that's not what I see from the source:
gpointer
g_try_malloc(gsizen_bytes)
{
gpointer mem;if (G_LIKELY (n_bytes)) mem = glib_mem_vtable.try_malloc (n_bytes); else mem = NULL; TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));
Quoted 2 lines
return mem;
}
You're right. Brain fart, please ignore.
Am 27.01.2015 um 14:42 hat Markus Armbruster geschrieben:
Quoted 20 lines
Quoted 18 lines · Paolo Bonzini
Quoted 4 lines · Markus Armbruster
Unlike
malloc(),g_try_malloc()& friends return a null pointer only on failure, never for a zero size. Simplify tests for failure accordingly. This helps Coverity see returned null pointers can't be dereferenced. Also makes the code easier to read.Unfortunately that's not what I see from the source:
gpointer
g_try_malloc(gsizen_bytes)
{
gpointer mem;if (G_LIKELY (n_bytes)) mem = glib_mem_vtable.try_malloc (n_bytes); else mem = NULL;
Quoted 6 lines
Quoted 4 lines
TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));return mem;
}You're right. Brain fart, please ignore.
Should we consider introducing a qemu_try_malloc() that has the desired behaviour? The (g_try_)malloc() interface is really easy to misuse and I've messed up some error checks in the block layer myself.
Kevin