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
77 str_t strStreamFileName[1024] = { 0 }; //for .26477 str_t strStreamFileName[1024] = { 0 }; //for .264
78 int32_t iBufferUsed = 0;78 int32_t iBufferUsed = 0;
79 int32_t iBufferLeft = 1023;79 int32_t iBufferLeft = 1023;
80 int32_t iCurUsed;
80 81
81 str_t strLenFileName[1024] = { 0 }; //for .len82 str_t strLenFileName[1024] = { 0 }; //for .len
82 int32_t iBufferUsedSize = 0;83 int32_t iBufferUsedSize = 0;
83 int32_t iBufferLeftSize = 1023;84 int32_t iBufferLeftSize = 1023;
85 int32_t iCurUsedSize;
84#endif//OUTPUT_BIT_STREAM86#endif//OUTPUT_BIT_STREAM
85 87
86#ifdef OUTPUT_BIT_STREAM88#ifdef OUTPUT_BIT_STREAM
@@ -118,26 +120,29 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 31 unmodified lines
118#ifdef _WIN32120#ifdef _WIN32
119#if defined(_MSC_VER)121#if defined(_MSC_VER)
120#if _MSC_VER>=1500122#if _MSC_VER>=1500
121 iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, iBufferLeft, "enc_bs_0x%p_", (void*)this);123 iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, iBufferLeft, "enc_bs_0x%p_", (void*)this);
122 iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, iBufferLeftSize, "enc_size_0x%p_", (void*)this);124 iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
123#else125#else
124 iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this);126 iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this);
125 iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);127 iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
126#endif//_MSC_VER>=1500128#endif//_MSC_VER>=1500
127#endif//_MSC_VER129#endif//_MSC_VER
128#else130#else
129 iBufferUsed += SNPRINTF (strStreamFileName, iBufferLeft, "/tmp/enc_bs_0x%p_", (void*)this);131 iCurUsed = SNPRINTF (strStreamFileName, iBufferLeft, "/tmp/enc_bs_0x%p_", (void*)this);
130 iBufferUsedSize += SNPRINTF (strLenFileName, iBufferLeftSize, "/tmp/enc_size_0x%p", (void*)this);132 iCurUsedSize = SNPRINTF (strLenFileName, iBufferLeftSize, "/tmp/enc_size_0x%p", (void*)this);
131#endif//WIN32133#endif//WIN32
132 134
133 135
134 iBufferLeft -= iBufferUsed;136 if (iCurUsed > 0) {
137 iBufferUsed += iCurUsed;
138 iBufferLeft -= iCurUsed;
139 }
135 if (iBufferLeft > 0) {140 if (iBufferLeft > 0) {
136#if defined(_GNUC__)141#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);
138#else143#else
139#if defined(_MSC_VER)144#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",
141#if _MSC_VER>=1500146#if _MSC_VER>=1500
142 & tTimeNow147 & tTimeNow
143#else148#else
@@ -146,16 +151,20 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 2 unmodified lines
146 );151 );
147#endif//_MSC_VER152#endif//_MSC_VER
148#endif//__GNUC__153#endif//__GNUC__
149 iBufferLeft -= iBufferUsed;154 iBufferUsed += iCurUsed;
155 iBufferLeft -= iCurUsed;
150 }156 }
151 157
152 iBufferLeftSize -= iBufferUsedSize;158 if (iCurUsedSize > 0) {
159 iBufferUsedSize += iCurUsedSize;
160 iBufferLeftSize -= iCurUsedSize;
161 }
153 if (iBufferLeftSize > 0) {162 if (iBufferLeftSize > 0) {
154#if defined(_GNUC__)163#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);
156#else165#else
157#if defined(_MSC_VER)166#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",
159#if _MSC_VER>=1500168#if _MSC_VER>=1500
160 & tTimeNow169 & tTimeNow
161#else170#else
@@ -164,38 +173,45 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 2 unmodified lines
164 );173 );
165#endif//_MSC_VER174#endif//_MSC_VER
166#endif//__GNUC__175#endif//__GNUC__
167 iBufferLeftSize -= iBufferUsedSize;176 iBufferUsedSize += iCurUsedSize;
177 iBufferLeftSize -= iCurUsedSize;
168 }178 }
169 179
170 if (iBufferLeft > 0) {180 if (iBufferLeft > 0) {
171#ifdef _WIN32181#ifdef _WIN32
172#if defined(_MSC_VER)182#if defined(_MSC_VER)
173#if _MSC_VER>=1500183#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);
175#else185#else
176 iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm);186 iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm);
177#endif//_MSC_VER>=1500187#endif//_MSC_VER>=1500
178#endif//_MSC_VER188#endif//_MSC_VER
179#else189#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);
181#endif//WIN32191#endif//WIN32
182 iBufferLeft -= iBufferUsed;192 if (iCurUsed > 0) {
193 iBufferUsed += iCurUsed;
194 iBufferLeft -= iCurUsed;
195 }
183 }196 }
184 197
185 if (iBufferLeftSize > 0) {198 if (iBufferLeftSize > 0) {
186#ifdef _WIN32199#ifdef _WIN32
187#if defined(_MSC_VER)200#if defined(_MSC_VER)
188#if _MSC_VER>=1500201#if _MSC_VER>=1500
189 iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, iBufferLeftSize, ".%03.3u.len",202 iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, iBufferLeftSize, ".%03.3u.len",
190 tTimeb.millitm);203 tTimeb.millitm);
191#else204#else
192 iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm);205 iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm);
193#endif//_MSC_VER>=1500206#endif//_MSC_VER>=1500
194#endif//_MSC_VER207#endif//_MSC_VER
195#else208#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);
197#endif//WIN32210#endif//WIN32
198 iBufferLeftSize -= iBufferUsedSize;211 if (iCurUsedSize > 0) {
212 iBufferUsedSize += iCurUsedSize;
213 iBufferLeftSize -= iCurUsedSize;
214 }
199 }215 }
200 216
201#if defined(__GNUC__)217#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