- bugfix for memory leak

This commit is contained in:
SoftCoder
2013-12-16 17:48:52 -08:00
parent d1f9fbc4a7
commit 1463dc0fc1

View File

@@ -2435,13 +2435,16 @@ string getUserHome() {
} }
string safeCharPtrCopy(const char *ptr,int maxLength) { string safeCharPtrCopy(const char *ptr,int maxLength) {
string result = "";
if(ptr == NULL) { if(ptr == NULL) {
return ""; return result;
} }
if(maxLength <= 0) { if(maxLength <= 0) {
maxLength = 8096; maxLength = 8096;
} }
int ptrLength = (int)strlen(ptr);
if(ptrLength >= maxLength) {
char *pBuffer = new char[maxLength+1]; char *pBuffer = new char[maxLength+1];
memset(pBuffer,0,maxLength+1); memset(pBuffer,0,maxLength+1);
#ifdef WIN32 #ifdef WIN32
@@ -2449,7 +2452,14 @@ string safeCharPtrCopy(const char *ptr,int maxLength) {
#else #else
memcpy(pBuffer,ptr,std::min((int)strlen(ptr),maxLength)); memcpy(pBuffer,ptr,std::min((int)strlen(ptr),maxLength));
#endif #endif
return pBuffer;
result = pBuffer;
delete [] pBuffer;
}
else {
result = ptr;
}
return result;
} }