Merge pull request #227 from mstorsjo/snprintf-fixes

Fix handling of snprintf return values and buffer size calculations
This commit is contained in:
volvet 2014-01-26 22:26:38 -08:00
commit cbe650a3e5
3 changed files with 81 additions and 57 deletions

View File

@ -90,10 +90,12 @@ CWelsDecoder::CWelsDecoder (void_t)
str_t chFileName[1024] = { 0 }; //for .264 str_t chFileName[1024] = { 0 }; //for .264
int iBufUsed = 0; int iBufUsed = 0;
int iBufLeft = 1023; int iBufLeft = 1023;
int iCurUsed;
str_t chFileNameSize[1024] = { 0 }; //for .len str_t chFileNameSize[1024] = { 0 }; //for .len
int iBufUsedSize = 0; int iBufUsedSize = 0;
int iBufLeftSize = 1023; int iBufLeftSize = 1023;
int iCurUsedSize;
#endif//OUTPUT_BIT_STREAM #endif//OUTPUT_BIT_STREAM
m_pTrace = CreateWelsTrace (Wels_Trace_Type); m_pTrace = CreateWelsTrace (Wels_Trace_Type);
@ -106,30 +108,44 @@ CWelsDecoder::CWelsDecoder (void_t)
WelsGetTimeOfDay (&sCurTime); WelsGetTimeOfDay (&sCurTime);
iBufUsed += WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void_t*)this); iCurUsed = WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void_t*)this);
iBufUsedSize += WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void_t*)this); iCurUsedSize = WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void_t*)this);
iBufLeft -= iBufUsed; if (iCurUsed > 0) {
if (iBufLeft > iBufUsed) { iBufUsed += iCurUsed;
iBufUsed += WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime); iBufLeft -= iCurUsed;
iBufLeft -= iBufUsed; }
if (iBufLeft > 0) {
iCurUsed = WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime);
iBufUsed += iCurUsed;
iBufLeft -= iCurUsed;
} }
iBufLeftSize -= iBufUsedSize; if (iCurUsedSize > 0) {
if (iBufLeftSize > iBufUsedSize) { iBufUsedSize += iCurUsedSize;
iBufUsedSize += WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime); iBufLeftSize -= iCurUsedSize;
iBufLeftSize -= iBufUsedSize; }
if (iBufLeftSize > 0) {
iCurUsedSize = WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime);
iBufUsedSize += iCurUsedSize;
iBufLeftSize -= iCurUsedSize;
} }
if (iBufLeft > iBufUsed) { if (iBufLeft > 0) {
iBufUsed += WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillsecond (&sCurTime)); iCurUsed = WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillsecond (&sCurTime));
iBufLeft -= iBufUsed; if (iCurUsed > 0) {
iBufUsed += iCurUsed;
iBufLeft -= iCurUsed;
}
} }
if (iBufLeftSize > iBufUsedSize) { if (iBufLeftSize > 0) {
iBufUsedSize += WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len", iCurUsedSize = WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len",
WelsGetMillsecond (&sCurTime)); WelsGetMillsecond (&sCurTime));
iBufLeftSize -= iBufUsedSize; if (iCurUsedSize > 0) {
iBufUsedSize += iCurUsedSize;
iBufLeftSize -= iCurUsedSize;
}
} }

View File

