History

Fix calculating buffer positions when appending with SNPRINTF/WelsSnprintf

This fixes two separate issues. First, with the MSVC _snprintf implementations, the return value is negative if the buffer wasn't large enough - this would in the worst case lead to making iBufferUsed negative, writing before the start of the buffer. Secondly, when both iBufferUsed and iBufferLeft are accumulated, one can't do "iBufferLeft -= iBufferUsed;". As an example, say the buffer is 100 bytes in total and iBufferLeft is 40 and iBufferUsed is 60. If SNPRINTF then writes 5 more bytes to the buffer, iBufferUsed would be 65, but if we now do "iBufferLeft -= iBufferUsed;" then iBufferLeft would end up as -25 even though there's 35 bytes left in the buffer to use. Therefore, we use a separate variable to store the return value from the latest SNPRINTF call. This is checked to make sure it wasn't negative, and only this amount is added to iBufferUsed and subtracted from iBufferLeft. This is the same pattern used in codec/encoder/core/src/utils.cpp. strftime never returns negative numbers, so those calls don't need as much checking.

Martin Storsjö committed 12 years ago

Commit 076b1111 parent a344d102 files · +67 −35Browse files

Showing the changes to codec/decoder/plus/src/welsDecoderExt.cpp (1 of 2 files). Show all

Unified · Split

codec/decoder/plus/src/welsDecoderExt.cpp+29−13
@@ -90,10 +90,12 @@ CWelsDecoder::CWelsDecoder (void_t) · 89 unmodified lines
9090 str_t chFileName[1024] = { 0 }; //for .264
9191 int iBufUsed = 0;
9292 int iBufLeft = 1023;
93+ int iCurUsed;
9394
9495 str_t chFileNameSize[1024] = { 0 }; //for .len
9596 int iBufUsedSize = 0;
9697 int iBufLeftSize = 1023;
98+ int iCurUsedSize;
9799 #endif//OUTPUT_BIT_STREAM
98100
99101 m_pTrace = CreateWelsTrace (Wels_Trace_Type);
@@ -106,30 +108,44 @@ CWelsDecoder::CWelsDecoder (void_t) · 6 unmodified lines
106108
107109 WelsGetTimeOfDay (&sCurTime);
108110
109− iBufUsed += WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void_t*)this);
110− iBufUsedSize += WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void_t*)this);
111+ iCurUsed = WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void_t*)this);
112+ iCurUsedSize = WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void_t*)this);
111113
112− iBufLeft -= iBufUsed;
114+ if (iCurUsed > 0) {
115+ iBufUsed += iCurUsed;
116+ iBufLeft -= iCurUsed;
117+ }
113118 if (iBufLeft > 0) {
114− iBufUsed += WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime);
115− iBufLeft -= iBufUsed;
119+ iCurUsed = WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime);
120+ iBufUsed += iCurUsed;
121+ iBufLeft -= iCurUsed;
116122 }
117123
118− iBufLeftSize -= iBufUsedSize;
124+ if (iCurUsedSize > 0) {
125+ iBufUsedSize += iCurUsedSize;
126+ iBufLeftSize -= iCurUsedSize;
127+ }
119128 if (iBufLeftSize > 0) {
120− iBufUsedSize += WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime);
121− iBufLeftSize -= iBufUsedSize;
129+ iCurUsedSize = WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime);
130+ iBufUsedSize += iCurUsedSize;
131+ iBufLeftSize -= iCurUsedSize;
122132 }
123133
124134 if (iBufLeft > 0) {
125− iBufUsed += WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillsecond (&sCurTime));
126− iBufLeft -= iBufUsed;
135+ iCurUsed = WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillsecond (&sCurTime));
136+ if (iCurUsed > 0) {
137+ iBufUsed += iCurUsed;
138+ iBufLeft -= iCurUsed;
139+ }
127140 }
128141
129142 if (iBufLeftSize > 0) {
130− iBufUsedSize += WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len",
131− WelsGetMillsecond (&sCurTime));
132− iBufLeftSize -= iBufUsedSize;
143+ iCurUsedSize = WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len",
144+ WelsGetMillsecond (&sCurTime));
145+ if (iCurUsedSize > 0) {
146+ iBufUsedSize += iCurUsedSize;
147+ iBufLeftSize -= iCurUsedSize;
148+ }
133149 }
134150
135151

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t