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/encoder/plus/src/welsEncoderExt.cpp (1 of 2 files). Show all

Unified · Split

codec/encoder/plus/src/welsEncoderExt.cpp+38−22
@@ -77,10 +77,12 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 76 unmodified lines
7777 str_t strStreamFileName[1024] = { 0 }; //for .264
7878 int32_t iBufferUsed = 0;
7979 int32_t iBufferLeft = 1023;
80+ int32_t iCurUsed;
8081
8182 str_t strLenFileName[1024] = { 0 }; //for .len
8283 int32_t iBufferUsedSize = 0;
8384 int32_t iBufferLeftSize = 1023;
85+ int32_t iCurUsedSize;
8486 #endif//OUTPUT_BIT_STREAM
8587
8688 #ifdef OUTPUT_BIT_STREAM
@@ -118,26 +120,29 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 31 unmodified lines
118120 #ifdef _WIN32
119121 #if defined(_MSC_VER)
120122 #if _MSC_VER>=1500
121− iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, iBufferLeft, "enc_bs_0x%p_", (void*)this);
122− iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
123+ iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, iBufferLeft, "enc_bs_0x%p_", (void*)this);
124+ iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
123125 #else
124− iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this);
125− iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
126+ iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this);
127+ iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
126128 #endif//_MSC_VER>=1500
127129 #endif//_MSC_VER
128130 #else
129− iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, "/tmp/enc_bs_0x%p_", (void*)this);
130− iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, "/tmp/enc_size_0x%p", (void*)this);
131+ iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, "/tmp/enc_bs_0x%p_", (void*)this);
132+ iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, "/tmp/enc_size_0x%p", (void*)this);
131133 #endif//WIN32
132134
133135
134− iBufferLeft -= iBufferUsed;
136+ if (iCurUsed > 0) {
137+ iBufferUsed += iCurUsed;
138+ iBufferLeft -= iCurUsed;
139+ }
135140 if (iBufferLeft > 0) {
136141 #if defined(_GNUC__)
137− iBufferUsed += strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", tTimeNow);
142+ iCurUsed = strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", tTimeNow);
138143 #else
139144 #if defined(_MSC_VER)
140− iBufferUsed += strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S",
145+ iCurUsed = strftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S",
141146 #if _MSC_VER>=1500
142147 & tTimeNow
143148 #else
@@ -146,16 +151,20 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 2 unmodified lines
146151 );
147152 #endif//_MSC_VER
148153 #endif//__GNUC__
149− iBufferLeft -= iBufferUsed;
154+ iBufferUsed += iCurUsed;
155+ iBufferLeft -= iCurUsed;
150156 }
151157
152− iBufferLeftSize -= iBufferUsedSize;
158+ if (iCurUsedSize > 0) {
159+ iBufferUsedSize += iCurUsedSize;
160+ iBufferLeftSize -= iCurUsedSize;
161+ }
153162 if (iBufferLeftSize > 0) {
154163 #if defined(_GNUC__)
155− iBufferUsedSize += strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", tTimeNow);
164+ iCurUsedSize = strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", tTimeNow);
156165 #else
157166 #if defined(_MSC_VER)
158− iBufferUsedSize += strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S",
167+ iCurUsedSize = strftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S",
159168 #if _MSC_VER>=1500
160169 & tTimeNow
161170 #else
@@ -164,38 +173,45 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 2 unmodified lines
164173 );
165174 #endif//_MSC_VER
166175 #endif//__GNUC__
167− iBufferLeftSize -= iBufferUsedSize;
176+ iBufferUsedSize += iCurUsedSize;
177+ iBufferLeftSize -= iCurUsedSize;
168178 }
169179
170180 if (iBufferLeft > 0) {
171181 #ifdef _WIN32
172182 #if defined(_MSC_VER)
173183 #if _MSC_VER>=1500
174− iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, iBufferLeft, ".%03.3u.264", tTimeb.millitm);
184+ iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, iBufferLeft, ".%03.3u.264", tTimeb.millitm);
175185 #else
176− iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm);
186+ iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm);
177187 #endif//_MSC_VER>=1500
178188 #endif//_MSC_VER
179189 #else
180− iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimev.tv_usec / 1000);
190+ iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimev.tv_usec / 1000);
181191 #endif//WIN32
182− iBufferLeft -= iBufferUsed;
192+ if (iCurUsed > 0) {
193+ iBufferUsed += iCurUsed;
194+ iBufferLeft -= iCurUsed;
195+ }
183196 }
184197
185198 if (iBufferLeftSize > 0) {
186199 #ifdef _WIN32
187200 #if defined(_MSC_VER)
188201 #if _MSC_VER>=1500
189− iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, iBufferLeftSize, ".%03.3u.len",
202+ iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, iBufferLeftSize, ".%03.3u.len",
190203 tTimeb.millitm);
191204 #else
192− iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm);
205+ iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm);
193206 #endif//_MSC_VER>=1500
194207 #endif//_MSC_VER
195208 #else
196− iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimev.tv_usec / 1000);
209+ iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimev.tv_usec / 1000);
197210 #endif//WIN32
198− iBufferLeftSize -= iBufferUsedSize;
211+ if (iCurUsedSize > 0) {
212+ iBufferUsedSize += iCurUsedSize;
213+ iBufferLeftSize -= iCurUsedSize;
214+ }
199215 }
200216
201217 #if defined(__GNUC__)

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