Skip to contentSkip to conversation

xl/libxl: fix issues discovered by Coverity scan

PATCH Superseded Wei Liu 4 participants 34 messages Jul 10, 2015 – Jul 14, 2015

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover xl/libxl: fix issues discovered by Coverity scan

Wei Liu (9):
libxl: fix libxl__abs_path
libxl: turn two malloc's to libxl__malloc
libxl: json string object can be NULL
libxl: dispose dominfo to avoid leaking resource
libxl: avoid leaking string in cpupool_info
libxl: localtime(3) can return NULL
libxl: qmp_init_handler can return NULL
xl: fix main_cpupoolcreate
xl: fix main_config_update

 tools/libxl/libxl.c          | 4 +++-
 tools/libxl/libxl_aoutils.c  | 3 +--
 tools/libxl/libxl_device.c   | 2 ++
 tools/libxl/libxl_dm.c       | 2 +-
 tools/libxl/libxl_internal.c | 4 ++--
 tools/libxl/libxl_json.c     | 9 +++++++--
 tools/libxl/libxl_qmp.c      | 1 +
 tools/libxl/libxl_x86.c      | 6 ++++++
 tools/libxl/xl_cmdimpl.c     | 5 +++--
 9 files changed, 26 insertions(+), 10 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/9 libxl: fix libxl__abs_path

If s is NULL, just return NULL to avoid libxl__strdup dereferencing NULL pointer.

---
 tools/libxl/libxl_internal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_internal.c +2 -2
index 42d548e..6402c1b 100644
@@ -233,8 +233,8 @@ void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
 
 char *libxl__abs_path(libxl__gc *gc, const char *s, const char *path)
 {
-    if (!s || s[0] == '/')
-        return libxl__strdup(gc, s);
+    if (!s) return NULL;
+    if (s[0] == '/') return libxl__strdup(gc, s);
     return libxl__sprintf(gc, "%s/%s", path, s);
 }
 
Signature · 1 line
1.9.1
Acked-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

On Fri, 2015-07-10 at 19:00 +0100, Wei Liu wrote:

I rather dislike subjects of the form "fix $function", since it gives very little clue to someone reading the shortlog what is going on.

In this case I think "libxl: make libxl__abs_path correctly handle a NULL argument" would be an accurate description.

Quoted 4 lines

If s is NULL, just return NULL to avoid libxl__strdup dereferencing NULL pointer.

For the change itself:

Quoted 19 lines
---
 tools/libxl/libxl_internal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_internal.c +2 -2
index 42d548e..6402c1b 100644
@@ -233,8 +233,8 @@ void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
 
 char *libxl__abs_path(libxl__gc *gc, const char *s, const char *path)
 {
-    if (!s || s[0] == '/')
-        return libxl__strdup(gc, s);
+    if (!s) return NULL;
+    if (s[0] == '/') return libxl__strdup(gc, s);
     return libxl__sprintf(gc, "%s/%s", path, s);
 }
 
author
#3 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

Quoted 7 lines · Ian Campbell

On Fri, 2015-07-10 at 19:00 +0100, Wei Liu wrote:

I rather dislike subjects of the form "fix $function", since it gives very little clue to someone reading the shortlog what is going on.

In this case I think "libxl: make libxl__abs_path correctly handle a NULL argument" would be an accurate description.

Ack.

I can resend the whole series after updating subject for patch #1, #8 and #9.

Wei.

#4 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

In reply to Ian Campbell

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 2 lines

I rather dislike subjects of the form "fix $function", since it gives very little clue to someone reading the shortlog what is going on.

Yes.

Quoted 2 lines

In this case I think "libxl: make libxl__abs_path correctly handle a NULL argument" would be an accurate description.

But: it is quite surprising that libxl__abs_path can be legally passed a NULL for any of its parameters.

There are no call sites in libxl which can pass a NULL.

I think that if we are to retain this feature, it ought to be documented, at least.

Maybe

_hidden char *libxl__abs_path(libxl__gc *gc,
const char *s /* NULL OK */, const char *path);

?

Ian.

#5 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

Quoted 11 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 2 lines

I rather dislike subjects of the form "fix $function", since it gives very little clue to someone reading the shortlog what is going on.

Yes.

