Merge pull request #233 from mstorsjo/snprintf-fixes

Make the *snprintf (and similar) functions always null terminate the buffer properly on all platforms
This commit is contained in:
Ethan Hugg 2014-01-27 08:52:37 -08:00
commit 5a8f5f9ad1
3 changed files with 29 additions and 12 deletions

View File

@ -87,8 +87,8 @@ int32_t WelsStrnlen (const str_t* kpStr, int32_t iMaxlen) {
return strnlen_s (kpStr, iMaxlen);
}
int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
return vsprintf_s (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
int32_t WelsVsnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
return vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
}
WelsFileHandle* WelsFopen (const str_t* kpFilename, const str_t* kpMode) {
@ -110,10 +110,14 @@ int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
struct tm sTimeNow;
int32_t iRc;
localtime_s (&sTimeNow, &kpTp->time);
return strftime (pBuffer, iSize, kpFormat, &sTimeNow);
iRc = strftime (pBuffer, iSize, kpFormat, &sTimeNow);
if (iRc == 0)
pBuffer[0] = '\0';
return iRc;
}
#else
@ -124,7 +128,9 @@ int32_t WelsSnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFor
va_start (pArgPtr, kpFormat);
iRc = vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
if (iRc < 0)
pBuffer[iSizeOfBuffer - 1] = '\0';
va_end (pArgPtr);
@ -141,8 +147,11 @@ int32_t WelsStrnlen (const str_t* kpStr, int32_t iMaxlen) {
return strlen (kpStr); //confirmed_safe_unsafe_usage
}
int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
return vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
int32_t WelsVsnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
int32_t iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
if (iRc < 0)
pBuffer[iSizeOfBuffer - 1] = '\0';
return iRc;
}
@ -161,10 +170,14 @@ int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
struct tm* pTnow;
int32_t iRc;
pTnow = localtime (&kpTp->time);
return strftime (pBuffer, iSize, kpFormat, pTnow);
iRc = strftime (pBuffer, iSize, kpFormat, pTnow);
if (iRc == 0)
pBuffer[0] = '\0';
return iRc;
}
@ -209,8 +222,8 @@ int32_t WelsStrnlen (const str_t* kpString, int32_t iMaxlen) {
}
#endif
int32_t WelsVsprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
return vsprintf (pBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
int32_t WelsVsnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {
return vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
}
WelsFileHandle* WelsFopen (const str_t* kpFilename, const str_t* kpMode) {
@ -236,10 +249,14 @@ int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
int32_t WelsStrftime (str_t* pBuffer, int32_t iSize, const str_t* kpFormat, const SWelsTime* kpTp) {
struct tm* pTnow;
int32_t iRc;
pTnow = localtime (&kpTp->time);
return strftime (pBuffer, iSize, kpFormat, pTnow);
iRc = strftime (pBuffer, iSize, kpFormat, pTnow);
if (iRc == 0)
pBuffer[0] = '\0';
return iRc;
}
#endif

View File

@ -79,7 +79,7 @@ int32_t WelsSnprintf (str_t* buffer, int32_t sizeOfBuffer, const str_t* form
str_t* WelsStrncpy (str_t* dest, int32_t sizeInBytes, const str_t* src, int32_t count);
str_t* WelsStrcat (str_t* dest, int32_t sizeInBytes, str_t* src);
int32_t WelsStrnlen (const str_t* str, int32_t maxlen);
int32_t WelsVsprintf (str_t* buffer, int32_t sizeOfBuffer, const str_t* format, va_list argptr);
int32_t WelsVsnprintf (str_t* buffer, int32_t sizeOfBuffer, const str_t* format, va_list argptr);
WelsFileHandle* WelsFopen (const str_t* filename, const str_t* mode);
int32_t WelsFclose (WelsFileHandle* fp);

View File

@ -68,7 +68,7 @@ int32_t CWelsTraceBase::Trace (const int kLevel, const str_t* kpFormat, va_list
WelsStrncpy (chBuf, MAX_LOG_SIZE, (const str_t*)"[DECODER]: ", kLen);
WelsVsprintf ((chBuf + kLen), MAX_LOG_SIZE - kLen, (const str_t*)kpFormat, pVl);
WelsVsnprintf ((chBuf + kLen), MAX_LOG_SIZE - kLen, (const str_t*)kpFormat, pVl);
WelsStrncpy (chResult, MAX_LOG_SIZE, (const str_t*)chBuf, WelsStrnlen ((const str_t*)chBuf, MAX_LOG_SIZE));
WriteString (kLevel, chResult);