issue #1166: Checked whether memory is allocated successfully in CCString::initWithFormatAndValist function.

This commit is contained in:
James Chen 2012-04-16 14:27:49 +08:00
parent be129913a8
commit 0af443a56f
1 changed files with 10 additions and 6 deletions

View File

@ -37,11 +37,16 @@ CCString& CCString::operator= (const CCString& other)
bool CCString::initWithFormatAndValist(const char* format, va_list ap) bool CCString::initWithFormatAndValist(const char* format, va_list ap)
{ {
bool bRet = false;
char* pBuf = (char*)malloc(kMaxStringLen); char* pBuf = (char*)malloc(kMaxStringLen);
vsnprintf(pBuf, kMaxStringLen, format, ap); if (pBuf != NULL)
m_sString = pBuf; {
free(pBuf); vsnprintf(pBuf, kMaxStringLen, format, ap);
return true; m_sString = pBuf;
free(pBuf);
bRet = true;
}
return bRet;
} }
bool CCString::initWithFormat(const char* format, ...) bool CCString::initWithFormat(const char* format, ...)
@ -152,13 +157,12 @@ CCString* CCString::stringWithData(unsigned char* pData, unsigned long nLen)
CCString* pRet = NULL; CCString* pRet = NULL;
if (pData != NULL && nLen > 0) if (pData != NULL && nLen > 0)
{ {
pRet = CCString::stringWithCString("");
char* pStr = (char*)malloc(nLen+1); char* pStr = (char*)malloc(nLen+1);
if (pStr != NULL) if (pStr != NULL)
{ {
pStr[nLen] = '\0'; pStr[nLen] = '\0';
memcpy(pStr, pData, nLen); memcpy(pStr, pData, nLen);
pRet->m_sString = pStr; pRet = CCString::stringWithCString(pStr);
free(pStr); free(pStr);
} }
} }