Quoted 2 lines

In this case I think "libxl: make libxl__abs_path correctly handle a NULL argument" would be an accurate description.

But: it is quite surprising that libxl__abs_path can be legally passed a NULL for any of its parameters.

True.

Quoted 11 lines

There are no call sites in libxl which can pass a NULL.

I think that if we are to retain this feature, it ought to be documented, at least.

Maybe

_hidden char *libxl__abs_path(libxl__gc *gc,
const char *s /* NULL OK */, const char *path);

Or add an assert if we don't wish to support this?

#6 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 7 lines · Ian Jackson
Quoted 4 lines

There are no call sites in libxl which can pass a NULL.

I think that if we are to retain this feature, it ought to be documented, at least.

Or add an assert if we don't wish to support this?

If we don't want to support it we can just remove the check and let it crash.

Ian.

#7 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

Quoted 11 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 7 lines · Ian Jackson
Quoted 4 lines

There are no call sites in libxl which can pass a NULL.

I think that if we are to retain this feature, it ought to be documented, at least.

Or add an assert if we don't wish to support this?

If we don't want to support it we can just remove the check and let it crash.

An assert at least gets us a slightly improved message when it happens.

Ian.

#8 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

If we don't want to support it we can just remove the check and let it crash.

An assert at least gets us a slightly improved message when it happens.

If that logic applies here it applies to almost every function in libxl. I don't think this is a sensible approach.

Ian.

#9 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

Quoted 9 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

If we don't want to support it we can just remove the check and let it crash.

An assert at least gets us a slightly improved message when it happens.

If that logic applies here it applies to almost every function in libxl. I don't think this is a sensible approach.

OK.

author
#10 lore raw

Re: [PATCH 1/9] libxl: fix libxl__abs_path

In reply to Ian Jackson
Quoted 4 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/9] libxl: fix libxl__abs_path"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

There are no call sites in libxl which can pass a NULL.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I think that if we are to retain this feature, it ought to be documented, at least.

Or add an assert if we don't wish to support this?

If we don't want to support it we can just remove the check and let it crash.

I will drop this patch and create a new one with this approach.

Wei.

Quoted 1 line

Ian.

author
#11 lore raw

PATCH 2/9 libxl: turn two malloc's to libxl__malloc

In reply to Wei Liu

One is to combine malloc + libxl__alloc_failed. The other is to avoid dereferencing NULL pointer in case of malloc failure.

---
 tools/libxl/libxl_aoutils.c | 3 +--
 tools/libxl/libxl_dm.c      | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
tools/libxl/libxl_aoutils.c +1 -2
index 0931eee..0300396 100644
@@ -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);
             }
tools/libxl/libxl_dm.c +1 -1
index ad434f0..0cc73be 100644
@@ -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(NOGC, dmargs_size);
     i = 1;
     dmargs[0] = '\0';
     while (args[i] != NULL) {
Signature · 1 line
1.9.1
Acked-by on v1 2/9
#12 lore raw

Re: [PATCH 2/9] libxl: turn two malloc's to libxl__malloc

Quoted 2 lines · Wei Liu

One is to combine malloc + libxl__alloc_failed. The other is to avoid dereferencing NULL pointer in case of malloc failure.

The non-use of a gc for the latter in particular looks a bit suspicious to me, but nonetheless this is an improvement:

Quoted 1 line
Quoted 32 lines
---
 tools/libxl/libxl_aoutils.c | 3 +--
 tools/libxl/libxl_dm.c      | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
tools/libxl/libxl_aoutils.c +1 -2
index 0931eee..0300396 100644
@@ -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);
             }
tools/libxl/libxl_dm.c +1 -1
index ad434f0..0cc73be 100644
@@ -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(NOGC, dmargs_size);
     i = 1;
     dmargs[0] = '\0';
     while (args[i] != NULL) {
author
#13 lore raw

Re: [PATCH 2/9] libxl: turn two malloc's to libxl__malloc

Quoted 7 lines · Ian Campbell
Quoted 2 lines · Wei Liu

One is to combine malloc + libxl__alloc_failed. The other is to avoid dereferencing NULL pointer in case of malloc failure.

The non-use of a gc for the latter in particular looks a bit suspicious to me, but nonetheless this is an improvement:

There is a free() later in this function. I wanted to make this patch minimum so I didn't switch to using gc and delete that free.

Quoted 3 lines
Quoted 1 line

Thanks.

Wei.

Quoted 34 lines
Quoted 33 lines
---
 tools/libxl/libxl_aoutils.c | 3 +--
 tools/libxl/libxl_dm.c      | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
tools/libxl/libxl_aoutils.c +1 -2
index 0931eee..0300396 100644
@@ -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);
             }
