Skip to contentSkip to conversation

qemu-config: Accept empty option values

PATCH Superseded Eduardo Habkost 3 participants 5 messages Apr 8, 2015 – Apr 9, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-config: Accept empty option values

Currently it is impossible to set an option in a config file to an empty string, because the parser matches only lines containing non-empty strings between double-quotes.

As sscanf() "[" conversion specifier only matches non-empty strings, add a special case for empty strings.

---
 util/qemu-config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/qemu-config.c +2 -1
index 2d32ce7..9f9577d 100644
@@ -413,7 +413,8 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
             opts = qemu_opts_create(list, NULL, 0, &error_abort);
             continue;
         }
-        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
+        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
+            (value[0] = '\0', sscanf(line, " %63s = \"\"", arg) == 1)) {
             /* arg = value */
             if (opts == NULL) {
                 error_report("no group defined");
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Eduardo Habkost

Currently it is impossible to set an option in a config file to an empty string, because the parser matches only lines containing non-empty strings between double-quotes.

As sscanf() "[" conversion specifier only matches non-empty strings, add a special case for empty strings.

I avoid sscanf() as a rule (as it's behavior on %d is undefined in the face of malicious input), so I had to read the man page; but you are right. Libvirt is trying to completely ban use of *scanf for that reason; but obviously qemu is not quite so opposed to it.

Quoted 11 lines
---
 util/qemu-config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/qemu-config.c +0 -0
index 2d32ce7..9f9577d 100644

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) · line 417

Quoted 6 lines
util/qemu-config.c +2 -1
@@ -413,7 +413,8 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
             opts = qemu_opts_create(list, NULL, 0, &error_abort);
             continue;
         }
-        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
+        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
+            (value[0] = '\0', sscanf(line, " %63s = \"\"", arg) == 1)) {

This is one of the few times I've seen sscanf used in a well-defined manner (albeit still arbitrarily limiting, in that we have fixed-size buffers) - but having to rely on a comma operator to get there makes this look quite arcane. I still wonder if hand-rolling a real scanner would beat the compactness of sscanf by making the code intentions a little more discernible, and have the benefits of avoiding my sscanf red-flag checker. But my wonder is not enough to stop me from accepting this hack as-is.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Acked-by on v1 1/1
#2 lore raw
In reply to Eduardo Habkost
Quoted 18 lines · Eduardo Habkost

Currently it is impossible to set an option in a config file to an empty string, because the parser matches only lines containing non-empty strings between double-quotes.

As sscanf() "[" conversion specifier only matches non-empty strings, add a special case for empty strings.

---
 util/qemu-config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/qemu-config.c +0 -0
index 2d32ce7..9f9577d 100644

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) · line 420

Quoted 9 lines
util/qemu-config.c +2 -1
@@ -413,7 +413,8 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
             opts = qemu_opts_create(list, NULL, 0, &error_abort);
             continue;
         }
-        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
+        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
+            (value[0] = '\0', sscanf(line, " %63s = \"\"", arg) == 1)) {
             /* arg = value */
             if (opts == NULL) {
                 error_report("no group defined");
#3 lore raw
Quoted 22 lines · Paolo Bonzini
Quoted 21 lines · Eduardo Habkost

Currently it is impossible to set an option in a config file to an empty string, because the parser matches only lines containing non-empty strings between double-quotes.

As sscanf() "[" conversion specifier only matches non-empty strings, add a special case for empty strings.

---
 util/qemu-config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/qemu-config.c +0 -1
index 2d32ce7..9f9577d 100644
@@ -413,7 +413,8 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
             opts = qemu_opts_create(list, NULL, 0, &error_abort);
             continue;
         }
-        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
Quoted 8 lines

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) · line 420

Quoted 6 lines
util/qemu-config.c +2 -0
+        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
+            (value[0] = '\0', sscanf(line, " %63s = \"\"", arg) == 1)) {
             /* arg = value */
             if (opts == NULL) {
                 error_report("no group defined");

Thanks! Whose tree this should go through?

Signature · 1 line
Eduardo
#4 lore raw
Quoted 32 lines · Eduardo Habkost
Quoted 22 lines · Paolo Bonzini
Quoted 21 lines · Eduardo Habkost

Currently it is impossible to set an option in a config file to an empty string, because the parser matches only lines containing non-empty strings between double-quotes.

As sscanf() "[" conversion specifier only matches non-empty strings, add a special case for empty strings.

---
 util/qemu-config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/qemu-config.c +0 -1
index 2d32ce7..9f9577d 100644
@@ -413,7 +413,8 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
             opts = qemu_opts_create(list, NULL, 0, &error_abort);
             continue;
         }
-        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
Quoted 8 lines

util/qemu-config.c int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) · line 420

Quoted 6 lines
util/qemu-config.c +2 -0
+        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
+            (value[0] = '\0', sscanf(line, " %63s = \"\"", arg) == 1)) {
             /* arg = value */
             if (opts == NULL) {
                 error_report("no group defined");
Quoted 1 line

Thanks! Whose tree this should go through?

Acked-by generally means that you're welcome to take it through yours.

My "misc" tree hosts non-trivial patches outside well-defined maintainership area, but it's less work to carry patches only from people who don't send pull requests. Maybe it's not the case of this patch, but using your own tree also means that it's simpler for you to sort out the dependencies.

Paolo