Re: [libvirt PATCH 10/21] tests: Use glib memory function in testConfRoundTrip

2020-09-11 Thread Ján Tomko

On a Friday in 2020, Tim Wiederhake wrote:

This also fixes a (hypothetical) bug where testConfRoundTrip would return 0 if
virConfWriteMem() returned 0 and virTestCompareToFile failed.



The bug is not hypothetical, it renders all the tests using testConfRoundTrip
useless since they always pass even if the functionality they're
supposed to be testing give a different output.

Since it's a real bug, I think it deserves a separate commit.


Signed-off-by: Tim Wiederhake 
---
tests/virconftest.c | 29 ++---
1 file changed, 10 insertions(+), 19 deletions(-)

diff --git a/tests/virconftest.c b/tests/virconftest.c
index ab29b5b712..c683344f49 100644
--- a/tests/virconftest.c
+++ b/tests/virconftest.c
@@ -32,40 +32,31 @@
static int testConfRoundTrip(const void *opaque)
{
const char *name = opaque;
-int ret = -1;
g_autoptr(virConf) conf = NULL;
int len = 1;
-char *buffer = NULL;
-char *srcfile = NULL;
-char *dstfile = NULL;
+g_autofree char *buffer = NULL;
+g_autofree char *srcfile = NULL;
+g_autofree char *dstfile = NULL;

srcfile = g_strdup_printf("%s/virconfdata/%s.conf", abs_srcdir, name);
dstfile = g_strdup_printf("%s/virconfdata/%s.out", abs_srcdir, name);

-if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
-fprintf(stderr, "out of memory\n");
-goto cleanup;
-}
+buffer = g_new0(char, len);
conf = virConfReadFile(srcfile, 0);
if (conf == NULL) {
fprintf(stderr, "Failed to process %s\n", srcfile);
-goto cleanup;
+return -1;
}



-ret = virConfWriteMem(buffer, &len, conf);
-if (ret < 0) {
+
+if (virConfWriteMem(buffer, &len, conf) < 0) {


With this change separated.

Reviewed-by: Ján Tomko 

Jano


fprintf(stderr, "Failed to serialize %s back\n", srcfile);
-goto cleanup;
+return -1;
}

if (virTestCompareToFile(buffer, dstfile) < 0)
-goto cleanup;
+return -1;

-ret = 0;
- cleanup:
-VIR_FREE(srcfile);
-VIR_FREE(dstfile);
-VIR_FREE(buffer);
-return ret;
+return 0;
}


--
2.26.2



signature.asc
Description: PGP signature


[libvirt PATCH 10/21] tests: Use glib memory function in testConfRoundTrip

2020-09-11 Thread Tim Wiederhake
This also fixes a (hypothetical) bug where testConfRoundTrip would return 0 if
virConfWriteMem() returned 0 and virTestCompareToFile failed.

Signed-off-by: Tim Wiederhake 
---
 tests/virconftest.c | 29 ++---
 1 file changed, 10 insertions(+), 19 deletions(-)

diff --git a/tests/virconftest.c b/tests/virconftest.c
index ab29b5b712..c683344f49 100644
--- a/tests/virconftest.c
+++ b/tests/virconftest.c
@@ -32,40 +32,31 @@
 static int testConfRoundTrip(const void *opaque)
 {
 const char *name = opaque;
-int ret = -1;
 g_autoptr(virConf) conf = NULL;
 int len = 1;
-char *buffer = NULL;
-char *srcfile = NULL;
-char *dstfile = NULL;
+g_autofree char *buffer = NULL;
+g_autofree char *srcfile = NULL;
+g_autofree char *dstfile = NULL;
 
 srcfile = g_strdup_printf("%s/virconfdata/%s.conf", abs_srcdir, name);
 dstfile = g_strdup_printf("%s/virconfdata/%s.out", abs_srcdir, name);
 
-if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
-fprintf(stderr, "out of memory\n");
-goto cleanup;
-}
+buffer = g_new0(char, len);
 conf = virConfReadFile(srcfile, 0);
 if (conf == NULL) {
 fprintf(stderr, "Failed to process %s\n", srcfile);
-goto cleanup;
+return -1;
 }
-ret = virConfWriteMem(buffer, &len, conf);
-if (ret < 0) {
+
+if (virConfWriteMem(buffer, &len, conf) < 0) {
 fprintf(stderr, "Failed to serialize %s back\n", srcfile);
-goto cleanup;
+return -1;
 }
 
 if (virTestCompareToFile(buffer, dstfile) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
- cleanup:
-VIR_FREE(srcfile);
-VIR_FREE(dstfile);
-VIR_FREE(buffer);
-return ret;
+return 0;
 }
 
 
-- 
2.26.2