util: storageencryption: Refactor cleanup section in virStorageEncryptionParseXML

The function used the 'cleanup' label only in error cases. This patch
makes the code pass the cleanup label in every case and removes few
unnecessary VIR_FREEs.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Peter Krempa 2018-03-06 14:57:17 +01:00
parent e7c44b3f7c
commit 15948e6266

View File

@ -246,13 +246,14 @@ static virStorageEncryptionPtr
virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
{
xmlNodePtr *nodes = NULL;
virStorageEncryptionPtr ret;
virStorageEncryptionPtr encdef = NULL;
virStorageEncryptionPtr ret = NULL;
char *format_str = NULL;
int n;
size_t i;
if (VIR_ALLOC(ret) < 0)
return NULL;
if (VIR_ALLOC(encdef) < 0)
goto cleanup;
if (!(format_str = virXPathString("string(./@format)", ctxt))) {
virReportError(VIR_ERR_XML_ERROR, "%s",
@ -260,60 +261,57 @@ virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
goto cleanup;
}
if ((ret->format =
if ((encdef->format =
virStorageEncryptionFormatTypeFromString(format_str)) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown volume encryption format type %s"),
format_str);
goto cleanup;
}
VIR_FREE(format_str);
if ((n = virXPathNodeSet("./secret", ctxt, &nodes)) < 0)
goto cleanup;
if (n > 0) {
if (VIR_ALLOC_N(ret->secrets, n) < 0)
if (VIR_ALLOC_N(encdef->secrets, n) < 0)
goto cleanup;
ret->nsecrets = n;
encdef->nsecrets = n;
for (i = 0; i < n; i++) {
if (!(ret->secrets[i] =
if (!(encdef->secrets[i] =
virStorageEncryptionSecretParse(ctxt, nodes[i])))
goto cleanup;
}
VIR_FREE(nodes);
}
if (ret->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
if (encdef->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
xmlNodePtr tmpnode;
if ((tmpnode = virXPathNode("./cipher[1]", ctxt))) {
if (virStorageEncryptionInfoParseCipher(tmpnode, &ret->encinfo) < 0)
if (virStorageEncryptionInfoParseCipher(tmpnode, &encdef->encinfo) < 0)
goto cleanup;
}
if ((tmpnode = virXPathNode("./ivgen[1]", ctxt))) {
/* If no cipher node, then fail */
if (!ret->encinfo.cipher_name) {
if (!encdef->encinfo.cipher_name) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("ivgen element found, but cipher is missing"));
goto cleanup;
}
if (virStorageEncryptionInfoParseIvgen(tmpnode, &ret->encinfo) < 0)
if (virStorageEncryptionInfoParseIvgen(tmpnode, &encdef->encinfo) < 0)
goto cleanup;
}
}
return ret;
VIR_STEAL_PTR(ret, encdef);
cleanup:
VIR_FREE(format_str);
VIR_FREE(nodes);
virStorageEncryptionFree(ret);
return NULL;
virStorageEncryptionFree(encdef);
return ret;
}
virStorageEncryptionPtr