From mboxrd@z Thu Jan 1 00:00:00 1970 From: Wei Liu Subject: [PATCH v3 02/10] libxl: turn two malloc's to libxl__malloc Date: Tue, 14 Jul 2015 17:41:05 +0100 Message-ID: <1436892073-14186-3-git-send-email-wei.liu2@citrix.com> References: <1436892073-14186-1-git-send-email-wei.liu2@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1ZF3Gi-0002Rs-4k for xen-devel@lists.xenproject.org; Tue, 14 Jul 2015 16:41:20 +0000 In-Reply-To: <1436892073-14186-1-git-send-email-wei.liu2@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel Cc: Wei Liu , Ian Jackson , Ian Campbell List-Id: xen-devel@lists.xenproject.org One is to combine malloc + libxl__alloc_failed. The other is to avoid dereferencing NULL pointer in case of malloc failure. Also use gc for memory allocation and remove free() in second case. Signed-off-by: Wei Liu --- tools/libxl/libxl_aoutils.c | 3 +-- tools/libxl/libxl_dm.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/libxl/libxl_aoutils.c b/tools/libxl/libxl_aoutils.c index 0931eee..0300396 100644 --- a/tools/libxl/libxl_aoutils.c +++ b/tools/libxl/libxl_aoutils.c @@ -245,8 +245,7 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, buf = LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs); if (!buf || buf->used >= sizeof(buf->buf)) { - buf = malloc(sizeof(*buf)); - if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf)); + buf = libxl__malloc(NOGC, sizeof(*buf)); buf->used = 0; LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry); } diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c index ad434f0..8f3c7a5 100644 --- a/tools/libxl/libxl_dm.c +++ b/tools/libxl/libxl_dm.c @@ -1010,7 +1010,7 @@ static int libxl__write_stub_dmargs(libxl__gc *gc, i++; } dmargs_size++; - dmargs = (char *) malloc(dmargs_size); + dmargs = (char *) libxl__malloc(gc, dmargs_size); i = 1; dmargs[0] = '\0'; while (args[i] != NULL) { @@ -1030,7 +1030,6 @@ retry_transaction: if (!xs_transaction_end(ctx->xsh, t, 0)) if (errno == EAGAIN) goto retry_transaction; - free(dmargs); return 0; } -- 1.9.1