Skip to contentSkip to conversation

qtest.c: Allow zero size in memset qtest commands

PATCH In review Peter Maydell 2 participants 4 messages Aug 5, 2016 – Sep 9, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qtest.c: Allow zero size in memset qtest commands

Some tests use the qtest protocol "memset" command with a zero size, expecting it to do nothing. However in the current code this will result in calling memset() with a NULL pointer, which is undefined behaviour. Detect and specially handle zero sizes to avoid this.

---
Looking at the code for the other commands that take a size ('read', 'write', 'b64read' and 'b64write' they all assume a non-zero size. I've left those alone though, somebody else can make them do nothing on zero size if they feel it's important.)

 qtest.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
qtest.c +7 -4
index da4826c..ce4c6db 100644
@@ -133,6 +133,7 @@ static bool qtest_opened;
  *  < OK
  *
  * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
+ * For 'memset' a zero size is permitted and does nothing.
  *
  * DATA is an arbitrarily long hex number prefixed with '0x'.  If it's smaller
  * than the expected size, the value will be zero filled at the end of the data
@@ -493,10 +494,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
         len = strtoull(words[2], NULL, 0);
         pattern = strtoull(words[3], NULL, 0);
 
-        data = g_malloc(len);
-        memset(data, pattern, len);
-        cpu_physical_memory_write(addr, data, len);
-        g_free(data);
+        if (len) {
+            data = g_malloc(len);
+            memset(data, pattern, len);
+            cpu_physical_memory_write(addr, data, len);
+            g_free(data);
+        }
 
         qtest_send_prefix(chr);
         qtest_send(chr, "OK\n");
Signature · 1 line
2.7.4
#1 lore raw

Ping?

(Now that 2.7 is out it would be nice to get rid of the clang warnings cluttering up my build logs :-))

thanks
-- PMM

Quoted 47 lines · Peter Maydell

Some tests use the qtest protocol "memset" command with a zero size, expecting it to do nothing. However in the current code this will result in calling memset() with a NULL pointer, which is undefined behaviour. Detect and specially handle zero sizes to avoid this.

---
Looking at the code for the other commands that take a size ('read', 'write', 'b64read' and 'b64write' they all assume a non-zero size. I've left those alone though, somebody else can make them do nothing on zero size if they feel it's important.)

 qtest.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
qtest.c +7 -5
index da4826c..ce4c6db 100644
@@ -133,6 +133,7 @@ static bool qtest_opened;
  *  < OK
  *
  * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
+ * For 'memset' a zero size is permitted and does nothing.
  *
  * DATA is an arbitrarily long hex number prefixed with '0x'.  If it's smaller
  * than the expected size, the value will be zero filled at the end of the data
@@ -493,10 +494,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
         len = strtoull(words[2], NULL, 0);
         pattern = strtoull(words[3], NULL, 0);
 
-        data = g_malloc(len);
-        memset(data, pattern, len);
-        cpu_physical_memory_write(addr, data, len);
-        g_free(data);
+        if (len) {
+            data = g_malloc(len);
+            memset(data, pattern, len);
+            cpu_physical_memory_write(addr, data, len);
+            g_free(data);
+        }
 
         qtest_send_prefix(chr);
         qtest_send(chr, "OK\n");
--

2.7.4

Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Some tests use the qtest protocol "memset" command with a zero size, expecting it to do nothing. However in the current code this will result in calling memset() with a NULL pointer, which is undefined behaviour. Detect and specially handle zero sizes to avoid this.

Quoted 6 lines

---
Looking at the code for the other commands that take a size ('read', 'write', 'b64read' and 'b64write' they all assume a non-zero size. I've left those alone though, somebody else can make them do nothing on zero size if they feel it's important.)

I obviously missed reviewing this in time for 2.7, but looks reasonable to me.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author Applied
#3 lore raw
Quoted 14 lines · Eric Blake
Quoted 6 lines · Peter Maydell

Some tests use the qtest protocol "memset" command with a zero size, expecting it to do nothing. However in the current code this will result in calling memset() with a NULL pointer, which is undefined behaviour. Detect and specially handle zero sizes to avoid this.

Quoted 6 lines

---
Looking at the code for the other commands that take a size ('read', 'write', 'b64read' and 'b64write' they all assume a non-zero size. I've left those alone though, somebody else can make them do nothing on zero size if they feel it's important.)

Quoted 4 lines

I obviously missed reviewing this in time for 2.7, but looks reasonable to me.

Applied to master, thanks.

-- PMM