]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chsh: fix small memory leak
authorKarel Zak <kzak@redhat.com>
Thu, 17 Feb 2011 10:23:24 +0000 (11:23 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 17 Feb 2011 10:23:24 +0000 (11:23 +0100)
Reported-by: Steve Grubb <sgrubb@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/setpwnam.c

index 2aa7dd5bb9575583cd10f4b7e314e2cece0c4bd3..05ce147b4b598f7f1b8418b31cd7db77efe85462 100644 (file)
@@ -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;