summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCsaba Henk <csaba@redhat.com>2012-05-16 15:40:47 +0530
committerVijay Bellur <vijay@gluster.com>2012-05-22 11:22:48 -0700
commit258b0f9b2e73d560774d80bd4724155e550ea31b (patch)
tree95815e4aabb155b8f39b065f0a95049b92ba094d
parent52f3aac6d80aea5d7c531005cb621463806000af (diff)
geo-rep / gsyncd.c: fix coverity fix
gsyncd wrapper was segfaulting as coverity fix freed up pointer at wrong place (after it was reused) Instead of the apporach of the original coverity fix that added elaborate control flow to hunt down potential leaks, here we move the code over to static allocations in place of (the not really necessary) dynamic ones. Change-Id: Ida3855ff4a4f4371b350d27f858f129ceed51785 BUG: 789278 Signed-off-by: Csaba Henk <csaba@redhat.com> Reviewed-on: http://review.gluster.com/3345 Tested-by: Gluster Build System <jenkins@build.gluster.com> Reviewed-by: Vijay Bellur <vijay@gluster.com> Reviewed-on: http://review.gluster.com/3405
-rw-r--r--xlators/features/marker/utils/src/gsyncd.c63
-rw-r--r--xlators/features/marker/utils/src/procdiggy.c59
2 files changed, 36 insertions, 86 deletions
diff --git a/xlators/features/marker/utils/src/gsyncd.c b/xlators/features/marker/utils/src/gsyncd.c
index 24de4096f8b..438451a4450 100644
--- a/xlators/features/marker/utils/src/gsyncd.c
+++ b/xlators/features/marker/utils/src/gsyncd.c
@@ -159,6 +159,7 @@ static int
find_gsyncd (pid_t pid, pid_t ppid, char *name, void *data)
{
char buf[NAME_MAX * 2] = {0,};
+ char path[PATH_MAX] = {0,};
char *p = NULL;
int zeros = 0;
int ret = 0;
@@ -166,28 +167,16 @@ find_gsyncd (pid_t pid, pid_t ppid, char *name, void *data)
pid_t *pida = (pid_t *)data;
if (ppid != pida[0])
- goto out;
+ return 0;
- ret = gf_asprintf (&p, PROC"/%d/cmdline", pid);
- if (ret == -1) {
- fprintf (stderr, "out of memory\n");
- goto out;
- }
-
- ret = 0;
-
- fd = open (p, O_RDONLY);
+ sprintf (path, PROC"/%d/cmdline", pid);
+ fd = open (path, O_RDONLY);
if (fd == -1)
- goto out;
-
+ return 0;
ret = read (fd, buf, sizeof (buf));
close (fd);
-
- if (ret == -1) {
- ret = 0;
- goto out;
- }
-
+ if (ret == -1)
+ return 0;
for (zeros = 0, p = buf; zeros < 2 && p < buf + ret; p++)
zeros += !*p;
@@ -208,25 +197,19 @@ find_gsyncd (pid_t pid, pid_t ppid, char *name, void *data)
if (ret == 1) {
if (pida[1] != -1) {
fprintf (stderr, GSYNCD_PY" sibling is not unique");
- ret = -1;
- goto out;
+ return -1;
}
pida[1] = pid;
}
- ret = 0;
-out:
- if (p)
- GF_FREE (p);
-
- return ret;
+ return 0;
}
static int
invoke_rsync (int argc, char **argv)
{
int i = 0;
- char *p = NULL;
+ char path[PATH_MAX] = {0,};
pid_t pid = -1;
pid_t ppid = -1;
pid_t pida[] = {-1, -1};
@@ -253,10 +236,11 @@ invoke_rsync (int argc, char **argv)
fprintf (stderr, "sshd ancestor not found\n");
goto error;
}
- if (strcmp (name, "sshd") == 0)
+ if (strcmp (name, "sshd") == 0) {
+ GF_FREE (name);
break;
+ }
GF_FREE (name);
- name = NULL;
}
/* look up "ssh-sibling" gsyncd */
pida[0] = pid;
@@ -266,15 +250,12 @@ invoke_rsync (int argc, char **argv)
goto error;
}
/* check if rsync target matches gsyncd target */
- if (gf_asprintf (&p, PROC"/%d/cwd", pida[1]) == -1) {
- fprintf (stderr, "out of memory\n");
- goto error;
- }
- ret = readlink (p, buf, sizeof (buf));
+ sprintf (path, PROC"/%d/cwd", pida[1]);
+ ret = readlink (path, buf, sizeof (buf));
if (ret == -1 || ret == sizeof (buf))
goto error;
if (strcmp (argv[argc - 1], "/") == 0 /* root dir cannot be a target */ ||
- (strcmp (argv[argc - 1], p) /* match against gluster target */ &&
+ (strcmp (argv[argc - 1], path) /* match against gluster target */ &&
strcmp (argv[argc - 1], buf) /* match against file target */) != 0) {
fprintf (stderr, "rsync target does not match "GEOREP" session\n");
goto error;
@@ -284,22 +265,10 @@ invoke_rsync (int argc, char **argv)
execvp (RSYNC, argv);
- if (p)
- GF_FREE (p);
-
- if (name)
- GF_FREE (name);
-
fprintf (stderr, "exec of "RSYNC" failed\n");
return 127;
error:
- if (p)
- GF_FREE (p);
-
- if (name)
- GF_FREE (name);
-
fprintf (stderr, "disallowed "RSYNC" invocation\n");
return 1;
}
diff --git a/xlators/features/marker/utils/src/procdiggy.c b/xlators/features/marker/utils/src/procdiggy.c
index e5f9af7b614..6f6e1dd31f7 100644
--- a/xlators/features/marker/utils/src/procdiggy.c
+++ b/xlators/features/marker/utils/src/procdiggy.c
@@ -37,20 +37,15 @@ pidinfo (pid_t pid, char **name)
{
char buf[NAME_MAX * 2] = {0,};
FILE *f = NULL;
+ char path[PATH_MAX] = {0,};
char *p = NULL;
- char *free_p = NULL;
int ret = 0;
- ret = gf_asprintf (&p, PROC"/%d/status", pid);
- if (ret == -1)
- goto oom;
+ sprintf (path, PROC"/%d/status", pid);
- f = fopen (p, "r");
- if (!f) {
- GF_FREE (p);
+ f = fopen (path, "r");
+ if (!f)
return -1;
- }
- free_p = p;
if (name)
*name = NULL;
@@ -68,8 +63,10 @@ pidinfo (pid_t pid, char **name)
if (p) {
while (isspace (*++p));
*name = gf_strdup (p);
- if (!*name)
- goto oom;
+ if (!*name) {
+ pid = -2;
+ goto out;
+ }
continue;
}
}
@@ -85,17 +82,12 @@ pidinfo (pid_t pid, char **name)
pid = -1;
out:
- if (free_p)
- GF_FREE (free_p);
fclose (f);
+ if (pid == -1 && name && *name)
+ GF_FREE (name);
+ if (pid == -2)
+ fprintf (stderr, "out of memory\n");
return pid;
-
- oom:
- if (free_p)
- GF_FREE (free_p);
- fclose (f);
- fprintf (stderr, "out of memory\n");
- return -2;
}
int
@@ -110,38 +102,27 @@ prociter (int (*proch) (pid_t pid, pid_t ppid, char *tmpname, void *data),
int ret = 0;
d = opendir (PROC);
- if (!d) {
- ret = -1;
- goto out;
- }
+ if (!d)
+ return -1;
while (errno = 0, de = readdir (d)) {
if (gf_string2int (de->d_name, &pid) != -1 && pid >= 0) {
ppid = pidinfo (pid, &name);
switch (ppid) {
case -1: continue;
- case -2: closedir (d); return -1;
+ case -2: ret = -1; break;
}
ret = proch (pid, ppid, name, data);
- if (ret) {
- goto out;
- }
GF_FREE (name);
- name = NULL;
+ if (ret)
+ break;
}
}
- if (errno) {
+ closedir (d);
+ if (!de && errno) {
fprintf (stderr, "failed to traverse "PROC" (%s)\n",
strerror (errno));
- goto out;
+ ret = -1;
}
- ret = 0;
-out:
- if (d)
- closedir (d);
-
- if (name)
- GF_FREE (name);
-
return ret;
}