shithub: openh264

Download patch

ref: 5a8f5f9ad134a2946c4bb232b6b72559f0068ff6
parent: dacd6735fbedd7622609d888ae97df8ede94d7bd
parent: c7b74b2b129e02981c1decb0aa98452a4d0381b3
author: Ethan Hugg <[email protected]>
date: Mon Jan 27 03:52:37 EST 2014

Merge pull request #233 from mstorsjo/snprintf-fixes

Make the *snprintf (and similar) functions always null terminate the buffer properly on all platforms

--- a/codec/common/crt_util_safe_x.cpp
+++ b/codec/common/crt_util_safe_x.cpp
@@ -87,8 +87,8 @@
   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 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 @@
 
   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 @@
   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 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 @@
 }
 #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 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
--- a/codec/common/crt_util_safe_x.h
+++ b/codec/common/crt_util_safe_x.h
@@ -79,7 +79,7 @@
 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);
--- a/codec/decoder/plus/src/welsCodecTrace.cpp
+++ b/codec/decoder/plus/src/welsCodecTrace.cpp
@@ -68,7 +68,7 @@
 
     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);