- bugfix for memory leak

This commit is contained in:
SoftCoder 2013-12-16 17:48:52 -08:00
parent d1f9fbc4a7
commit 1463dc0fc1
1 changed files with 16 additions and 6 deletions

View File

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