nsswitch: cppcheck: Fix memleakOnRealloc errors
authorNoel Power <noel.power@suse.com>
Wed, 22 May 2019 11:09:41 +0000 (11:09 +0000)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 4 Jun 2019 22:13:07 +0000 (22:13 +0000)
Fixes the following errors

nsswitch/nsstest.c:192: error: memleakOnRealloc: Common realloc mistake: 'buf' nulled but not freed upon failure <--[cppcheck]
nsswitch/nsstest.c:230: error: memleakOnRealloc: Common realloc mistake: 'buf' nulled but not freed upon failure <--[cppcheck]
nsswitch/nsstest.c:269: error: memleakOnRealloc: Common realloc mistake: 'buf' nulled but not freed upon failure <--[cppcheck]

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
nsswitch/nsstest.c

index 6d92806..e8c4306 100644 (file)
@@ -188,9 +188,11 @@ static struct group *nss_getgrent(void)
 again:
        status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
        if (status == NSS_STATUS_TRYAGAIN) {
+               char *oldbuf = buf;
                buflen *= 2;
                buf = (char *)realloc(buf, buflen);
                if (!buf) {
+                       SAFE_FREE(oldbuf);
                        return NULL;
                }
                goto again;
@@ -226,9 +228,11 @@ static struct group *nss_getgrnam(const char *name)
 again:
        status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
        if (status == NSS_STATUS_TRYAGAIN) {
+               char *oldbuf = buf;
                buflen *= 2;
                buf = (char *)realloc(buf, buflen);
                if (!buf) {
+                       SAFE_FREE(oldbuf);
                        return NULL;
                }
                goto again;
@@ -265,9 +269,11 @@ static struct group *nss_getgrgid(gid_t gid)
 again:
        status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
        if (status == NSS_STATUS_TRYAGAIN) {
+               char *oldbuf = buf;
                buflen *= 2;
                buf = (char *)realloc(buf, buflen);
                if (!buf) {
+                       SAFE_FREE(oldbuf);
                        return NULL;
                }
                goto again;