@ -443,7 +443,6 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara
while (iIdx < iThreadNum) { while (iIdx < iThreadNum) {
#if defined(__GNUC__) && !defined(_WIN32) // for posix threading #if defined(__GNUC__) && !defined(_WIN32) // for posix threading
str_t name[SEM_NAME_MAX] = {0}; str_t name[SEM_NAME_MAX] = {0};
int32_t used_len = 0;
WELS_THREAD_ERROR_CODE err = 0; WELS_THREAD_ERROR_CODE err = 0;
#endif//__GNUC__ #endif//__GNUC__
pSmt->pThreadPEncCtx[iIdx].pWelsPEncCtx = (void*) (*ppCtx); pSmt->pThreadPEncCtx[iIdx].pWelsPEncCtx = (void*) (*ppCtx);
@ -462,8 +461,7 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara
#if defined(ENABLE_TRACE_MT) #if defined(ENABLE_TRACE_MT)
WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pUpdateMbListEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pUpdateMbListEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno);
#endif #endif
used_len = SNPRINTF (name, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (name, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx));
name[used_len] = '\0';
err = WelsEventOpen (&pSmt->pFinUpdateMbListEvent[iIdx], name); err = WelsEventOpen (&pSmt->pFinUpdateMbListEvent[iIdx], name);
#if defined(ENABLE_TRACE_MT) #if defined(ENABLE_TRACE_MT)
WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pFinUpdateMbListEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pFinUpdateMbListEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno);
@ -477,14 +475,12 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara
WelsEventInit (&pSmt->pFinSliceCodingEvent[iIdx]); WelsEventInit (&pSmt->pFinSliceCodingEvent[iIdx]);
WelsEventInit (&pSmt->pExitEncodeEvent[iIdx]); WelsEventInit (&pSmt->pExitEncodeEvent[iIdx]);
#else #else
used_len = SNPRINTF (name, SEM_NAME_MAX, "sc%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (name, SEM_NAME_MAX, "sc%d%p", iIdx, (void*) (*ppCtx));
name[used_len] = '\0';
err = WelsEventOpen (&pSmt->pSliceCodedEvent[iIdx], name); err = WelsEventOpen (&pSmt->pSliceCodedEvent[iIdx], name);
#if defined(ENABLE_TRACE_MT) #if defined(ENABLE_TRACE_MT)
WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pSliceCodedEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pSliceCodedEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno);
#endif #endif
used_len = SNPRINTF (name, SEM_NAME_MAX, "rc%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (name, SEM_NAME_MAX, "rc%d%p", iIdx, (void*) (*ppCtx));
name[used_len] = '\0';
err = WelsEventOpen (&pSmt->pReadySliceCodingEvent[iIdx], name); err = WelsEventOpen (&pSmt->pReadySliceCodingEvent[iIdx], name);
#if defined(ENABLE_TRACE_MT) #if defined(ENABLE_TRACE_MT)
WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d\n", iIdx, WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d\n", iIdx,
@ -577,19 +573,15 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) {
#endif//DYNAMIC_SLICE_ASSIGN && TRY_SLICING_BALANCE #endif//DYNAMIC_SLICE_ASSIGN && TRY_SLICING_BALANCE
#else #else
str_t ename[SEM_NAME_MAX] = {0}; str_t ename[SEM_NAME_MAX] = {0};
int32_t used_len = 0;
// length of semaphore name should be system constrained at least on mac 10.7 // length of semaphore name should be system constrained at least on mac 10.7
SNPRINTF (ename, SEM_NAME_MAX, "sc%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (ename, SEM_NAME_MAX, "sc%d%p", iIdx, (void*) (*ppCtx));
WelsEventClose (pSmt->pSliceCodedEvent[iIdx], ename); WelsEventClose (pSmt->pSliceCodedEvent[iIdx], ename);
used_len = SNPRINTF (ename, SEM_NAME_MAX, "rc%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (ename, SEM_NAME_MAX, "rc%d%p", iIdx, (void*) (*ppCtx));
ename[used_len] = '\0';
WelsEventClose (pSmt->pReadySliceCodingEvent[iIdx], ename); WelsEventClose (pSmt->pReadySliceCodingEvent[iIdx], ename);
#if defined(DYNAMIC_SLICE_ASSIGN) && defined(TRY_SLICING_BALANCE) #if defined(DYNAMIC_SLICE_ASSIGN) && defined(TRY_SLICING_BALANCE)
used_len = SNPRINTF (ename, SEM_NAME_MAX, "ud%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (ename, SEM_NAME_MAX, "ud%d%p", iIdx, (void*) (*ppCtx));
ename[used_len] = '\0';
WelsEventClose (pSmt->pUpdateMbListEvent[iIdx], ename); WelsEventClose (pSmt->pUpdateMbListEvent[iIdx], ename);
used_len = SNPRINTF (ename, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx)); SNPRINTF (ename, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx));
ename[used_len] = '\0';
WelsEventClose (pSmt->pFinUpdateMbListEvent[iIdx], ename); WelsEventClose (pSmt->pFinUpdateMbListEvent[iIdx], ename);
#endif//DYNAMIC_SLICE_ASSIGN && TRY_SLICING_BALANCE #endif//DYNAMIC_SLICE_ASSIGN && TRY_SLICING_BALANCE
#endif//_WIN32 #endif//_WIN32

View File

@ -77,10 +77,12 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder()
str_t strStreamFileName[1024] = { 0 }; //for .264 str_t strStreamFileName[1024] = { 0 }; //for .264
int32_t iBufferUsed = 0; int32_t iBufferUsed = 0;
int32_t iBufferLeft = 1023; int32_t iBufferLeft = 1023;
int32_t iCurUsed;
str_t strLenFileName[1024] = { 0 }; //for .len str_t strLenFileName[1024] = { 0 }; //for .len
int32_t iBufferUsedSize = 0; int32_t iBufferUsedSize = 0;
int32_t iBufferLeftSize = 1023; int32_t iBufferLeftSize = 1023;
int32_t iCurUsedSize;
#endif//OUTPUT_BIT_STREAM #endif//OUTPUT_BIT_STREAM
#ifdef OUTPUT_BIT_STREAM #ifdef OUTPUT_BIT_STREAM
@ -118,26 +120,29 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder()
#ifdef _WIN32 #ifdef _WIN32
#if defined(_MSC_VER) #if defined(_MSC_VER)
#if _MSC_VER>=1500 #if _MSC_VER>=1500
iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, iBufferLeft, "enc_bs_0x%p_", (void*)this); iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, iBufferLeft, "enc_bs_0x%p_", (void*)this);
iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, iBufferLeftSize, "enc_size_0x%p_", (void*)this); iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
#else #else
iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this); iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this);
iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this); iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
#endif//_MSC_VER>=1500 #endif//_MSC_VER>=1500
#endif//_MSC_VER #endif//_MSC_VER
#else #else
iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, "/tmp/enc_bs_0x%p_", (void*)this); iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, "/tmp/enc_bs_0x%p_", (void*)this);
iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, "/tmp/enc_size_0x%p", (void*)this); iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, "/tmp/enc_size_0x%p", (void*)this);
#endif//WIN32 #endif//WIN32
iBufferLeft -= iBufferUsed; if (iCurUsed > 0) {
if (iBufferLeft > iBufferUsed) { iBufferUsed += iCurUsed;
iBufferLeft -= iCurUsed;
}
if (iBufferLeft > 0) {
#if defined(_GNUC__) #if defined(_GNUC__)
iBufferUsed += strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", tTimeNow); iCurUsed = strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", tTimeNow);
#else #else
#if defined(_MSC_VER) #if defined(_MSC_VER)
iBufferUsed += strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", iCurUsed = strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S",
#if _MSC_VER>=1500 #if _MSC_VER>=1500
& tTimeNow & tTimeNow
#else #else
@ -146,16 +151,20 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder()
); );
#endif//_MSC_VER #endif//_MSC_VER
#endif//__GNUC__ #endif//__GNUC__
iBufferLeft -= iBufferUsed; iBufferUsed += iCurUsed;
iBufferLeft -= iCurUsed;
} }
iBufferLeftSize -= iBufferUsedSize; if (iCurUsedSize > 0) {
if (iBufferLeftSize > iBufferUsedSize) { iBufferUsedSize += iCurUsedSize;
iBufferLeftSize -= iCurUsedSize;
}
if (iBufferLeftSize > 0) {
#if defined(_GNUC__) #if defined(_GNUC__)
iBufferUsedSize += strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", tTimeNow); iCurUsedSize = strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", tTimeNow);
#else #else
#if defined(_MSC_VER) #if defined(_MSC_VER)
iBufferUsedSize += strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", iCurUsedSize = strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S",
#if _MSC_VER>=1500 #if _MSC_VER>=1500
& tTimeNow & tTimeNow
#else #else
@ -164,38 +173,45 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder()
); );
#endif//_MSC_VER #endif//_MSC_VER
#endif//__GNUC__ #endif//__GNUC__
iBufferLeftSize -= iBufferUsedSize; iBufferUsedSize += iCurUsedSize;
iBufferLeftSize -= iCurUsedSize;
} }
if (iBufferLeft > iBufferUsed) { if (iBufferLeft > 0) {
#ifdef _WIN32 #ifdef _WIN32
#if defined(_MSC_VER) #if defined(_MSC_VER)
#if _MSC_VER>=1500 #if _MSC_VER>=1500
iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, iBufferLeft, ".%03.3u.264", tTimeb.millitm); iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, iBufferLeft, ".%03.3u.264", tTimeb.millitm);
#else #else
iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm); iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm);
#endif//_MSC_VER>=1500 #endif//_MSC_VER>=1500
#endif//_MSC_VER #endif//_MSC_VER
#else #else
iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimev.tv_usec / 1000); iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimev.tv_usec / 1000);
#endif//WIN32 #endif//WIN32
iBufferLeft -= iBufferUsed; if (iCurUsed > 0) {
iBufferUsed += iCurUsed;
iBufferLeft -= iCurUsed;
}
} }
if (iBufferLeftSize > iBufferUsedSize) { if (iBufferLeftSize > 0) {
#ifdef _WIN32 #ifdef _WIN32
#if defined(_MSC_VER) #if defined(_MSC_VER)
#if _MSC_VER>=1500 #if _MSC_VER>=1500
iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, iBufferLeftSize, ".%03.3u.len", iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, iBufferLeftSize, ".%03.3u.len",
tTimeb.millitm); tTimeb.millitm);
#else #else
iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm); iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm);
#endif//_MSC_VER>=1500 #endif//_MSC_VER>=1500
#endif//_MSC_VER #endif//_MSC_VER
#else #else
iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimev.tv_usec / 1000); iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimev.tv_usec / 1000);
#endif//WIN32 #endif//WIN32
iBufferLeftSize -= iBufferUsedSize; if (iCurUsedSize > 0) {
iBufferUsedSize += iCurUsedSize;
iBufferLeftSize -= iCurUsedSize;
}
} }
#if defined(__GNUC__) #if defined(__GNUC__)