From 1689e769e65dd4c42ebffc440003e89197a00723 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 5 Feb 2012 21:02:34 +0100 Subject: [PATCH] sysctl: fix potential null derefence [smatch scan] sysctl.c:560 PreloadSystem(29) error: potential null derefence 'cfgs'. Signed-off-by: Sami Kerola --- sysctl.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/sysctl.c b/sysctl.c index 646d295b..eb570586 100644 --- 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) {