Skip to contentSkip to conversation

libxenstat bug fixes and cleanups

PATCH Superseded Wei Liu 3 participants 14 messages Apr 8, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover libxenstat bug fixes and cleanups

Wei Liu (4):
libxenstat: check xc_interface_open return value
libxenstat: YAJL_GET_STRING may return NULL
libxenstat: always free qmp_stats
libxenstat: qmp_read fix and cleanup

 tools/xenstat/libxenstat/src/xenstat_qmp.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/4 libxenstat: check xc_interface_open return value

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 2 ++
 1 file changed, 2 insertions(+)
tools/xenstat/libxenstat/src/xenstat_qmp.c +2 -0
index 2cb99e9..f3aeec4 100644
@@ -366,6 +366,8 @@ static xc_domaininfo_t *get_domain_ids(int *num_doms)
 	if (dominfo == NULL)
 		return NULL;
 	xc_handle = xc_interface_open(0,0,0);
+	if (xc_handle == NULL)
+		return NULL;
 	*num_doms = xc_domain_getinfolist(xc_handle, 0, 1024, dominfo);
 	xc_interface_close(xc_handle);
 	return dominfo;
Signature · 1 line
1.9.1
author
#4 lore raw

PATCH 2/4 libxenstat: YAJL_GET_STRING may return NULL

In reply to Wei Liu

Passing NULL to strcmp can cause segmentation fault. Continue in that case.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xenstat/libxenstat/src/xenstat_qmp.c +1 -1
index f3aeec4..6bfbc2f 100644
@@ -110,7 +110,7 @@ static char *qmp_get_block_image(xenstat_node *node, char *qmp_devname, int qfd)
 		ptr[0] = qblock[QMP_BLOCK_DEVICE]; /* "device" */
 		if ((dev_obj = yajl_tree_get(n, ptr, yajl_t_any)) != NULL) {
 			tmp = YAJL_GET_STRING(dev_obj);
-			if (strcmp(qmp_devname, tmp))
+			if (!tmp || strcmp(qmp_devname, tmp))
 				continue;
 		}
 		else
Signature · 1 line
1.9.1
author
#7 lore raw

PATCH 3/4 libxenstat: always free qmp_stats

In reply to Wei Liu

Originally qmp_stats is only freed in failure path and leaked in success path.

Instead of wiring up the success path, rearrange the code a bit to always free qmp_stats before checking if info is NULL.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/xenstat/libxenstat/src/xenstat_qmp.c +3 -3
index 6bfbc2f..e4f343b 100644
@@ -95,10 +95,10 @@ static char *qmp_get_block_image(xenstat_node *node, char *qmp_devname, int qfd)
 		return NULL;
 
 	/* Use libyajl version 2.0.3 or newer for the tree parser feature with bug fixes */
-	if ((info = yajl_tree_parse((char *)qmp_stats, NULL, 0)) == NULL) {
-		free(qmp_stats);
+	info = yajl_tree_parse((char *)qmp_stats, NULL, 0);
+	free(qmp_stats);
+	if (info == NULL)
 		return NULL;
-	}
 
 	ptr[0] = qblock[QMP_BLOCK_RETURN]; /* "return" */
 	if ((ret_obj = yajl_tree_get(info, ptr, yajl_t_array)) == NULL)
Signature · 1 line
1.9.1
author
#10 lore raw

PATCH 4/4 libxenstat: qmp_read fix and cleanup

In reply to Wei Liu

The second argument of poll(2) is the number of file descriptors. POLLIN is defined as 1 so it happens to work.

