[augeas-devel] [PATCH v2] don't use variables uninitialized upon error

Jim Meyering jim at meyering.net
Fri Dec 23 22:32:35 UTC 2011


Jim Meyering wrote:
> Building with gcc-4.7.0..., I saw this:
>
>     transform.c: In function 'transform_load':
>     transform.c:697:5: warning: 'pathc' may be used uninitialized in
>       this function [-Wmaybe-uninitialized]
>
> That warning is legit, since the function looks like this:
>
>     ...
>     list_for_each(f, xfm->children) {
>         ...
>         if (r != 0 && r != GLOB_NOMATCH) {
>             ret = -1;
>             goto error;
>         }
>         gl_flags |= GLOB_APPEND;
>     }
>
>     ...
>
>     char **pathv = NULL;
>     int pathc = globbuf.gl_pathc, pathind = 0;
>
>     ...
>
>  error:
>     if (pathv != NULL)
>         for (int i=0; i < pathc; i++)
>             free(pathv[i]);
>     free(pathv);
>     ret = -1;
>     goto done;
> }
>
> So control can reach the uses of pathv and pathc without
> before they are initialized.

Oops.
That patch was not right.  It moved the use of globbuf up to before set.
This one is better:


>From 8f5ef285778017f3f1b2a0b9fdd9c298060cec27 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Fri, 23 Dec 2011 23:30:45 +0100
Subject: [PATCH] don't use variables uninitialized upon error

* src/transform.c (filter_generate): Move declarations and
initializations of pathv and pathc to precede possible
"goto error;" via which those variables are used.
---
 src/transform.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/transform.c b/src/transform.c
index b36b944..4d55184 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -179,6 +179,8 @@ static int filter_generate(struct tree *xfm, const char *root,
     int gl_flags = glob_flags;
     int r;
     int ret = 0;
+    char **pathv = NULL;
+    int pathc = 0;

     *nmatches = 0;
     *matches = NULL;
@@ -199,8 +201,8 @@ static int filter_generate(struct tree *xfm, const char *root,
         gl_flags |= GLOB_APPEND;
     }

-    char **pathv = NULL;
-    int pathc = globbuf.gl_pathc, pathind = 0;
+    pathc = globbuf.gl_pathc;
+    int pathind = 0;

     if (ALLOC_N(pathv, pathc) < 0)
         goto error;
--
1.7.8.1.367.g25ecc




More information about the augeas-devel mailing list