xenParseSxprSound: Refactor parsing of model list

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>
This commit is contained in:
Peter Krempa 2021-03-02 12:04:41 +01:00
parent cd217e702c
commit 8b0e845d67

View File

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