From a77e97df5b8145f29cb097bced90f7d91e2ff548 Mon Sep 17 00:00:00 2001 From: Oliver Kurth Date: Fri, 15 Sep 2017 11:23:27 -0700 Subject: [PATCH] Add Posix_Free and try harder to avoid clearing errno free() can clear errno. Apparently POSIX will prohibit that in a future version of the standard; in the meantime, add a Posix_Free function that guarantees errno preservation and start using it. Similarly, make Util_ZeroFree, Util_FreeList, etc. also preserve errno. --- open-vm-tools/lib/include/posix.h | 68 ++++++++--- open-vm-tools/lib/include/util.h | 15 +++ open-vm-tools/lib/misc/posixDlopen.c | 2 +- open-vm-tools/lib/misc/posixInt.h | 32 ++--- open-vm-tools/lib/misc/posixPosix.c | 168 +++++++++++++-------------- open-vm-tools/lib/misc/posixPwd.c | 56 +++++---- 6 files changed, 190 insertions(+), 151 deletions(-) diff --git a/open-vm-tools/lib/include/posix.h b/open-vm-tools/lib/include/posix.h index 5bce5b7a9..fd829657b 100644 --- a/open-vm-tools/lib/include/posix.h +++ b/open-vm-tools/lib/include/posix.h @@ -16,8 +16,8 @@ * *********************************************************/ -#ifndef _POSIX_H_ -#define _POSIX_H_ +#ifndef VMWARE_POSIX_H +#define VMWARE_POSIX_H #define INCLUDE_ALLOW_USERLEVEL #define INCLUDE_ALLOW_VMCORE @@ -102,6 +102,38 @@ long Posix_Pathconf(const char *pathName, int name); int Posix_Lstat(const char *pathName, struct stat *statbuf); char *Posix_MkTemp(const char *pathName); + +/* + *----------------------------------------------------------------------------- + * + * Posix_Free -- + * + * Wrapper around free() that preserves errno. + * + * C11 (and earlier) does not prohibit free() implementations from + * modifying errno. That is undesirable since it can clobber errno along + * cleanup paths, and it is expected to be prohibited by a future (as of + * January 2017) version of the POSIX standard. See: + * + * + * Results: + * None. + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +static INLINE void +Posix_Free(void *p) // IN +{ + int err = errno; + free(p); + errno = err; +} + + #if !defined(_WIN32) /* * These Windows APIs actually work with non-ASCII (MBCS) strings. @@ -170,7 +202,7 @@ int Posix_GetGroupList(const char *user, gid_t group, gid_t *groups, #if !defined(__APPLE__) && !defined(__FreeBSD__) int Posix_Mount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, - const void *data); + const void *data); int Posix_Umount(const char *target); FILE *Posix_Setmntent(const char *pathName, const char *mode); struct mntent *Posix_Getmntent(FILE *fp); @@ -280,6 +312,8 @@ Posix_FreeHostent(struct hostent *he) char **p; if (he) { + // See Posix_Free. + int err = errno; free(he->h_name); if (he->h_aliases) { Util_FreeStringList(he->h_aliases, -1); @@ -290,6 +324,7 @@ Posix_FreeHostent(struct hostent *he) } free(he->h_addr_list); free(he); + errno = err; } #else (void) he; @@ -342,10 +377,10 @@ Posix_GetHostName(char *name, // OUT retval = -1; WSASetLastError(WSAEFAULT); } - free(nameUTF8); + Posix_Free(nameUTF8); } - free(nameMBCS); + Posix_Free(nameMBCS); return retval; } @@ -384,7 +419,7 @@ Posix_GetHostByName(const char *name) // IN if (nameMBCS != NULL) { hostentMBCS = gethostbyname(nameMBCS); - free(nameMBCS); + Posix_Free(nameMBCS); if (hostentMBCS != NULL) { newhostent = (struct hostent *)Util_SafeMalloc(sizeof *newhostent); @@ -433,11 +468,11 @@ static INLINE void Posix_FreeHostent(struct hostent *he) { if (he) { - free(he->h_name); + Posix_Free(he->h_name); if (he->h_aliases) { Util_FreeStringList(he->h_aliases, -1); } - free(he); + Posix_Free(he); } } #endif // defined(_WINSOCKAPI_) || defined(_WINSOCK2API_) @@ -511,8 +546,8 @@ Posix_GetAddrInfo(const char *nodename, // IN FreeAddrInfoW(resW); } - free(nodenameW); - free(servnameW); + Posix_Free(nodenameW); + Posix_Free(servnameW); return retval; } @@ -539,6 +574,8 @@ Posix_FreeAddrInfo(struct addrinfo *ai) { struct addrinfo *temp; + // See Posix_Free. + int err = errno; while (ai) { temp = ai; ai = ai->ai_next; @@ -546,6 +583,7 @@ Posix_FreeAddrInfo(struct addrinfo *ai) free(temp->ai_addr); free(temp); } + errno = err; } @@ -617,10 +655,10 @@ Posix_GetNameInfo(const struct sockaddr *sa, // IN } exit: - free(hostW); - free(servW); - free(hostUTF8); - free(servUTF8); + Posix_Free(hostW); + Posix_Free(servW); + Posix_Free(hostUTF8); + Posix_Free(servUTF8); return retval; } @@ -694,4 +732,4 @@ exit: } // extern "C" #endif -#endif // _POSIX_H_ +#endif // VMWARE_POSIX_H diff --git a/open-vm-tools/lib/include/util.h b/open-vm-tools/lib/include/util.h index 17f633a19..39c028e6f 100644 --- a/open-vm-tools/lib/include/util.h +++ b/open-vm-tools/lib/include/util.h @@ -515,8 +515,11 @@ Util_ZeroFree(void *buf, // OUT/OPT size_t bufSize) // IN { if (buf != NULL) { + // See Posix_Free. + int err = errno; Util_Zero(buf, bufSize); free(buf); + errno = err; } } @@ -542,8 +545,11 @@ static INLINE void Util_ZeroFreeString(char *str) // IN/OUT/OPT { if (str != NULL) { + // See Posix_Free. + int err = errno; Util_ZeroString(str); free(str); + errno = err; } } @@ -570,8 +576,11 @@ static INLINE void Util_ZeroFreeStringW(wchar_t *str) // IN/OUT/OPT { if (str != NULL) { + // See Posix_Free. + int err = errno; Util_Zero(str, wcslen(str) * sizeof *str); free(str); + errno = err; } } #endif // _WIN32 @@ -604,11 +613,16 @@ static INLINE void Util_FreeList(void **list, // IN/OUT/OPT: the list to free ssize_t length) // IN: the length { + // See Posix_Free. + int err; + if (list == NULL) { ASSERT(length <= 0); return; } + err = errno; + if (length >= 0) { ssize_t i; @@ -625,6 +639,7 @@ Util_FreeList(void **list, // IN/OUT/OPT: the list to free } } free(list); + errno = err; } static INLINE void diff --git a/open-vm-tools/lib/misc/posixDlopen.c b/open-vm-tools/lib/misc/posixDlopen.c index a8f002f6b..fca6a18fb 100644 --- a/open-vm-tools/lib/misc/posixDlopen.c +++ b/open-vm-tools/lib/misc/posixDlopen.c @@ -60,7 +60,7 @@ Posix_Dlopen(const char *pathName, // IN: ret = dlopen(path, flag); - free(path); + Posix_Free(path); return ret; } #endif diff --git a/open-vm-tools/lib/misc/posixInt.h b/open-vm-tools/lib/misc/posixInt.h index f166ff052..02e8e7432 100644 --- a/open-vm-tools/lib/misc/posixInt.h +++ b/open-vm-tools/lib/misc/posixInt.h @@ -175,8 +175,8 @@ PosixGetenvHash(const char *name, // IN } ht = HashTable_AllocOnce(&htPtr, 128, - HASH_FLAG_ATOMIC | HASH_FLAG_COPYKEY | - HASH_STRING_KEY, + HASH_FLAG_ATOMIC | HASH_FLAG_COPYKEY | + HASH_STRING_KEY, PosixEnvFree); /* @@ -195,14 +195,14 @@ PosixGetenvHash(const char *name, // IN */ if (!HashTable_Lookup(ht, name, (void **) &e)) { - e = Util_SafeMalloc(sizeof *e); - Atomic_WritePtr(&e->value, value); - Atomic_WritePtr(&e->lastValue, NULL); - if (!HashTable_Insert(ht, name, e)) { - free(e); - continue; - } - break; + e = Util_SafeMalloc(sizeof *e); + Atomic_WritePtr(&e->value, value); + Atomic_WritePtr(&e->lastValue, NULL); + if (!HashTable_Insert(ht, name, e)) { + Posix_Free(e); + continue; + } + break; } /* @@ -211,9 +211,9 @@ PosixGetenvHash(const char *name, // IN oldValue = Atomic_ReadPtr(&e->value); if (Str_Strcmp(oldValue, value) == 0) { - free(value); - value = oldValue; - break; + Posix_Free(value); + value = oldValue; + break; } /* @@ -225,9 +225,9 @@ PosixGetenvHash(const char *name, // IN */ if (Atomic_ReadIfEqualWritePtr(&e->value, oldValue, value) == oldValue) { - oldValue = Atomic_ReadWritePtr(&e->lastValue, oldValue); - free(oldValue); - break; + oldValue = Atomic_ReadWritePtr(&e->lastValue, oldValue); + Posix_Free(oldValue); + break; } } diff --git a/open-vm-tools/lib/misc/posixPosix.c b/open-vm-tools/lib/misc/posixPosix.c index c4bd4be64..8d240988a 100644 --- a/open-vm-tools/lib/misc/posixPosix.c +++ b/open-vm-tools/lib/misc/posixPosix.c @@ -138,7 +138,7 @@ Posix_Open(const char *pathName, // IN: fd = open(path, flags, mode); - free(path); + Posix_Free(path); return fd; } @@ -200,7 +200,7 @@ Posix_Fopen(const char *pathName, // IN: stream = fopen(path, mode); - free(path); + Posix_Free(path); return stream; } @@ -236,7 +236,7 @@ Posix_Stat(const char *pathName, // IN: ret = stat(path, statbuf); - free(path); + Posix_Free(path); return ret; } @@ -272,7 +272,7 @@ Posix_Chmod(const char *pathName, // IN: ret = chmod(path, mode); - free(path); + Posix_Free(path); return ret; } @@ -306,14 +306,14 @@ Posix_Rename(const char *fromPathName, // IN: return -1; } if (!PosixConvertToCurrent(toPathName, &toPath)) { - free(fromPath); + Posix_Free(fromPath); return -1; } result = rename(fromPath, toPath); - free(toPath); - free(fromPath); + Posix_Free(toPath); + Posix_Free(fromPath); return result; } @@ -347,7 +347,7 @@ Posix_Unlink(const char *pathName) // IN: ret = unlink(path); - free(path); + Posix_Free(path); return ret; } @@ -381,7 +381,7 @@ Posix_Rmdir(const char *pathName) // IN: ret = rmdir(path); - free(path); + Posix_Free(path); return ret; } @@ -420,7 +420,7 @@ Posix_Freopen(const char *pathName, // IN: stream = freopen(path, mode, input_stream); - free(path); + Posix_Free(path); return stream; } @@ -467,7 +467,7 @@ Posix_Access(const char *pathName, // IN: ret = access(path, mode); #endif - free(path); + Posix_Free(path); return ret; } @@ -504,7 +504,7 @@ Posix_EuidAccess(const char *pathName, // IN: ret = euidaccess(path, mode); - free(path); + Posix_Free(path); return ret; #else errno = ENOSYS; @@ -543,7 +543,7 @@ Posix_Utime(const char *pathName, // IN: ret = utime(path, times); - free(path); + Posix_Free(path); return ret; } @@ -574,7 +574,7 @@ Posix_Perror(const char *str) // IN: // ignore conversion error silently perror(tmpstr); - free(tmpstr); + Posix_Free(tmpstr); } @@ -607,7 +607,7 @@ Posix_Pathconf(const char *pathName, // IN: ret = pathconf(path, name); - free(path); + Posix_Free(path); return ret; } @@ -645,7 +645,7 @@ Posix_Popen(const char *pathName, // IN: stream = popen(path, mode); - free(path); + Posix_Free(path); return stream; } @@ -682,7 +682,7 @@ Posix_Mknod(const char *pathName, // IN: ret = mknod(path, mode, dev); - free(path); + Posix_Free(path); return ret; } @@ -719,7 +719,7 @@ Posix_Chown(const char *pathName, // IN: ret = chown(path, owner, group); - free(path); + Posix_Free(path); return ret; } @@ -756,7 +756,7 @@ Posix_Lchown(const char *pathName, // IN: ret = lchown(path, owner, group); - free(path); + Posix_Free(path); return ret; } @@ -791,15 +791,15 @@ Posix_Link(const char *pathName1, // IN: return -1; } if (!PosixConvertToCurrent(pathName2, &path2)) { - free(path1); + Posix_Free(path1); return -1; } ret = link(path1, path2); - free(path1); - free(path2); + Posix_Free(path1); + Posix_Free(path2); return ret; } @@ -834,15 +834,15 @@ Posix_Symlink(const char *pathName1, // IN: return -1; } if (!PosixConvertToCurrent(pathName2, &path2)) { - free(path1); + Posix_Free(path1); return -1; } ret = symlink(path1, path2); - free(path1); - free(path2); + Posix_Free(path1); + Posix_Free(path2); return ret; } @@ -878,7 +878,7 @@ Posix_Mkfifo(const char *pathName, // IN: ret = mkfifo(path, mode); - free(path); + Posix_Free(path); return ret; } @@ -914,7 +914,7 @@ Posix_Truncate(const char *pathName, // IN: ret = truncate(path, length); - free(path); + Posix_Free(path); return ret; } @@ -950,7 +950,7 @@ Posix_Utimes(const char *pathName, // IN: ret = utimes(path, times); - free(path); + Posix_Free(path); return ret; } @@ -1026,10 +1026,8 @@ Posix_Execl(const char *pathName, // IN: ret = execv(path, argv); exit: - if (argv) { - Util_FreeStringList(argv, -1); - } - free(path); + Util_FreeStringList(argv, -1); + Posix_Free(path); return ret; } @@ -1105,10 +1103,8 @@ Posix_Execlp(const char *fileName, // IN: ret = execvp(file, argv); exit: - if (argv) { - Util_FreeStringList(argv, -1); - } - free(file); + Util_FreeStringList(argv, -1); + Posix_Free(file); return ret; } @@ -1149,10 +1145,8 @@ Posix_Execv(const char *pathName, // IN: ret = execv(path, argv); exit: - if (argv) { - Util_FreeStringList(argv, -1); - } - free(path); + Util_FreeStringList(argv, -1); + Posix_Free(path); return ret; } @@ -1177,7 +1171,7 @@ exit: int Posix_Execve(const char *pathName, // IN: - char *const argVal[], // IN: + char *const argVal[], // IN: char *const envPtr[]) // IN: { int ret = -1; @@ -1198,13 +1192,9 @@ Posix_Execve(const char *pathName, // IN: ret = execve(path, argv, envp); exit: - if (argv) { - Util_FreeStringList(argv, -1); - } - if (envp) { - Util_FreeStringList(envp, -1); - } - free(path); + Util_FreeStringList(argv, -1); + Util_FreeStringList(envp, -1); + Posix_Free(path); return ret; } @@ -1245,10 +1235,8 @@ Posix_Execvp(const char *fileName, // IN: ret = execvp(file, argv); exit: - if (argv) { - Util_FreeStringList(argv, -1); - } - free(file); + Util_FreeStringList(argv, -1); + Posix_Free(file); return ret; } @@ -1282,7 +1270,7 @@ Posix_System(const char *command) // IN: ret = system(tmpcommand); - free(tmpcommand); + Posix_Free(tmpcommand); return ret; } @@ -1318,7 +1306,7 @@ Posix_Mkdir(const char *pathName, // IN: ret = mkdir(path, mode); - free(path); + Posix_Free(path); return ret; } @@ -1353,7 +1341,7 @@ Posix_Chdir(const char *pathName) // IN: ret = chdir(path); - free(path); + Posix_Free(path); return ret; } @@ -1389,7 +1377,7 @@ Posix_RealPath(const char *pathName) // IN: p = realpath(path, rpath); - free(path); + Posix_Free(path); return p == NULL ? NULL : Unicode_Alloc(rpath, STRING_ENCODING_DEFAULT); } @@ -1426,23 +1414,23 @@ Posix_ReadLink(const char *pathName) // IN: ssize_t len = readlink(path, linkPath, size); if (len == -1) { - free(linkPath); + Posix_Free(linkPath); break; } if (len < size) { linkPath[len] = '\0'; // Add the missing NUL to path result = Unicode_Alloc(linkPath, STRING_ENCODING_DEFAULT); - free(linkPath); + Posix_Free(linkPath); break; } - free(linkPath); + Posix_Free(linkPath); size += 1024; } } - free(path); + Posix_Free(path); return result; } @@ -1478,7 +1466,7 @@ Posix_Lstat(const char *pathName, // IN: ret = lstat(path, statbuf); - free(path); + Posix_Free(path); return ret; } @@ -1513,7 +1501,7 @@ Posix_OpenDir(const char *pathName) // IN: ret = opendir(path); - free(path); + Posix_Free(path); return ret; } @@ -1547,14 +1535,14 @@ Posix_Getenv(const char *name) // IN: return NULL; } rawValue = getenv(rawName); - free(rawName); + Posix_Free(rawName); if (rawValue == NULL) { return NULL; } return PosixGetenvHash(name, Unicode_Alloc(rawValue, - STRING_ENCODING_DEFAULT)); + STRING_ENCODING_DEFAULT)); } @@ -1617,7 +1605,7 @@ Posix_Statfs(const char *pathName, // IN: ret = statfs(path, statfsbuf); - free(path); + Posix_Free(path); return ret; } @@ -1703,8 +1691,8 @@ Posix_Setenv(const char *name, // IN: #endif exit: - free(rawName); - free(rawValue); + Posix_Free(rawName); + Posix_Free(rawValue); return ret; } @@ -1740,7 +1728,7 @@ Posix_Unsetenv(const char *name) // IN: #else unsetenv(rawName); #endif - free(rawName); + Posix_Free(rawName); } @@ -1785,8 +1773,8 @@ Posix_Mount(const char *source, // IN: ret = mount(tmpsource, tmptarget, filesystemtype, mountflags, data); exit: - free(tmpsource); - free(tmptarget); + Posix_Free(tmpsource); + Posix_Free(tmptarget); return ret; } @@ -1821,7 +1809,7 @@ Posix_Umount(const char *target) // IN: ret = umount(tmptarget); - free(tmptarget); + Posix_Free(tmptarget); return ret; } @@ -1862,7 +1850,7 @@ Posix_Setmntent(const char *pathName, // IN: return NULL; } stream = setmntent(path, mode); - free(path); + Posix_Free(path); return stream; #endif @@ -1898,13 +1886,13 @@ Posix_Getmntent(FILE *fp) // IN: } /* Free static structure string pointers before reuse. */ - free(sm.mnt_fsname); + Posix_Free(sm.mnt_fsname); sm.mnt_fsname = NULL; - free(sm.mnt_dir); + Posix_Free(sm.mnt_dir); sm.mnt_dir = NULL; - free(sm.mnt_type); + Posix_Free(sm.mnt_type); sm.mnt_type = NULL; - free(sm.mnt_opts); + Posix_Free(sm.mnt_opts); sm.mnt_opts = NULL; /* Fill out structure with new values. */ @@ -2057,10 +2045,10 @@ Posix_Getmntent_r(FILE *fp, // IN: exit: - free(fsname); - free(dir); - free(type); - free(opts); + Posix_Free(fsname); + Posix_Free(dir); + Posix_Free(type); + Posix_Free(opts); if (ret != 0) { errno = ret; @@ -2108,8 +2096,8 @@ Posix_Printf(const char *format, // IN: } numChars = printf("%s", outCurr); - free(output); - free(outCurr); + Posix_Free(output); + Posix_Free(outCurr); return numChars; } @@ -2151,8 +2139,8 @@ Posix_Fprintf(FILE *stream, // IN: } nOutput = fprintf(stream, "%s", outCurr); - free(output); - free(outCurr); + Posix_Free(output); + Posix_Free(outCurr); return nOutput; } @@ -2189,11 +2177,11 @@ Posix_Getmntent(FILE *fp, // IN: ret = getmntent(fp, mp); if (ret == 0) { - free(m.mnt_special); - free(m.mnt_mountp); - free(m.mnt_fstype); - free(m.mnt_mntopts); - free(m.mnt_time); + Posix_Free(m.mnt_special); + Posix_Free(m.mnt_mountp); + Posix_Free(m.mnt_fstype); + Posix_Free(m.mnt_mntopts); + Posix_Free(m.mnt_time); m.mnt_special = Unicode_Alloc(mp->mnt_special, STRING_ENCODING_DEFAULT); m.mnt_mountp = Unicode_Alloc(mp->mnt_mountp, STRING_ENCODING_DEFAULT); m.mnt_fstype = Unicode_Alloc(mp->mnt_fstype, STRING_ENCODING_DEFAULT); @@ -2247,6 +2235,6 @@ Posix_MkTemp(const char *pathName) // IN: unlink(path); result = Unicode_Alloc(path, STRING_ENCODING_DEFAULT); } - free(path); + Posix_Free(path); return result; } diff --git a/open-vm-tools/lib/misc/posixPwd.c b/open-vm-tools/lib/misc/posixPwd.c index aadb31290..483f8f034 100644 --- a/open-vm-tools/lib/misc/posixPwd.c +++ b/open-vm-tools/lib/misc/posixPwd.c @@ -72,7 +72,7 @@ Posix_Getpwnam(const char *name) // IN: return NULL; } pw = getpwnam(tmpname); - free(tmpname); + Posix_Free(tmpname); return GetpwInternal(pw); } @@ -130,20 +130,20 @@ GetpwInternal(struct passwd *pw) // IN: } /* Free static structure string pointers before reuse. */ - free(spw.pw_passwd); + Posix_Free(spw.pw_passwd); spw.pw_passwd = NULL; - free(spw.pw_dir); + Posix_Free(spw.pw_dir); spw.pw_dir = NULL; - free(spw.pw_name); + Posix_Free(spw.pw_name); spw.pw_name = NULL; #if !defined __ANDROID__ - free(spw.pw_gecos); + Posix_Free(spw.pw_gecos); spw.pw_gecos = NULL; #endif - free(spw.pw_shell); + Posix_Free(spw.pw_shell); spw.pw_shell = NULL; #if defined(__FreeBSD__) - free(spw.pw_class); + Posix_Free(spw.pw_class); spw.pw_class = NULL; #endif @@ -208,6 +208,7 @@ exit: /* *---------------------------------------------------------------------- + * * Posix_Getpwent -- * * POSIX getpwent() @@ -643,7 +644,7 @@ Posix_Getpwnam_r(const char *name, // IN: ret = EmulateGetpwnam_r(tmpname, pw, buf, size, ppw); #endif - free(tmpname); + Posix_Free(tmpname); // ret is errno on failure, *ppw is NULL if no matching entry found. if (ret != 0 || *ppw == NULL) { @@ -826,13 +827,13 @@ GetpwInternal_r(struct passwd *pw, // IN: ret = 0; exit: - free(passwd); - free(dir); - free(pwname); + Posix_Free(passwd); + Posix_Free(dir); + Posix_Free(pwname); #if !defined __ANDROID__ - free(gecos); + Posix_Free(gecos); #endif - free(shell); + Posix_Free(shell); return ret; } @@ -883,7 +884,7 @@ Posix_GetGroupList(const char *user, // IN: *ngroups = 1; if (n < 1) { - return -1; + return -1; } ASSERT(groups != NULL); *groups = group; @@ -893,7 +894,7 @@ Posix_GetGroupList(const char *user, // IN: ret = getgrouplist(tmpuser, group, groups, ngroups); - free(tmpuser); + Posix_Free(tmpuser); return ret; } @@ -903,6 +904,7 @@ Posix_GetGroupList(const char *user, // IN: /* *---------------------------------------------------------------------- + * * Posix_Getgrnam -- * * POSIX getgrnam() @@ -928,21 +930,19 @@ Posix_Getgrnam(const char *name) // IN: return NULL; } gr = getgrnam(tmpname); - free(tmpname); + Posix_Free(tmpname); if (!gr) { return NULL; } /* Free static structure string pointers before reuse. */ - free(sgr.gr_name); + Posix_Free(sgr.gr_name); sgr.gr_name = NULL; - free(sgr.gr_passwd); + Posix_Free(sgr.gr_passwd); sgr.gr_passwd = NULL; - if (sgr.gr_mem != NULL) { - Util_FreeStringList(sgr.gr_mem, -1); - sgr.gr_mem = NULL; - } + Util_FreeStringList(sgr.gr_mem, -1); + sgr.gr_mem = NULL; /* Fill out structure with new values. */ sgr.gr_gid = gr->gr_gid; @@ -1023,7 +1023,7 @@ Posix_Getgrnam_r(const char *name, // IN: #else ret = EmulateGetgrnam_r(tmpname, gr, buf, size, pgr); #endif - free(tmpname); + Posix_Free(tmpname); // ret is errno on failure, *pgr is NULL if no matching entry found. if (ret != 0 || *pgr == NULL) { @@ -1088,7 +1088,7 @@ Posix_Getgrnam_r(const char *name, // IN: size_t len = strlen(grmem[i]) + 1; if (n + len > size) { - goto exit; + goto exit; } gr->gr_mem[i] = memcpy(buf + n, grmem[i], len); n += len; @@ -1098,11 +1098,9 @@ Posix_Getgrnam_r(const char *name, // IN: ret = 0; exit: - free(grpasswd); - free(grname); - if (grmem) { - Util_FreeStringList(grmem, -1); - } + Posix_Free(grpasswd); + Posix_Free(grname); + Util_FreeStringList(grmem, -1); return ret; } -- 2.47.3