From: Karel Zak Date: Thu, 17 Feb 2011 10:23:24 +0000 (+0100) Subject: chsh: fix small memory leak X-Git-Tag: v2.20-rc1~551 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d8bee4cb24994194b9af45754339dc87243738c5;p=thirdparty%2Futil-linux.git chsh: fix small memory leak Reported-by: Steve Grubb Signed-off-by: Karel Zak --- diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c index 2aa7dd5bb9..05ce147b4b 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;