Also do two cleanups while I was there:
1. There is only one fd, so a one-element array is enough.
2. Initialise pfd to make code linter happy.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/xenstat/libxenstat/src/xenstat_qmp.c +6 -2
index e4f343b..260cd34 100644
@@ -289,13 +289,14 @@ static size_t qmp_write(int qfd, char *cmd, size_t cmd_len)
 static int qmp_read(int qfd, unsigned char **qstats)
 {
 	unsigned char buf[1024], *ptr;
-	struct pollfd pfd[2];
+	struct pollfd pfd[1];
 	int n, qsize = 0;
 
 	*qstats = NULL;
+	memset(pfd, 0, sizeof(pfd));
 	pfd[0].fd = qfd;
 	pfd[0].events = POLLIN;
-	while ((n = poll(pfd, POLLIN, 10)) > 0) {
+	while ((n = poll(pfd, 1, 10)) > 0) {
 		if (pfd[0].revents & POLLIN) {
 			if ((n = read(qfd, buf, sizeof(buf))) < 0) {
 				free(*qstats);
@@ -311,6 +312,9 @@ static int qmp_read(int qfd, unsigned char **qstats)
 			ptr[qsize] = 0;
 			*qstats = ptr;
 		}
+		memset(pfd, 0, sizeof(pfd));
+		pfd[0].fd = qfd;
+		pfd[0].events = POLLIN;
 	}
 	return 1;
 }
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 1/4] libxenstat: check xc_interface_open return value

In reply to Wei Liu
Quoted 4 lines · Wei Liu
Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>

The xenstat handle passed around already has an open xc_handle. That should be reused rather than opening a new one every time we want to get a list of domain ids.

~Andrew

Quoted 17 lines
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 2 ++
 1 file changed, 2 insertions(+)
tools/xenstat/libxenstat/src/xenstat_qmp.c +2 -0
index 2cb99e9..f3aeec4 100644
@@ -366,6 +366,8 @@ static xc_domaininfo_t *get_domain_ids(int *num_doms)
 	if (dominfo == NULL)
 		return NULL;
 	xc_handle = xc_interface_open(0,0,0);
+	if (xc_handle == NULL)
+		return NULL;
 	*num_doms = xc_domain_getinfolist(xc_handle, 0, 1024, dominfo);
 	xc_interface_close(xc_handle);
 	return dominfo;
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] libxenstat: YAJL_GET_STRING may return NULL

In reply to Wei Liu
Quoted 7 lines · Wei Liu

Passing NULL to strcmp can cause segmentation fault. Continue in that case.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
Quoted 17 lines
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xenstat/libxenstat/src/xenstat_qmp.c +1 -1
index f3aeec4..6bfbc2f 100644
@@ -110,7 +110,7 @@ static char *qmp_get_block_image(xenstat_node *node, char *qmp_devname, int qfd)
 		ptr[0] = qblock[QMP_BLOCK_DEVICE]; /* "device" */
 		if ((dev_obj = yajl_tree_get(n, ptr, yajl_t_any)) != NULL) {
 			tmp = YAJL_GET_STRING(dev_obj);
-			if (strcmp(qmp_devname, tmp))
+			if (!tmp || strcmp(qmp_devname, tmp))
 				continue;
 		}
 		else
#11 lore raw

Re: [PATCH 4/4] libxenstat: qmp_read fix and cleanup

In reply to Wei Liu
Quoted 37 lines · Wei Liu

The second argument of poll(2) is the number of file descriptors. POLLIN is defined as 1 so it happens to work.

