Fix the implementations of WelsStrncpy

Make the MSVC "safe" version truncate instead of aborting the
process if the buffer is too small.

Update all the other functions to use the right parameter
(iSizeInBytes, not iCount) as 'n' parameter to strncpy.
(By passing iCount as parameter to the normal strncpy functions,
it meant that the resulting buffer actually never was null
terminated.)

Additionally make sure that the other implementations of WelsStrncpy
always null terminate the resulting buffer, just as the MSVC safe
version does when passed the _TRUNCATE parameter.
This commit is contained in:
Martin Storsjö 2014-01-28 12:08:35 +02:00
parent 74e7c9b6d8
commit a16ccc0b4d

View File

@ -75,7 +75,7 @@ int32_t WelsSnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFor
}
str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
strncpy_s (pDest, iSizeInBytes, kpSrc, iCount);
strncpy_s (pDest, iSizeInBytes, kpSrc, _TRUNCATE);
return pDest;
}
@ -131,7 +131,8 @@ int32_t WelsSnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFor
}
str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
strncpy (pDest, kpSrc, iCount); //confirmed_safe_unsafe_usage
strncpy (pDest, kpSrc, iSizeInBytes); //confirmed_safe_unsafe_usage
pDest[iSizeInBytes - 1] = '\0';
return pDest;
}
@ -188,7 +189,9 @@ int32_t WelsSnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFor
}
str_t* WelsStrncpy (str_t* pDest, int32_t iSizeInBytes, const str_t* kpSrc, int32_t iCount) {
return strncpy (pDest, kpSrc, iCount); //confirmed_safe_unsafe_usage
strncpy (pDest, kpSrc, iSizeInBytes); //confirmed_safe_unsafe_usage
pDest[iSizeInBytes - 1] = '\0';
return pDest;
}
int32_t WelsVsnprintf (str_t* pBuffer, int32_t iSizeOfBuffer, const str_t* kpFormat, va_list pArgPtr) {