From patchwork Mon Jan 16 09:28:06 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 136265 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 52FF61007D1 for ; Mon, 16 Jan 2012 20:28:35 +1100 (EST) Received: from localhost ([::1]:47252 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rmirh-0004d4-V8 for incoming@patchwork.ozlabs.org; Mon, 16 Jan 2012 04:28:33 -0500 Received: from eggs.gnu.org ([140.186.70.92]:55317) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmirZ-0004ci-Kg for qemu-devel@nongnu.org; Mon, 16 Jan 2012 04:28:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RmirP-0004te-6a for qemu-devel@nongnu.org; Mon, 16 Jan 2012 04:28:24 -0500 Received: from e06smtp10.uk.ibm.com ([195.75.94.106]:41502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RmirO-0004tI-O9 for qemu-devel@nongnu.org; Mon, 16 Jan 2012 04:28:15 -0500 Received: from /spool/local by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 16 Jan 2012 09:28:12 -0000 Received: from d06nrmr1407.portsmouth.uk.ibm.com ([9.149.38.185]) by e06smtp10.uk.ibm.com ([192.168.101.140]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 16 Jan 2012 09:28:11 -0000 Received: from d06av08.portsmouth.uk.ibm.com (d06av08.portsmouth.uk.ibm.com [9.149.37.249]) by d06nrmr1407.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q0G9SAW52060300 for ; Mon, 16 Jan 2012 09:28:10 GMT Received: from d06av08.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av08.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q0G9SAga026322 for ; Mon, 16 Jan 2012 09:28:10 GMT Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av08.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q0G9S9Oh026306; Mon, 16 Jan 2012 09:28:10 GMT From: Stefan Hajnoczi To: Date: Mon, 16 Jan 2012 09:28:06 +0000 Message-Id: <1326706086-4316-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.8.3 x-cbid: 12011609-4966-0000-0000-0000012B1788 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.106 Cc: Kevin Wolf , davidagilbert@uk.ibm.com, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH] block: replace unchecked strdup/malloc/calloc with glib X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Most of the codebase as been converted to use glib memory allocation functions. There are still a few instances of malloc/calloc in the block layer and qemu-io. Replace them, especially since they do not check the strdup/malloc/calloc return value. Reported-by: Dr David Alan Gilbert Signed-off-by: Stefan Hajnoczi --- block/blkdebug.c | 4 ++-- block/blkverify.c | 4 ++-- qemu-io.c | 48 ++++++++++++++++++++++++------------------------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index 9b88535..a251802 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -292,10 +292,10 @@ static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags) return -EINVAL; } - config = strdup(filename); + config = g_strdup(filename); config[c - filename] = '\0'; ret = read_config(s, config); - free(config); + g_free(config); if (ret < 0) { return ret; } diff --git a/block/blkverify.c b/block/blkverify.c index 4ca8584..9d5f1ec 100644 --- a/block/blkverify.c +++ b/block/blkverify.c @@ -87,10 +87,10 @@ static int blkverify_open(BlockDriverState *bs, const char *filename, int flags) return -EINVAL; } - raw = strdup(filename); + raw = g_strdup(filename); raw[c - filename] = '\0'; ret = bdrv_file_open(&bs->file, raw, flags); - free(raw); + g_free(raw); if (ret < 0) { return ret; } diff --git a/qemu-io.c b/qemu-io.c index ffa62fb..7c446b6 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -130,7 +130,7 @@ static void print_report(const char *op, struct timeval *t, int64_t offset, static void * create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern) { - size_t *sizes = calloc(nr_iov, sizeof(size_t)); + size_t *sizes = g_new0(size_t, nr_iov); size_t count = 0; void *buf = NULL; void *p; @@ -172,7 +172,7 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern) } fail: - free(sizes); + g_free(sizes); return buf; } @@ -471,14 +471,14 @@ static int read_f(int argc, char **argv) } if (Pflag) { - void *cmp_buf = malloc(pattern_count); + void *cmp_buf = g_malloc(pattern_count); memset(cmp_buf, pattern, pattern_count); if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { printf("Pattern verification failed at offset %" PRId64 ", %d bytes\n", offset + pattern_offset, pattern_count); } - free(cmp_buf); + g_free(cmp_buf); } if (qflag) { @@ -601,13 +601,13 @@ static int readv_f(int argc, char **argv) } if (Pflag) { - void *cmp_buf = malloc(qiov.size); + void *cmp_buf = g_malloc(qiov.size); memset(cmp_buf, pattern, qiov.size); if (memcmp(buf, cmp_buf, qiov.size)) { printf("Pattern verification failed at offset %" PRId64 ", %zd bytes\n", offset, qiov.size); } - free(cmp_buf); + g_free(cmp_buf); } if (qflag) { @@ -1063,7 +1063,7 @@ static void aio_write_done(void *opaque, int ret) ctx->qiov.size, 1, ctx->Cflag); out: qemu_io_free(ctx->buf); - free(ctx); + g_free(ctx); } static void aio_read_done(void *opaque, int ret) @@ -1079,14 +1079,14 @@ static void aio_read_done(void *opaque, int ret) } if (ctx->Pflag) { - void *cmp_buf = malloc(ctx->qiov.size); + void *cmp_buf = g_malloc(ctx->qiov.size); memset(cmp_buf, ctx->pattern, ctx->qiov.size); if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { printf("Pattern verification failed at offset %" PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size); } - free(cmp_buf); + g_free(cmp_buf); } if (ctx->qflag) { @@ -1103,7 +1103,7 @@ static void aio_read_done(void *opaque, int ret) ctx->qiov.size, 1, ctx->Cflag); out: qemu_io_free(ctx->buf); - free(ctx); + g_free(ctx); } static void aio_read_help(void) @@ -1141,7 +1141,7 @@ static const cmdinfo_t aio_read_cmd = { static int aio_read_f(int argc, char **argv) { int nr_iov, c; - struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx)); + struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); while ((c = getopt(argc, argv, "CP:qv")) != EOF) { switch (c) { @@ -1152,7 +1152,7 @@ static int aio_read_f(int argc, char **argv) ctx->Pflag = 1; ctx->pattern = parse_pattern(optarg); if (ctx->pattern < 0) { - free(ctx); + g_free(ctx); return 0; } break; @@ -1163,20 +1163,20 @@ static int aio_read_f(int argc, char **argv) ctx->vflag = 1; break; default: - free(ctx); + g_free(ctx); return command_usage(&aio_read_cmd); } } if (optind > argc - 2) { - free(ctx); + g_free(ctx); return command_usage(&aio_read_cmd); } ctx->offset = cvtnum(argv[optind]); if (ctx->offset < 0) { printf("non-numeric length argument -- %s\n", argv[optind]); - free(ctx); + g_free(ctx); return 0; } optind++; @@ -1184,14 +1184,14 @@ static int aio_read_f(int argc, char **argv) if (ctx->offset & 0x1ff) { printf("offset %" PRId64 " is not sector aligned\n", ctx->offset); - free(ctx); + g_free(ctx); return 0; } nr_iov = argc - optind; ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab); if (ctx->buf == NULL) { - free(ctx); + g_free(ctx); return 0; } @@ -1237,7 +1237,7 @@ static int aio_write_f(int argc, char **argv) { int nr_iov, c; int pattern = 0xcd; - struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx)); + struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); while ((c = getopt(argc, argv, "CqP:")) != EOF) { switch (c) { @@ -1250,25 +1250,25 @@ static int aio_write_f(int argc, char **argv) case 'P': pattern = parse_pattern(optarg); if (pattern < 0) { - free(ctx); + g_free(ctx); return 0; } break; default: - free(ctx); + g_free(ctx); return command_usage(&aio_write_cmd); } } if (optind > argc - 2) { - free(ctx); + g_free(ctx); return command_usage(&aio_write_cmd); } ctx->offset = cvtnum(argv[optind]); if (ctx->offset < 0) { printf("non-numeric length argument -- %s\n", argv[optind]); - free(ctx); + g_free(ctx); return 0; } optind++; @@ -1276,14 +1276,14 @@ static int aio_write_f(int argc, char **argv) if (ctx->offset & 0x1ff) { printf("offset %" PRId64 " is not sector aligned\n", ctx->offset); - free(ctx); + g_free(ctx); return 0; } nr_iov = argc - optind; ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern); if (ctx->buf == NULL) { - free(ctx); + g_free(ctx); return 0; }