From 8b0e845d6757f034277fe7161b54ff441995687e Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 2 Mar 2021 12:04:41 +0100 Subject: [PATCH] xenParseSxprSound: Refactor parsing of model list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copy the input string so that we don't have to use a static buffer and virStrncpy. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/libxl/xen_common.c | 44 +++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c index 781483f496..c56815d7fc 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -1376,38 +1376,30 @@ xenParseSxprSound(virDomainDefPtr def, def->sounds[def->nsounds++] = sound; } } else { - char model[10]; - const char *offset = str, *offset2; + g_autofree char *sounds = g_strdup(str); + char *sound = sounds; + int model; - 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); + while (*sound != '\0') { + char *next = strchr(sound, ','); + virDomainSoundDefPtr snddef; + + if (next) + *next = '\0'; + + if ((model = virDomainSoundModelTypeFromString(sound)) < 0) return -1; - } - sound = g_new0(virDomainSoundDef, 1); + snddef = g_new0(virDomainSoundDef, 1); + snddef->model = model; - if ((sound->model = virDomainSoundModelTypeFromString(model)) < 0) { - VIR_FREE(sound); - return -1; - } + ignore_value(VIR_APPEND_ELEMENT(def->sounds, def->nsounds, snddef)); - if (VIR_APPEND_ELEMENT(def->sounds, def->nsounds, sound) < 0) { - virDomainSoundDefFree(sound); - return -1; - } + if (!next) + break; - offset = offset2 ? offset2 + 1 : NULL; - } while (offset); + sound = next + 1; + } } return 0;