Also do two cleanups while I was there:
1. There is only one fd, so a one-element array is enough.
2. Initialise pfd to make code linter happy.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/xenstat/libxenstat/src/xenstat_qmp.c +3 -2
index e4f343b..260cd34 100644
@@ -289,13 +289,14 @@ static size_t qmp_write(int qfd, char *cmd, size_t cmd_len)
 static int qmp_read(int qfd, unsigned char **qstats)
 {
 	unsigned char buf[1024], *ptr;
-	struct pollfd pfd[2];
+	struct pollfd pfd[1];
 	int n, qsize = 0;
 
 	*qstats = NULL;
+	memset(pfd, 0, sizeof(pfd));
 	pfd[0].fd = qfd;
 	pfd[0].events = POLLIN;
-	while ((n = poll(pfd, POLLIN, 10)) > 0) {
+	while ((n = poll(pfd, 1, 10)) > 0) {
 		if (pfd[0].revents & POLLIN) {
 			if ((n = read(qfd, buf, sizeof(buf))) < 0) {
 				free(*qstats);

tools/xenstat/libxenstat/src/xenstat_qmp.c static int qmp_read(int qfd, unsigned char **qstats) · line 317

Quoted 6 lines
tools/xenstat/libxenstat/src/xenstat_qmp.c +3 -0
@@ -311,6 +312,9 @@ static int qmp_read(int qfd, unsigned char **qstats)
 			ptr[qsize] = 0;
 			*qstats = ptr;
 		}
+		memset(pfd, 0, sizeof(pfd));
+		pfd[0].fd = qfd;
+		pfd[0].events = POLLIN;

I don't think this is necessary. .fd and .events should be untouched by poll().

Ignore the complaint about revents being uninitialised. That is because Coverity doesn't have a model for poll(), and doesn't know that poll() writes to revents.

I was going to find some copious free time to correctly model poll(), which will fix several related defects.

~Andrew

Quoted 3 lines
tools/xenstat/libxenstat/src/xenstat_qmp.c +0 -0
 	}
 	return 1;
 }
Acked-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] libxenstat: YAJL_GET_STRING may return NULL

In reply to Wei Liu

Wei Liu writes ("[PATCH 2/4] libxenstat: YAJL_GET_STRING may return NULL"):

Quoted 7 lines

Passing NULL to strcmp can cause segmentation fault. Continue in that case.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
author
#3 lore raw

Re: [PATCH 1/4] libxenstat: check xc_interface_open return value

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Wei Liu
Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>

The xenstat handle passed around already has an open xc_handle. That should be reused rather than opening a new one every time we want to get a list of domain ids.

Yes, you're right. I will use that.

Wei.

Quoted 19 lines

~Andrew

Quoted 17 lines
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 2 ++
 1 file changed, 2 insertions(+)
tools/xenstat/libxenstat/src/xenstat_qmp.c +2 -0
index 2cb99e9..f3aeec4 100644
@@ -366,6 +366,8 @@ static xc_domaininfo_t *get_domain_ids(int *num_doms)
 	if (dominfo == NULL)
 		return NULL;
 	xc_handle = xc_interface_open(0,0,0);
+	if (xc_handle == NULL)
+		return NULL;
 	*num_doms = xc_domain_getinfolist(xc_handle, 0, 1024, dominfo);
 	xc_interface_close(xc_handle);
 	return dominfo;
author
#12 lore raw

Re: [PATCH 4/4] libxenstat: qmp_read fix and cleanup

In reply to Andrew Cooper
Quoted 48 lines · Andrew Cooper
Quoted 37 lines · Wei Liu

The second argument of poll(2) is the number of file descriptors. POLLIN is defined as 1 so it happens to work.

Also do two cleanups while I was there:
1. There is only one fd, so a one-element array is enough.
2. Initialise pfd to make code linter happy.

Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Charles Arnold <[email protected]>
---
 tools/xenstat/libxenstat/src/xenstat_qmp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/xenstat/libxenstat/src/xenstat_qmp.c +3 -2
index e4f343b..260cd34 100644
@@ -289,13 +289,14 @@ static size_t qmp_write(int qfd, char *cmd, size_t cmd_len)
 static int qmp_read(int qfd, unsigned char **qstats)
 {
 	unsigned char buf[1024], *ptr;
-	struct pollfd pfd[2];
+	struct pollfd pfd[1];
 	int n, qsize = 0;
 
 	*qstats = NULL;
+	memset(pfd, 0, sizeof(pfd));
 	pfd[0].fd = qfd;
 	pfd[0].events = POLLIN;
-	while ((n = poll(pfd, POLLIN, 10)) > 0) {
+	while ((n = poll(pfd, 1, 10)) > 0) {
 		if (pfd[0].revents & POLLIN) {
 			if ((n = read(qfd, buf, sizeof(buf))) < 0) {
 				free(*qstats);

tools/xenstat/libxenstat/src/xenstat_qmp.c static int qmp_read(int qfd, unsigned char **qstats) · line 317

Quoted 6 lines
tools/xenstat/libxenstat/src/xenstat_qmp.c +3 -0
@@ -311,6 +312,9 @@ static int qmp_read(int qfd, unsigned char **qstats)
 			ptr[qsize] = 0;
 			*qstats = ptr;
 		}
+		memset(pfd, 0, sizeof(pfd));
+		pfd[0].fd = qfd;
+		pfd[0].events = POLLIN;

I don't think this is necessary. .fd and .events should be untouched by poll().

Quoted 4 lines

Ignore the complaint about revents being uninitialised. That is because Coverity doesn't have a model for poll(), and doesn't know that poll() writes to revents.

Sure.

Quoted 3 lines

I was going to find some copious free time to correctly model poll(), which will fix several related defects.

That would be good!

Wei.

Quoted 5 lines

~Andrew

Quoted 3 lines
tools/xenstat/libxenstat/src/xenstat_qmp.c +0 -0
 	}
 	return 1;
 }
Acked-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] libxenstat: always free qmp_stats

In reply to Wei Liu

Wei Liu writes ("[PATCH 3/4] libxenstat: always free qmp_stats"):

Quoted 5 lines

Originally qmp_stats is only freed in failure path and leaked in success path.

Instead of wiring up the success path, rearrange the code a bit to always free qmp_stats before checking if info is NULL.

Having said that, I am not a great fan of the error handling cleanup style adopted in this code in general. The ownership and lifetime of the various allocated objects has to be inferred by reading the code in detail.

Ian.

#13 lore raw

Re: [PATCH 4/4] libxenstat: qmp_read fix and cleanup

In reply to Andrew Cooper

Andrew Cooper writes ("Re: [PATCH 4/4] libxenstat: qmp_read fix and cleanup"):

Quoted 3 lines · Wei Liu
Quoted 2 lines

The second argument of poll(2) is the number of file descriptors. POLLIN is defined as 1 so it happens to work.

...

Quoted 10 lines

tools/xenstat/libxenstat/src/xenstat_qmp.c static int qmp_read(int qfd, unsigned char **qstats) · line 317

Quoted 7 lines
tools/xenstat/libxenstat/src/xenstat_qmp.c +3 -0
@@ -311,6 +312,9 @@ static int qmp_read(int qfd, unsigned char **qstats)
 			ptr[qsize] = 0;
 			*qstats = ptr;
 		}
+		memset(pfd, 0, sizeof(pfd));
+		pfd[0].fd = qfd;
+		pfd[0].events = POLLIN;

I don't think this is necessary. .fd and .events should be untouched by poll().

Indeed.

Quoted 3 lines

Ignore the complaint about revents being uninitialised. That is because Coverity doesn't have a model for poll(), and doesn't know that poll() writes to revents.

I would be happy with the patch either with or without the memset.

Ian.

author
#9 lore raw

Re: [PATCH 3/4] libxenstat: always free qmp_stats

In reply to Ian Jackson
Quoted 7 lines · Ian Jackson

Wei Liu writes ("[PATCH 3/4] libxenstat: always free qmp_stats"):

Quoted 5 lines

Originally qmp_stats is only freed in failure path and leaked in success path.

Instead of wiring up the success path, rearrange the code a bit to always free qmp_stats before checking if info is NULL.

Quoted 7 lines

Having said that, I am not a great fan of the error handling cleanup style adopted in this code in general. The ownership and lifetime of the various allocated objects has to be inferred by reading the code in detail.

Yeah... I'm with you. On the other hand I want my bug fix patch to be as small as possible. Changing this function to goto style will make the patch a lot bigger. I think we can clean this function up with another patch if anyone feels keen enough.

Wei.

Quoted 1 line

Ian.