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

Jim Meyering jim at meyering.net
Fri Dec 23 21:55:45 UTC 2011


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.


>From 011238cfb90be0404090c1037fd2757f140d2e06 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Fri, 23 Dec 2011 22:50:24 +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 |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/src/transform.c b/src/transform.c
index b36b944..de47eb3 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 = globbuf.gl_pathc, pathind = 0;

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

-    char **pathv = NULL;
-    int pathc = globbuf.gl_pathc, pathind = 0;
-
     if (ALLOC_N(pathv, pathc) < 0)
         goto error;

--
1.7.8.1.367.g25ecc




More information about the augeas-devel mailing list