Modify code to use VIR_AUTOFREE macro wherever required.
Signed-off-by: Sukrit Bhatnagar <skrtbhtngr(a)gmail.com>
---
src/util/virconf.c | 42 ++++++++++++------------------------------
1 file changed, 12 insertions(+), 30 deletions(-)
diff --git a/src/util/virconf.c b/src/util/virconf.c
index e0a3fd1..7dd8820 100644
--- a/src/util/virconf.c
+++ b/src/util/virconf.c
@@ -745,9 +745,8 @@ virConfParse(const char *filename, const char *content, int len,
virConfPtr
virConfReadFile(const char *filename, unsigned int flags)
{
- char *content;
+ VIR_AUTOFREE(char *) content = NULL;
int len;
- virConfPtr conf;
VIR_DEBUG("filename=%s", NULLSTR(filename));
@@ -759,11 +758,7 @@ virConfReadFile(const char *filename, unsigned int flags)
if ((len = virFileReadAll(filename, MAX_CONFIG_FILE_SIZE, &content)) < 0)
return NULL;
- conf = virConfParse(filename, content, len, flags);
-
- VIR_FREE(content);
-
- return conf;
+ return virConfParse(filename, content, len, flags);
}
/**
@@ -1451,7 +1446,7 @@ virConfWriteFile(const char *filename, virConfPtr conf)
virConfEntryPtr cur;
int ret;
int fd;
- char *content;
+ VIR_AUTOFREE(char *) content = NULL;
unsigned int use;
if (conf == NULL)
@@ -1476,7 +1471,6 @@ virConfWriteFile(const char *filename, virConfPtr conf)
use = virBufferUse(&buf);
content = virBufferContentAndReset(&buf);
ret = safewrite(fd, content, use);
- VIR_FREE(content);
VIR_FORCE_CLOSE(fd);
if (ret != (int)use) {
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"));
@@ -1504,7 +1498,7 @@ virConfWriteMem(char *memory, int *len, virConfPtr conf)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
virConfEntryPtr cur;
- char *content;
+ VIR_AUTOFREE(char *) content = NULL;
unsigned int use;
if ((memory == NULL) || (len == NULL) || (*len <= 0) || (conf == NULL))
@@ -1524,11 +1518,9 @@ virConfWriteMem(char *memory, int *len, virConfPtr conf)
if ((int)use >= *len) {
*len = (int)use;
- VIR_FREE(content);
return -1;
}
memcpy(memory, content, use);
- VIR_FREE(content);
*len = use;
return use;
}
@@ -1542,16 +1534,13 @@ virConfLoadConfigPath(const char *name)
SYSCONFDIR, name) < 0)
return NULL;
} else {
- char *userdir = virGetUserConfigDirectory();
+ VIR_AUTOFREE(char *) userdir = virGetUserConfigDirectory();
if (!userdir)
return NULL;
if (virAsprintf(&path, "%s/%s",
- userdir, name) < 0) {
- VIR_FREE(userdir);
+ userdir, name) < 0)
return NULL;
- }
- VIR_FREE(userdir);
}
return path;
@@ -1560,26 +1549,19 @@ virConfLoadConfigPath(const char *name)
int
virConfLoadConfig(virConfPtr *conf, const char *name)
{
- char *path = NULL;
- int ret = -1;
+ VIR_AUTOFREE(char *) path = NULL;
*conf = NULL;
if (!(path = virConfLoadConfigPath(name)))
- goto cleanup;
+ return -1;
- if (!virFileExists(path)) {
- ret = 0;
- goto cleanup;
- }
+ if (!virFileExists(path))
+ return 0;
VIR_DEBUG("Loading config file '%s'", path);
if (!(*conf = virConfReadFile(path, 0)))
- goto cleanup;
-
- ret = 0;
+ return -1;
- cleanup:
- VIR_FREE(path);
- return ret;
+ return 0;
}
--
1.8.3.1