Message ID | 1408392029-29531-3-git-send-email-mreitz@redhat.com |
---|---|
State | New |
Headers | show |
On 18.08.2014 22:00, Max Reitz wrote: > With a variable cache size, the number given to qcow2_cache_create() may > be huge. Therefore, use g_try_new0(). > > While at it, use g_new0() instead of g_malloc0() for allocating the > Qcow2Cache object. > > Signed-off-by: Max Reitz <mreitz@redhat.com> > --- > block/qcow2-cache.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c > index fe0615a..6eabfaa 100644 > --- a/block/qcow2-cache.c > +++ b/block/qcow2-cache.c > @@ -48,9 +48,12 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) > Qcow2Cache *c; > int i; > > - c = g_malloc0(sizeof(*c)); > + c = g_new0(Qcow2Cache, 1); > c->size = num_tables; > - c->entries = g_new0(Qcow2CachedTable, num_tables); > + c->entries = g_try_new0(Qcow2CachedTable, num_tables); > + if (!c->entries) { > + goto fail; > + } Self-NACK: The fail path needs to acknowledge that c->entries may be NULL. Max
diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index fe0615a..6eabfaa 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -48,9 +48,12 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) Qcow2Cache *c; int i; - c = g_malloc0(sizeof(*c)); + c = g_new0(Qcow2Cache, 1); c->size = num_tables; - c->entries = g_new0(Qcow2CachedTable, num_tables); + c->entries = g_try_new0(Qcow2CachedTable, num_tables); + if (!c->entries) { + goto fail; + } for (i = 0; i < c->size; i++) { c->entries[i].table = qemu_try_blockalign(bs->file, s->cluster_size);
With a variable cache size, the number given to qcow2_cache_create() may be huge. Therefore, use g_try_new0(). While at it, use g_new0() instead of g_malloc0() for allocating the Qcow2Cache object. Signed-off-by: Max Reitz <mreitz@redhat.com> --- block/qcow2-cache.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)