[Libguestfs] [PATCH] When calling getline first time, initialize length to zero.

Richard W.M. Jones rjones at redhat.com
Thu May 14 12:24:20 UTC 2015


The man page for getline says:

   ssize_t getline(char **lineptr, size_t *n, FILE *stream);
 [...]
   If  *lineptr  is set to NULL and *n is set 0 before the call, then get‐
   line() will allocate a buffer for storing the line.  This buffer should
   be freed by the user program even if getline() failed.

which seems to indicate that we must initialize both line and len to 0
before the first call to getline.

In several places we were not initializing len.  The program still
worked fine, but it seems better to initialize the length anyway.
---
 df/estimate-max-threads.c | 2 +-
 examples/libvirt-auth.c   | 2 +-
 fuse/test-fuse.c          | 2 +-
 p2v/main.c                | 4 ++--
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/df/estimate-max-threads.c b/df/estimate-max-threads.c
index 7d5b090..ca1fd42 100644
--- a/df/estimate-max-threads.c
+++ b/df/estimate-max-threads.c
@@ -60,7 +60,7 @@ read_line_from (const char *cmd)
 {
   FILE *pp;
   char *ret = NULL;
-  size_t allocsize;
+  size_t allocsize = 0;
 
   pp = popen (cmd, "r");
   if (pp == NULL)
diff --git a/examples/libvirt-auth.c b/examples/libvirt-auth.c
index 699dd87..8a5db77 100644
--- a/examples/libvirt-auth.c
+++ b/examples/libvirt-auth.c
@@ -109,7 +109,7 @@ auth_callback (guestfs_h *g,
   size_t i;
   char *prompt;
   char *reply = NULL;
-  size_t allocsize;
+  size_t allocsize = 0;
   char *pass;
   ssize_t len;
   int r;
diff --git a/fuse/test-fuse.c b/fuse/test-fuse.c
index 84d58e8..6e91d71 100644
--- a/fuse/test-fuse.c
+++ b/fuse/test-fuse.c
@@ -250,7 +250,7 @@ test_fuse (void)
   fflush (stdout)
   FILE *fp;
   char *line = NULL;
-  size_t len;
+  size_t len = 0;
   struct stat statbuf;
   char buf[128];
   ssize_t r;
diff --git a/p2v/main.c b/p2v/main.c
index 15628ba..1f8370b 100644
--- a/p2v/main.c
+++ b/p2v/main.c
@@ -306,7 +306,7 @@ partition_parent (dev_t part_dev)
 {
   CLEANUP_FCLOSE FILE *fp = NULL;
   CLEANUP_FREE char *path = NULL, *content = NULL;
-  size_t len;
+  size_t len = 0;
   unsigned parent_major, parent_minor;
 
   if (asprintf (&path, "/sys/dev/block/%d:%d/../dev",
@@ -521,7 +521,7 @@ read_cmdline (void)
 {
   CLEANUP_FCLOSE FILE *fp = NULL;
   char *ret = NULL;
-  size_t len;
+  size_t len = 0;
 
   fp = fopen ("/proc/cmdline", "re");
   if (fp == NULL) {
-- 
2.3.1




More information about the Libguestfs mailing list