]> granicus.if.org Git - procps-ng/commitdiff
sysctl: fix potential null derefence [smatch scan]
authorSami Kerola <kerolasa@iki.fi>
Sun, 5 Feb 2012 20:02:34 +0000 (21:02 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 5 Feb 2012 20:02:34 +0000 (21:02 +0100)
sysctl.c:560 PreloadSystem(29) error: potential null derefence 'cfgs'.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sysctl.c

index 646d295babb8059a36713ec19ff027bc3c3ec7db..eb570586d83539400bb6ea92ae09db0a4d3f8169 100644 (file)
--- a/sysctl.c
+++ b/sysctl.c
@@ -557,7 +557,7 @@ static int PreloadSystem(void)
                                continue;
                        /* check if config already known */
                        for (i = 0; i < ncfgs; ++i) {
-                               if (!strcmp(cfgs[i]->name, de->d_name))
+                               if (cfgs && !strcmp(cfgs[i]->name, de->d_name))
                                        break;
                        }
                        if (i < ncfgs)
@@ -565,22 +565,32 @@ static int PreloadSystem(void)
                                continue;
 
                        if (ncfgs % nprealloc == 0)
-                               cfgs = xrealloc(cfgs, sizeof(struct pair *) * (ncfgs + nprealloc));
-                       cfgs[ncfgs] =
-                           xmalloc(sizeof(struct pair) + strlen(de->d_name) * 2 + 2 +
-                                   strlen(dirs[di]) + 1);
-                       cfgs[ncfgs]->name = (char *) cfgs[ncfgs] + sizeof(struct pair);
-                       strcpy(cfgs[ncfgs]->name, de->d_name);
-                       cfgs[ncfgs]->value =
-                           (char *) cfgs[ncfgs] + sizeof(struct pair) + strlen(cfgs[ncfgs]->name) +
-                           1;
-                       sprintf(cfgs[ncfgs]->value, "%s/%s", dirs[di], de->d_name);
-                       ncfgs++;
+                               cfgs =
+                                   xrealloc(cfgs,
+                                            sizeof(struct pair *) * (ncfgs +
+                                                                     nprealloc));
+
+                       if (cfgs) {
+                               cfgs[ncfgs] =
+                                   xmalloc(sizeof(struct pair) +
+                                           strlen(de->d_name) * 2 + 2 +
+                                           strlen(dirs[di]) + 1);
+                               cfgs[ncfgs]->name =
+                                   (char *)cfgs[ncfgs] + sizeof(struct pair);
+                               strcpy(cfgs[ncfgs]->name, de->d_name);
+                               cfgs[ncfgs]->value =
+                                   (char *)cfgs[ncfgs] + sizeof(struct pair) +
+                                   strlen(cfgs[ncfgs]->name) + 1;
+                               sprintf(cfgs[ncfgs]->value, "%s/%s", dirs[di],
+                                       de->d_name);
+                               ncfgs++;
+                       } else {
+                               xerrx(EXIT_FAILURE, _("internal error"));
+                       }
 
                }
                closedir(dp);
        }
-
        qsort(cfgs, ncfgs, sizeof(struct cfg *), sortpairs);
 
        for (i = 0; i < ncfgs; ++i) {