xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Wei Liu <wei.liu2@citrix.com>,
	Dario Faggioli <dario.faggioli@citrix.com>,
	Ian Jackson <Ian.Jackson@eu.citrix.com>,
	Ian Campbell <ian.campbell@citrix.com>
Subject: [PATCH for-4.6 v2 1/8] libxl: properly clean up array in libxl_list_cpupool failure path
Date: Mon, 27 Jul 2015 18:45:02 +0100	[thread overview]
Message-ID: <1438019109-31997-2-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1438019109-31997-1-git-send-email-wei.liu2@citrix.com>

Document how cpupool_info works.  Distinguish success (ERROR_FAIL +
ENOENT) vs failure in libxl_list_cpupool and properly clean up the array
in failure path.

Also switch to libxl__realloc and call libxl_cpupool_{init,dispose}
where appropriate.

There is change of behaviour. Previously if memory allocation fails the
said function returns NULL. Now memory allocation failure is fatal. This
is in line with how we deal with memory allocation failure in other
places in libxl though.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
Cc: Dario Faggioli <dario.faggioli@citrix.com>
---
 tools/libxl/libxl.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index ff0d616..c3a13f6 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -682,6 +682,11 @@ int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
     return 0;
 }
 
+/* Returns:
+ *   0 - success
+ *   ERROR_FAIL + errno == ENOENT - no entry found
+ *   ERROR_$FOO + errno != ENOENT - other failure
+ */
 static int cpupool_info(libxl__gc *gc,
                         libxl_cpupoolinfo *info,
                         uint32_t poolid,
@@ -737,7 +742,8 @@ int libxl_cpupool_info(libxl_ctx *ctx,
 libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool_out)
 {
     GC_INIT(ctx);
-    libxl_cpupoolinfo info, *ptr, *tmp;
+    libxl_cpupoolinfo info, *ptr;
+
     int i;
     uint32_t poolid;
 
@@ -745,24 +751,29 @@ libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool_out)
 
     poolid = 0;
     for (i = 0;; i++) {
-        if (cpupool_info(gc, &info, poolid, false))
+        libxl_cpupoolinfo_init(&info);
+        if (cpupool_info(gc, &info, poolid, false)) {
+            libxl_cpupoolinfo_dispose(&info);
+            if (errno != ENOENT) goto out;
             break;
-        tmp = realloc(ptr, (i + 1) * sizeof(libxl_cpupoolinfo));
-        if (!tmp) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpupool info");
-            libxl_cpupoolinfo_list_free(ptr, i);
-            ptr = NULL;
-            goto out;
         }
-        ptr = tmp;
+
+        ptr = libxl__realloc(NOGC, ptr, (i+1) * sizeof(libxl_cpupoolinfo));
         ptr[i] = info;
         poolid = info.poolid + 1;
+        /* Don't dispose of info because it will be returned to caller */
     }
 
     *nb_pool_out = i;
-out:
+
     GC_FREE;
     return ptr;
+
+out:
+    libxl_cpupoolinfo_list_free(ptr, i);
+    *nb_pool_out = 0;
+    GC_FREE;
+    return NULL;
 }
 
 /* this API call only list VM running on this host. A VM can
-- 
2.1.4

  reply	other threads:[~2015-07-27 17:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-27 17:45 [PATCH for-4.6 v2 0/8] tools: fixes inspired by Coverity scan Wei Liu
2015-07-27 17:45 ` Wei Liu [this message]
2015-07-28  8:39   ` [PATCH for-4.6 v2 1/8] libxl: properly clean up array in libxl_list_cpupool failure path Dario Faggioli
2015-07-28 10:27   ` Ian Campbell
2015-07-27 17:45 ` [PATCH for-4.6 v2 2/8] xl: lockdir should be lockfile in error message Wei Liu
2015-07-28 10:27   ` Ian Campbell
2015-07-27 17:45 ` [PATCH for-4.6 v2 3/8] xl: call libxl_dominfo_init in main_list Wei Liu
2015-07-28 10:28   ` Ian Campbell
2015-07-27 17:45 ` [PATCH for-4.6 v2 4/8] xl: valid fd can be 0 in main_loadpolicy Wei Liu
2015-07-28 10:28   ` Ian Campbell
2015-07-27 17:45 ` [PATCH for-4.6 v2 5/8] xl: call libxl_dominfo_{init, dispose} in main_cpupoolnumasplit Wei Liu
2015-07-28  9:49   ` Dario Faggioli
2015-07-28 10:33   ` Ian Campbell
2015-07-27 17:45 ` [PATCH for-4.6 v2 6/8] libxlu: free buffer in failure path for PCI related functions Wei Liu
2015-07-28 10:35   ` Ian Campbell
2015-07-27 17:45 ` [PATCH for-4.6 v2 7/8] python/xc: reinstate original implementation of next_bdf Wei Liu
2015-07-27 22:58   ` Andrew Cooper
2015-07-28  1:06   ` Chen, Tiejun
2015-07-28 10:38   ` Ian Campbell
2015-07-28 10:44     ` Wei Liu
2015-07-27 17:45 ` [PATCH for-4.6 v2 8/8] libxl: remove dead code libxl__domain_shutdown_reason Wei Liu
2015-07-28 10:52   ` Ian Campbell
2015-07-28 11:08 ` [PATCH for-4.6 v2 0/8] tools: fixes inspired by Coverity scan Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1438019109-31997-2-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=dario.faggioli@citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).