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
90 str_t chFileName[1024] = { 0 }; //for .26490 str_t chFileName[1024] = { 0 }; //for .264
91 int iBufUsed = 0;91 int iBufUsed = 0;
92 int iBufLeft = 1023;92 int iBufLeft = 1023;
93 int iCurUsed;
93 94
94 str_t chFileNameSize[1024] = { 0 }; //for .len95 str_t chFileNameSize[1024] = { 0 }; //for .len
95 int iBufUsedSize = 0;96 int iBufUsedSize = 0;
96 int iBufLeftSize = 1023;97 int iBufLeftSize = 1023;
98 int iCurUsedSize;
97#endif//OUTPUT_BIT_STREAM99#endif//OUTPUT_BIT_STREAM
98 100
99 m_pTrace = CreateWelsTrace (Wels_Trace_Type);101 m_pTrace = CreateWelsTrace (Wels_Trace_Type);
@@ -106,30 +108,44 @@ CWelsDecoder::CWelsDecoder (void_t) · 6 unmodified lines
106 108
107 WelsGetTimeOfDay (&sCurTime);109 WelsGetTimeOfDay (&sCurTime);
108 110
109 iBufUsed += WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void_t*)this);111 iCurUsed = WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void_t*)this);
110 iBufUsedSize += WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void_t*)this);112 iCurUsedSize = WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void_t*)this);
111 113
112 iBufLeft -= iBufUsed;114 if (iCurUsed > 0) {
115 iBufUsed += iCurUsed;
116 iBufLeft -= iCurUsed;
117 }
113 if (iBufLeft > 0) {118 if (iBufLeft > 0) {
114 iBufUsed += WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime);119 iCurUsed = WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime);
115 iBufLeft -= iBufUsed;120 iBufUsed += iCurUsed;
121 iBufLeft -= iCurUsed;
116 }122 }
117 123
118 iBufLeftSize -= iBufUsedSize;124 if (iCurUsedSize > 0) {
125 iBufUsedSize += iCurUsedSize;
126 iBufLeftSize -= iCurUsedSize;
127 }
119 if (iBufLeftSize > 0) {128 if (iBufLeftSize > 0) {
120 iBufUsedSize += WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime);129 iCurUsedSize = WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime);
121 iBufLeftSize -= iBufUsedSize;130 iBufUsedSize += iCurUsedSize;
131 iBufLeftSize -= iCurUsedSize;
122 }132 }
123 133
124 if (iBufLeft > 0) {134 if (iBufLeft > 0) {
125 iBufUsed += WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillsecond (&sCurTime));135 iCurUsed = WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillsecond (&sCurTime));
126 iBufLeft -= iBufUsed;136 if (iCurUsed > 0) {
137 iBufUsed += iCurUsed;
138 iBufLeft -= iCurUsed;
139 }
127 }140 }
128 141
129 if (iBufLeftSize > 0) {142 if (iBufLeftSize > 0) {
130 iBufUsedSize += WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len",143 iCurUsedSize = WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len",
131 WelsGetMillsecond (&sCurTime));144 WelsGetMillsecond (&sCurTime));
132 iBufLeftSize -= iBufUsedSize;145 if (iCurUsedSize > 0) {
146 iBufUsedSize += iCurUsedSize;
147 iBufLeftSize -= iCurUsedSize;
148 }
133 }149 }
134 150
135 151

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