esx: use g_steal_pointer+g_autofree on return value

If we put the potential return string into the g_autofreed tmpResult,
and the move it to the returned "result" only as a final step ater, we
can avoid the need to explicitly VIR_FREE (or g_free) on failure.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Laine Stump 2021-02-12 14:21:36 -05:00
parent 443c79dd7f
commit 33d3ebff71

View File

@ -275,7 +275,7 @@ esxParseVMXFileName(const char *fileName,
static char * static char *
esxFormatVMXFileName(const char *fileName, void *opaque) esxFormatVMXFileName(const char *fileName, void *opaque)
{ {
bool success = false; g_autofree char *tmpResult = NULL;
char *result = NULL; char *result = NULL;
esxVMX_Data *data = opaque; esxVMX_Data *data = opaque;
g_autofree char *datastoreName = NULL; g_autofree char *datastoreName = NULL;
@ -329,10 +329,10 @@ esxFormatVMXFileName(const char *fileName, void *opaque)
virBufferAddChar(&buffer, separator); virBufferAddChar(&buffer, separator);
virBufferAdd(&buffer, directoryAndFileName, -1); virBufferAdd(&buffer, directoryAndFileName, -1);
result = virBufferContentAndReset(&buffer); tmpResult = virBufferContentAndReset(&buffer);
} else if (*fileName == '/') { } else if (*fileName == '/') {
/* FIXME: need to deal with Windows paths here too */ /* FIXME: need to deal with Windows paths here too */
result = g_strdup(fileName); tmpResult = g_strdup(fileName);
} else { } else {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Could not handle file name '%s'"), fileName); _("Could not handle file name '%s'"), fileName);
@ -341,15 +341,11 @@ esxFormatVMXFileName(const char *fileName, void *opaque)
/* FIXME: Check if referenced path/file really exists */ /* FIXME: Check if referenced path/file really exists */
success = true; result = g_steal_pointer(&tmpResult);
cleanup: cleanup:
if (! success)
VIR_FREE(result);
esxVI_ObjectContent_Free(&datastore); esxVI_ObjectContent_Free(&datastore);
esxVI_DatastoreHostMount_Free(&hostMount); esxVI_DatastoreHostMount_Free(&hostMount);
return result; return result;
} }