ref: 564d16c2efbb70c296a815ebd35ae69033475915
parent: ade7df0ca74233ef7588c36926eedcc25540fb05
author: Martin Storsjö <martin@martin.st>
date: Mon Apr 21 17:58:22 EDT 2014
Make Wels*Snprintf return values be non-negative This makes sure the windows version of these functions behave more like the posix version. The posix *snprintf returns how much would have been written if the buffer had been large enough, which we don't know easily in the windows versions. This basically means that we can assume that the return value is >= 0 now, which can simplify the calling code.
--- a/codec/common/src/crt_util_safe_x.cpp
+++ b/codec/common/src/crt_util_safe_x.cpp
@@ -68,6 +68,8 @@
va_start (pArgPtr, kpFormat);
iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
+ if (iRc < 0)
+ iRc = iSizeOfBuffer;
va_end (pArgPtr);
@@ -81,7 +83,10 @@
}
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
- return vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
+ int32_t iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
+ if (iRc < 0)
+ iRc = iSizeOfBuffer;
+ return iRc;
}
WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
@@ -122,8 +127,10 @@
va_start (pArgPtr, kpFormat);
iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
- if (iRc < 0)
+ if (iRc < 0) {
pBuffer[iSizeOfBuffer - 1] = '\0';
+ iRc = iSizeOfBuffer;
+ }
va_end (pArgPtr);
@@ -139,8 +146,10 @@
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
int32_t iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
- if (iRc < 0)
+ if (iRc < 0) {
pBuffer[iSizeOfBuffer - 1] = '\0';
+ iRc = iSizeOfBuffer;
+ }
return iRc;
}