From 235357382b2d3266cda484f37d8765f77fc96db3 Mon Sep 17 00:00:00 2001 From: "valeski%netscape.com" Date: Wed, 16 Sep 1998 07:17:34 +0000 Subject: [PATCH] General memory repairs (leakage, freeing mismatched mem, umrs) --- network/main/mkconect.c | 10 ++++++++-- network/module/nsNetFile.cpp | 4 +++- network/module/nsNetStubs.cpp | 28 ++++++++++++++++++---------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/network/main/mkconect.c b/network/main/mkconect.c index 763b78c02535..fe80d4d8a19d 100644 --- a/network/main/mkconect.c +++ b/network/main/mkconect.c @@ -926,8 +926,10 @@ net_start_first_connect(const char *host, * The user is guaranteing it's existence and usability. */ if ( localIP > 0 ) { PRStatus status; - PRNetAddr *addr = (PRNetAddr *)PR_Malloc(sizeof(PRNetAddr)); PRErrorCode errorCode; /* see http://www.mozilla.org/docs/refList/refNSPR/prerr.htm#1027954 */ + PRNetAddr *addr = (PRNetAddr *)PR_Malloc(sizeof(PRNetAddr)); + if (!addr) + return MK_UNABLE_TO_CONNECT; status = PR_InitializeNetAddr(PR_IpAddrNull, 0, addr); if (status != PR_SUCCESS) { errorCode = PR_GetError(); @@ -938,6 +940,7 @@ net_start_first_connect(const char *host, errorCode = PR_GetError(); } } + PR_Free(addr); } /* if it's not equal to PR_SUCCESS something went wrong @@ -1454,8 +1457,10 @@ NET_FinishConnect (CONST char *url, * The user is guaranteing it's existence and usability. */ if ( localIP > 0 ) { PRStatus status; - PRNetAddr *addr = (PRNetAddr *)PR_Malloc(sizeof(PRNetAddr)); PRErrorCode errorCode; /* see http://www.mozilla.org/docs/refList/refNSPR/prerr.htm#1027954 */ + PRNetAddr *addr = (PRNetAddr *)PR_Malloc(sizeof(PRNetAddr)); + if (!addr) + return MK_UNABLE_TO_CONNECT; status = PR_InitializeNetAddr(PR_IpAddrNull, 0, addr); if (status != PR_SUCCESS) { errorCode = PR_GetError(); @@ -1466,6 +1471,7 @@ NET_FinishConnect (CONST char *url, errorCode = PR_GetError(); } } + PR_Free(addr); } if(PR_SUCCESS != PR_Connect (*sock, diff --git a/network/module/nsNetFile.cpp b/network/module/nsNetFile.cpp index 5ac76905d084..5510d8888d36 100644 --- a/network/module/nsNetFile.cpp +++ b/network/module/nsNetFile.cpp @@ -207,12 +207,14 @@ void nsNetFile::GenerateGlobalRandomBytes(void *aDest, size_t aLen) { #define MAX_PATH_LEN 512 nsresult nsNetFile::GetCacheFileName(char *aDirTok, char **aRes) { - char file_buf[MAX_PATH_LEN]; + char *file_buf = nsnull; char *ext = ".MOZ"; char *prefix = "M"; PRStatus status; PRFileInfo statinfo; char *dir = (char*)PL_HashTableLookup(mHTDirs, aDirTok); + + file_buf = (char*)PR_Calloc(1, MAX_PATH_LEN); if (!dir) return NS_ERROR_FAILURE; diff --git a/network/module/nsNetStubs.cpp b/network/module/nsNetStubs.cpp index 66a7194a6fa3..0b154a1209d0 100644 --- a/network/module/nsNetStubs.cpp +++ b/network/module/nsNetStubs.cpp @@ -924,6 +924,7 @@ WH_FileName (const char *NetName, XP_FileType type) } rv = fileMgr->GetFilePath( (aName ? aName : NetName), &path); + PR_FREEIF(aName); if (rv != NS_OK) return NULL; @@ -949,10 +950,11 @@ WH_TempName(XP_FileType type, const char * prefix) } rv = fileMgr->GetCacheFileName(aName, &path); + PR_FREEIF(aName); if (rv != NS_OK) return NULL; - return PL_strdup(path); + return path; } PUBLIC int @@ -1050,8 +1052,9 @@ NET_I_XP_FileOpen(const char * name, XP_FileType type, const XP_FilePerm perm) /* call OpenFile with nsNetFile syntax if necesary. */ if ( (!name || !*name) || type == xpCache ) { - nsString newName; - newName = xpFileTypeToName(type); + char *tmpName = xpFileTypeToName(type); + nsString newName = tmpName; + PR_FREEIF(tmpName) if (newName.Length() < 1) { PR_Free(trans); PR_Free(xpFp); @@ -1062,7 +1065,8 @@ NET_I_XP_FileOpen(const char * name, XP_FileType type, const XP_FilePerm perm) } rv = fileMgr->OpenFile( (aName ? aName : name), mode, &nsFp); - PR_Free(aName); + if (aName) + delete aName; if (NS_OK != rv) { return NULL; } @@ -1180,8 +1184,9 @@ NET_I_XP_FileRemove(const char * name, XP_FileType type) if ( (!name || !*name) || type == xpCache ) { - nsString newName; - newName = xpFileTypeToName(type); + char *tmpName = xpFileTypeToName(type); + nsString newName = tmpName; + PR_FREEIF(tmpName); if (newName.Length() < 1) { return NULL; } @@ -1196,6 +1201,8 @@ NET_I_XP_FileRemove(const char * name, XP_FileType type) } rv = fileMgr->FileRemove((aName ? aName : name)); + if (aName) + delete aName; if (rv != NS_OK) return -1; @@ -1208,17 +1215,18 @@ NET_I_XP_Stat(const char * name, XP_StatStruct * info, XP_FileType type) int result = -1; PRFileInfo fileInfo; PRStatus status; - char *newName; + char *newName, *tmpName; nsString ourName; char *path; nsresult rv; switch (type) { case xpCache: - ourName = xpFileTypeToName(type); - if (ourName.Length() < 1) { + tmpName = xpFileTypeToName(type); + if (!tmpName) return NULL; - } + ourName = tmpName; + PR_FREEIF(tmpName); ourName.Append(name); newName = ourName.ToNewCString();