summaryrefslogtreecommitdiffstats
path: root/login-utils/setpwnam.c
diff options
context:
space:
mode:
authorKarel Zak2011-02-17 11:23:24 +0100
committerKarel Zak2011-02-17 11:23:24 +0100
commitd8bee4cb24994194b9af45754339dc87243738c5 (patch)
treea56efa3a14c2ea23266d967d7c1cef61c4987f02 /login-utils/setpwnam.c
parentlogin: does not ignore setgid() return code for non-roots (diff)
downloadkernel-qcow2-util-linux-d8bee4cb24994194b9af45754339dc87243738c5.tar.gz
kernel-qcow2-util-linux-d8bee4cb24994194b9af45754339dc87243738c5.tar.xz
kernel-qcow2-util-linux-d8bee4cb24994194b9af45754339dc87243738c5.zip
chsh: fix small memory leak
Reported-by: Steve Grubb <sgrubb@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'login-utils/setpwnam.c')
-rw-r--r--login-utils/setpwnam.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c
index 2aa7dd5bb..05ce147b4 100644
--- a/login-utils/setpwnam.c
+++ b/login-utils/setpwnam.c
@@ -87,9 +87,7 @@ setpwnam (struct passwd *pwd)
int namelen;
int buflen = 256;
int contlen;
- char *linebuf = malloc(buflen);
-
- if (!linebuf) return -1;
+ char *linebuf = NULL;
oldumask = umask(0); /* Create with exact permissions */
@@ -125,19 +123,25 @@ setpwnam (struct passwd *pwd)
namelen = strlen(pwd->pw_name);
+ linebuf = malloc(buflen);
+ if (!linebuf) goto fail;
+
/* parse the passwd file */
found = false;
/* Do you wonder why I don't use getpwent? Read comments at top of file */
while (fgets(linebuf, buflen, pwf) != NULL) {
contlen = strlen(linebuf);
while (linebuf[contlen-1] != '\n' && !feof(pwf)) {
+ char *tmp;
+
/* Extend input buffer if it failed getting the whole line */
/* So now we double the buffer size */
buflen *= 2;
- linebuf = realloc(linebuf, buflen);
- if (linebuf == NULL) goto fail;
+ tmp = realloc(linebuf, buflen);
+ if (tmp== NULL) goto fail;
+ linebuf = tmp;
/* And fill the rest of the buffer */
if (fgets(&linebuf[contlen], buflen/2, pwf) == NULL) break;