[Libvir] avoid reading uninitialized memory in test program

Jim Meyering jim at meyering.net
Wed Oct 17 11:34:21 UTC 2007


valgrind complained about the printf in conftest.c.
Here's the fix:

	* src/conf.c (__virConfWriteMem): Fix typos in comment.
	* tests/conftest.c (main): Use fwrite, not printf, since the
	result buffer is not NUL-terminated.

===================================================================
RCS file: /data/cvs/libvirt/src/conf.c,v
retrieving revision 1.11
diff -u -p -r1.11 conf.c
--- src/conf.c	4 Jul 2007 09:16:23 -0000	1.11
+++ src/conf.c	17 Oct 2007 11:31:54 -0000
@@ -1,7 +1,7 @@
 /**
  * conf.c: parser for a subset of the Python encoded Xen configuration files
  *
- * Copyright (C) 2006 Red Hat, Inc.
+ * Copyright (C) 2006, 2007 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -916,7 +916,7 @@ error:
 /**
  * __virConfWriteMem:
  * @memory: pointer to the memory to store the config file
- * @len: pointer to the lenght in byte of the store, on output the size
+ * @len: pointer to the length in bytes of the store, on output the size
  * @conf: the conf
  *
  * Writes a configuration file back to a memory area. @len is an IN/OUT
===================================================================
RCS file: /data/cvs/libvirt/tests/conftest.c,v
retrieving revision 1.1
diff -u -p -r1.1 conftest.c
--- tests/conftest.c	29 Aug 2006 22:27:07 -0000	1.1
+++ tests/conftest.c	17 Oct 2007 11:31:54 -0000
@@ -24,7 +24,10 @@ int main(int argc, char **argv) {
         fprintf(stderr, "Failed to serialize %s back\n", argv[1]);
 	exit(3);
     }
-    printf("%s", buffer);
     virConfFree(conf);
+    if (fwrite(buffer, len, 1, stdout) != len) {
+	fprintf(stderr, "Write failed\n");
+	exit(1);
+    }
     exit(0);
 }




More information about the libvir-list mailing list