tools/libxl/libxl_dm.c +1 -1
index ad434f0..0cc73be 100644
@@ -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(NOGC, dmargs_size);
     i = 1;
     dmargs[0] = '\0';
     while (args[i] != NULL) {
author
#14 lore raw

PATCH 3/9 libxl: json string object can be NULL

In reply to Wei Liu
---
 tools/libxl/libxl_json.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/libxl_json.c +7 -2
index 346929a..652b3f4 100644
@@ -433,8 +433,13 @@ int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
 
     if (libxl__json_object_is_null(o))
         *p = NULL;
-    else
-        *p = libxl__strdup(NOGC, libxl__json_object_get_string(o));
+    else {
+        const char *s = libxl__json_object_get_string(o);
+        if (!s)
+            *p = NULL;
+        else
+            *p = libxl__strdup(NOGC, s);
+    }
 
     return 0;
 }
Signature · 1 line
1.9.1
Acked-by on v1 3/9
#15 lore raw

Re: [PATCH 3/9] libxl: json string object can be NULL

Quoted 1 line · Wei Liu

It occurs to me that since libxl__strdup can never return NULL due to allocation failure we could consider make libxl__strdup(gc, NULL) be well defined as returning NULL.

Quoted 24 lines
---
 tools/libxl/libxl_json.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/libxl_json.c +7 -2
index 346929a..652b3f4 100644
@@ -433,8 +433,13 @@ int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
 
     if (libxl__json_object_is_null(o))
         *p = NULL;
-    else
-        *p = libxl__strdup(NOGC, libxl__json_object_get_string(o));
+    else {
+        const char *s = libxl__json_object_get_string(o);
+        if (!s)
+            *p = NULL;
+        else
+            *p = libxl__strdup(NOGC, s);
+    }
 
     return 0;
 }
#16 lore raw

Re: [PATCH 3/9] libxl: json string object can be NULL

Ian Campbell writes ("Re: [PATCH 3/9] libxl: json string object can be NULL"):

Quoted 8 lines · Wei Liu
Quoted 1 line

It occurs to me that since libxl__strdup can never return NULL due to allocation failure we could consider make libxl__strdup(gc, NULL) be well defined as returning NULL.

This would be a nicer fix, yes.

Ian.

author
#17 lore raw

PATCH 4/9 libxl: dispose dominfo to avoid leaking resource

In reply to Wei Liu

Add libxl_dominfo_dispose to one return path that doesn't have it.

---
 tools/libxl/libxl_device.c | 2 ++
 1 file changed, 2 insertions(+)
tools/libxl/libxl_device.c +2 -0
index 2493972..3f8b555 100644
@@ -816,6 +816,8 @@ void libxl__initiate_device_remove(libxl__egc *egc,
                            be_path);
                 goto out;
             }
+
+            libxl_dominfo_dispose(&info);
             return;
         }
     }
Signature · 1 line
1.9.1
Acked-by on v1 4/9
#18 lore raw

Re: [PATCH 4/9] libxl: dispose dominfo to avoid leaking resource

Quoted 3 lines · Wei Liu

Add libxl_dominfo_dispose to one return path that doesn't have it.

That return is a bit at odds with the generally correct error handling in that function, but this improves things at least a little and I can sort of see why this a slightly special case, so:

Quoted 17 lines
---
 tools/libxl/libxl_device.c | 2 ++
 1 file changed, 2 insertions(+)
tools/libxl/libxl_device.c +2 -0
index 2493972..3f8b555 100644
@@ -816,6 +816,8 @@ void libxl__initiate_device_remove(libxl__egc *egc,
                            be_path);
                 goto out;
             }
+
+            libxl_dominfo_dispose(&info);
             return;
         }
     }
#19 lore raw

