]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
xenParseSxprSound: Refactor parsing of model list
authorPeter Krempa <pkrempa@redhat.com>
Tue, 2 Mar 2021 11:04:41 +0000 (12:04 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 11 Mar 2021 17:00:35 +0000 (18:00 +0100)
Copy the input string so that we don't have to use a static buffer and
virStrncpy.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libxl/xen_common.c

index 781483f49695907b5e5f07114b6749533060948f..c56815d7fcbb32a0475eae9465dd54498795d723 100644 (file)
@@ -1376,38 +1376,30 @@ xenParseSxprSound(virDomainDefPtr def,
             def->sounds[def->nsounds++] = sound;
         }
     } else {
-        char model[10];
-        const char *offset = str, *offset2;
-
-        do {
-            int len;
-            virDomainSoundDefPtr sound;
-            offset2 = strchr(offset, ',');
-            if (offset2)
-                len = (offset2 - offset);
-            else
-                len = strlen(offset);
-            if (virStrncpy(model, offset, len, sizeof(model)) < 0) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("Sound model %s too big for destination"),
-                               offset);
-                return -1;
-            }
+        g_autofree char *sounds = g_strdup(str);
+        char *sound = sounds;
+        int model;
 
-            sound = g_new0(virDomainSoundDef, 1);
+        while (*sound != '\0') {
+            char *next = strchr(sound, ',');
+            virDomainSoundDefPtr snddef;
 
-            if ((sound->model = virDomainSoundModelTypeFromString(model)) < 0) {
-                VIR_FREE(sound);
-                return -1;
-            }
+            if (next)
+                *next = '\0';
 
-            if (VIR_APPEND_ELEMENT(def->sounds, def->nsounds, sound) < 0) {
-                virDomainSoundDefFree(sound);
+            if ((model = virDomainSoundModelTypeFromString(sound)) < 0)
                 return -1;
-            }
 
-            offset = offset2 ? offset2 + 1 : NULL;
-        } while (offset);
+            snddef = g_new0(virDomainSoundDef, 1);
+            snddef->model = model;
+
+            ignore_value(VIR_APPEND_ELEMENT(def->sounds, def->nsounds, snddef));
+
+            if (!next)
+                break;
+
+            sound = next + 1;
+        }
     }
 
     return 0;