Re: [PATCH 4/9] libxl: dispose dominfo to avoid leaking resource

Ian Campbell writes ("Re: [PATCH 4/9] libxl: dispose dominfo to avoid leaking resource"):

Quoted 8 lines · Wei Liu
Quoted 3 lines

Add libxl_dominfo_dispose to one return path that doesn't have it.

That return is a bit at odds with the generally correct error handling in that function, but this improves things at least a little and I can sort of see why this a slightly special case, so:

I think this should be done by changing return' to goto out_success' or some such.

Ian.

author
#20 lore raw

PATCH 5/9 libxl: avoid leaking string in cpupool_info

In reply to Wei Liu
---
 tools/libxl/libxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +3 -1
index 38aff8d..4151dcb 100644
@@ -740,8 +740,10 @@ static int cpupool_info(libxl__gc *gc,
     info->sched = xcinfo->sched_id;
     info->n_dom = xcinfo->n_dom;
     rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
-    if (rc)
+    if (rc) {
+        free(info->pool_name);
         goto out;
+    }
 
     memcpy(info->cpumap.map, xcinfo->cpumap, info->cpumap.size);
 
Signature · 1 line
1.9.1
#21 lore raw

Re: [PATCH 5/9] libxl: avoid leaking string in cpupool_info

Quoted 1 line · Wei Liu

Would an "if (rc) libxl_cpupoolinfo_dispose(info)" on the exit path be a more robust alternative? Might require the addition of a libxl_cpupoolinfo_init() somewhere before any possible error.

Quoted 20 lines
---
 tools/libxl/libxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +3 -1
index 38aff8d..4151dcb 100644
@@ -740,8 +740,10 @@ static int cpupool_info(libxl__gc *gc,
     info->sched = xcinfo->sched_id;
     info->n_dom = xcinfo->n_dom;
     rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
-    if (rc)
+    if (rc) {
+        free(info->pool_name);
         goto out;
+    }
 
     memcpy(info->cpumap.map, xcinfo->cpumap, info->cpumap.size);
 
author
#22 lore raw

Re: [PATCH 5/9] libxl: avoid leaking string in cpupool_info

Quoted 7 lines · Ian Campbell
Quoted 1 line · Wei Liu

Would an "if (rc) libxl_cpupoolinfo_dispose(info)" on the exit path be a more robust alternative? Might require the addition of a libxl_cpupoolinfo_init() somewhere before any possible error.

Yes, you're right, it would be better to fix the caller.

This results in a larger patch than this one. I will send out the new version soon.

Wei.

Quoted 21 lines
Quoted 20 lines
---
 tools/libxl/libxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +3 -1
index 38aff8d..4151dcb 100644
@@ -740,8 +740,10 @@ static int cpupool_info(libxl__gc *gc,
     info->sched = xcinfo->sched_id;
     info->n_dom = xcinfo->n_dom;
     rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
-    if (rc)
+    if (rc) {
+        free(info->pool_name);
         goto out;
+    }
 
     memcpy(info->cpumap.map, xcinfo->cpumap, info->cpumap.size);
 
#23 lore raw

Re: [PATCH 5/9] libxl: avoid leaking string in cpupool_info

Quoted 10 lines · Wei Liu
Quoted 7 lines · Ian Campbell
Quoted 1 line · Wei Liu

Would an "if (rc) libxl_cpupoolinfo_dispose(info)" on the exit path be a more robust alternative? Might require the addition of a libxl_cpupoolinfo_init() somewhere before any possible error.

Yes, you're right, it would be better to fix the caller.

I was suggesting to do it in this function, not the caller.

Quoted 27 lines

This results in a larger patch than this one. I will send out the new version soon.

Wei.

Quoted 21 lines
Quoted 20 lines
---
 tools/libxl/libxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +3 -1
index 38aff8d..4151dcb 100644
@@ -740,8 +740,10 @@ static int cpupool_info(libxl__gc *gc,
     info->sched = xcinfo->sched_id;
     info->n_dom = xcinfo->n_dom;
     rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
-    if (rc)
+    if (rc) {
+        free(info->pool_name);
         goto out;
+    }
 
     memcpy(info->cpumap.map, xcinfo->cpumap, info->cpumap.size);
 
author
#24 lore raw

Re: [PATCH 5/9] libxl: avoid leaking string in cpupool_info

Quoted 5 lines · Ian Campbell
Quoted 4 lines · Wei Liu
Quoted 3 lines · Ian Campbell
Quoted 1 line · Wei Liu
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Would an "if (rc) libxl_cpupoolinfo_dispose(info)" on the exit path be a more robust alternative? Might require the addition of a libxl_cpupoolinfo_init() somewhere before any possible error.

Yes, you're right, it would be better to fix the caller.

I was suggesting to do it in this function, not the caller.

No, that's not right, that violates libxl calling style. The caller is supposed to call _init function before coming to this function.

Wei.

Quoted 29 lines
Quoted 28 lines

This results in a larger patch than this one. I will send out the new version soon.

Wei.

Quoted 21 lines
Quoted 20 lines
---
 tools/libxl/libxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl.c +3 -1
index 38aff8d..4151dcb 100644
@@ -740,8 +740,10 @@ static int cpupool_info(libxl__gc *gc,
     info->sched = xcinfo->sched_id;
     info->n_dom = xcinfo->n_dom;
     rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
-    if (rc)
+    if (rc) {
+        free(info->pool_name);
         goto out;
+    }
 
     memcpy(info->cpumap.map, xcinfo->cpumap, info->cpumap.size);
 
author
#25 lore raw

PATCH 6/9 libxl: localtime(3) can return NULL

In reply to Wei Liu
---
 tools/libxl/libxl_x86.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/libxl/libxl_x86.c +6 -0
index 8cd15ca..6be9b1b 100644
@@ -311,6 +311,11 @@ int libxl__arch_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         t = time(NULL);
         tm = localtime(&t);
 
+        if (!tm) {
+            ret = ERROR_FAIL;
+            goto out;
+        }
+
         rtc_timeoffset += tm->tm_gmtoff;
     }
 
@@ -335,6 +340,7 @@ int libxl__arch_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
         }
     }
 
+out:
     return ret;
 }
 
Signature · 1 line
1.9.1
author
#27 lore raw

PATCH 7/9 libxl: qmp_init_handler can return NULL

In reply to Wei Liu
---
 tools/libxl/libxl_qmp.c | 1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl_qmp.c +1 -0
index 6484f5e..965c507 100644
@@ -694,6 +694,7 @@ libxl__qmp_handler *libxl__qmp_initialize(libxl__gc *gc, uint32_t domid)
     char *qmp_socket;
 
     qmp = qmp_init_handler(gc, domid);
+    if (!qmp) return NULL;
 
     qmp_socket = GCSPRINTF("%s/qmp-libxl-%d", libxl__run_dir_path(), domid);
     if ((ret = qmp_open(qmp, qmp_socket, QMP_SOCKET_CONNECT_TIMEOUT)) < 0) {
Signature · 1 line
1.9.1
Acked-by on v1 7/9
#28 lore raw

Re: [PATCH 7/9] libxl: qmp_init_handler can return NULL

Quoted 1 line · Wei Liu

(although the only actual reason for a failure today is a memory allocation failure, which ought to abort really).

Quoted 16 lines
---
 tools/libxl/libxl_qmp.c | 1 +
 1 file changed, 1 insertion(+)
tools/libxl/libxl_qmp.c +1 -0
index 6484f5e..965c507 100644
@@ -694,6 +694,7 @@ libxl__qmp_handler *libxl__qmp_initialize(libxl__gc *gc, uint32_t domid)
     char *qmp_socket;
 
     qmp = qmp_init_handler(gc, domid);
+    if (!qmp) return NULL;
 
     qmp_socket = GCSPRINTF("%s/qmp-libxl-%d", libxl__run_dir_path(), domid);
     if ((ret = qmp_open(qmp, qmp_socket, QMP_SOCKET_CONNECT_TIMEOUT)) < 0) {
author
#29 lore raw

PATCH 8/9 xl: fix main_cpupoolcreate

In reply to Wei Liu

Don't dereference extra_config if it's NULL. Don't leak extra_config in the end.

---
 tools/libxl/xl_cmdimpl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +2 -1
index 971209c..d44eb4b 100644
@@ -7228,7 +7228,7 @@ int main_cpupoolcreate(int argc, char **argv)
     else
         config_src="command line";
 
-    if (strlen(extra_config)) {
+    if (extra_config && strlen(extra_config)) {
         if (config_len > INT_MAX - (strlen(extra_config) + 2)) {
             fprintf(stderr, "Failed to attach extra configration\n");
             goto out;
@@ -7365,6 +7365,7 @@ out_cfg:
 out:
     free(name);
     free(config_data);
+    free(extra_config);
     return rc;
 }
 
Signature · 1 line
1.9.1
#30 lore raw

Re: [PATCH 8/9] xl: fix main_cpupoolcreate

Quoted 2 lines · Wei Liu

Don't dereference extra_config if it's NULL. Don't leak extra_config in the end.

Subject should be more descriptive. "xl: correct handling of extra_config in main_cpupoolcreate" perhaps? (It's a lot easier to write non-vague messages for patches which only do one thing)

Quoted 10 lines
---
 tools/libxl/xl_cmdimpl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +0 -0
index 971209c..d44eb4b 100644

tools/libxl/xl_cmdimpl.c int main_cpupoolcreate(int argc, char **argv) · line 7233

Quoted 7 lines
tools/libxl/xl_cmdimpl.c +1 -1
@@ -7228,7 +7228,7 @@ int main_cpupoolcreate(int argc, char **argv)
     else
         config_src="command line";
 
-    if (strlen(extra_config)) {
+    if (extra_config && strlen(extra_config)) {
         if (config_len > INT_MAX - (strlen(extra_config) + 2)) {
             fprintf(stderr, "Failed to attach extra configration\n");

There's a typo in this line of context...

Quoted 9 lines
tools/libxl/xl_cmdimpl.c +1 -0
             goto out;
@@ -7365,6 +7365,7 @@ out_cfg:
 out:
     free(name);
     free(config_data);
+    free(extra_config);
     return rc;
 }
 
author
#31 lore raw

PATCH 9/9 xl: fix main_config_update

In reply to Wei Liu

Don't dereference NULL.

---
 tools/libxl/xl_cmdimpl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +1 -1
index d44eb4b..631dbd1 100644
@@ -5010,7 +5010,7 @@ int main_config_update(int argc, char **argv)
         if (rc) { fprintf(stderr, "Failed to read config file: %s: %s\n",
                            filename, strerror(errno));
                   free(extra_config); return ERROR_FAIL; }
-        if (strlen(extra_config)) {
+        if (extra_config && strlen(extra_config)) {
             if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1)) {
                 fprintf(stderr, "Failed to attach extra configration\n");
                 exit(1);
Signature · 1 line
1.9.1
#32 lore raw

Re: [PATCH 9/9] xl: fix main_config_update

Quoted 1 line · Wei Liu

Don't dereference NULL.

Subject: xl: correctly handle null extra config in main_config_update

Quoted 19 lines
---
 tools/libxl/xl_cmdimpl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/xl_cmdimpl.c +1 -1
index d44eb4b..631dbd1 100644
@@ -5010,7 +5010,7 @@ int main_config_update(int argc, char **argv)
         if (rc) { fprintf(stderr, "Failed to read config file: %s: %s\n",
                            filename, strerror(errno));
                   free(extra_config); return ERROR_FAIL; }
-        if (strlen(extra_config)) {
+        if (extra_config && strlen(extra_config)) {
             if (config_len > INT_MAX - (strlen(extra_config) + 2 + 1)) {
                 fprintf(stderr, "Failed to attach extra configration\n");
                 exit(1);
#33 lore raw
In reply to Wei Liu
Quoted 10 lines · Wei Liu

Wei Liu (9):
libxl: fix libxl__abs_path
libxl: turn two malloc's to libxl__malloc
libxl: json string object can be NULL
libxl: dispose dominfo to avoid leaking resource
libxl: avoid leaking string in cpupool_info
libxl: localtime(3) can return NULL
libxl: qmp_init_handler can return NULL
xl: fix main_cpupoolcreate
xl: fix main_config_update

Correlating with Coverity Scan, the only defect in common is

1/9 - Coverity-ID: 1198722

All other defects are only found in XenServers instance (we do have all the knobs turned up to 11).

~Andrew