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.
Showing the changes to codec/encoder/plus/src/welsEncoderExt.cpp (1 of 2 files). Show all
Unified · Split
| @@ -77,10 +77,12 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 76 unmodified lines | ||
| 77 | 77 | str_t strStreamFileName[1024] = { 0 }; //for .264 |
| 78 | 78 | int32_t iBufferUsed = 0; |
| 79 | 79 | int32_t iBufferLeft = 1023; |
| 80 | + int32_t iCurUsed; | |
| 80 | 81 | |
| 81 | 82 | str_t strLenFileName[1024] = { 0 }; //for .len |
| 82 | 83 | int32_t iBufferUsedSize = 0; |
| 83 | 84 | int32_t iBufferLeftSize = 1023; |
| 85 | + int32_t iCurUsedSize; | |
| 84 | 86 | #endif//OUTPUT_BIT_STREAM |
| 85 | 87 | |
| 86 | 88 | #ifdef OUTPUT_BIT_STREAM |
| @@ -118,26 +120,29 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 31 unmodified lines | ||
| 118 | 120 | #ifdef _WIN32 |
| 119 | 121 | #if defined(_MSC_VER) |
| 120 | 122 | #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); | |
| 123 | 125 | #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); | |
| 126 | 128 | #endif//_MSC_VER>=1500 |
| 127 | 129 | #endif//_MSC_VER |
| 128 | 130 | #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); | |
| 131 | 133 | #endif//WIN32 |
| 132 | 134 | |
| 133 | 135 | |
| 134 | − iBufferLeft -= iBufferUsed; | |
| 136 | + if (iCurUsed > 0) { | |
| 137 | + iBufferUsed += iCurUsed; | |
| 138 | + iBufferLeft -= iCurUsed; | |
| 139 | + } | |
| 135 | 140 | if (iBufferLeft > 0) { |
| 136 | 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 | 143 | #else |
| 139 | 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 | 146 | #if _MSC_VER>=1500 |
| 142 | 147 | & tTimeNow |
| 143 | 148 | #else |
| @@ -146,16 +151,20 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 2 unmodified lines | ||
| 146 | 151 | ); |
| 147 | 152 | #endif//_MSC_VER |
| 148 | 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 | 162 | if (iBufferLeftSize > 0) { |
| 154 | 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 | 165 | #else |
| 157 | 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 | 168 | #if _MSC_VER>=1500 |
| 160 | 169 | & tTimeNow |
| 161 | 170 | #else |
| @@ -164,38 +173,45 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder() · 2 unmodified lines | ||
| 164 | 173 | ); |
| 165 | 174 | #endif//_MSC_VER |
| 166 | 175 | #endif//__GNUC__ |
| 167 | − iBufferLeftSize -= iBufferUsedSize; | |
| 176 | + iBufferUsedSize += iCurUsedSize; | |
| 177 | + iBufferLeftSize -= iCurUsedSize; | |
| 168 | 178 | } |
| 169 | 179 | |
| 170 | 180 | if (iBufferLeft > 0) { |
| 171 | 181 | #ifdef _WIN32 |
| 172 | 182 | #if defined(_MSC_VER) |
| 173 | 183 | #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 | 185 | #else |
| 176 | − iBufferUsed += SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm); | |
| 186 | + iCurUsed = SNPRINTF (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", tTimeb.millitm); | |
| 177 | 187 | #endif//_MSC_VER>=1500 |
| 178 | 188 | #endif//_MSC_VER |
| 179 | 189 | #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 | 191 | #endif//WIN32 |
| 182 | − iBufferLeft -= iBufferUsed; | |
| 192 | + if (iCurUsed > 0) { | |
| 193 | + iBufferUsed += iCurUsed; | |
| 194 | + iBufferLeft -= iCurUsed; | |
| 195 | + } | |
| 183 | 196 | } |
| 184 | 197 | |
| 185 | 198 | if (iBufferLeftSize > 0) { |
| 186 | 199 | #ifdef _WIN32 |
| 187 | 200 | #if defined(_MSC_VER) |
| 188 | 201 | #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 | 203 | tTimeb.millitm); |
| 191 | 204 | #else |
| 192 | − iBufferUsedSize += SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm); | |
| 205 | + iCurUsedSize = SNPRINTF (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", tTimeb.millitm); | |
| 193 | 206 | #endif//_MSC_VER>=1500 |
| 194 | 207 | #endif//_MSC_VER |
| 195 | 208 | #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 | 210 | #endif//WIN32 |
| 198 | − iBufferLeftSize -= iBufferUsedSize; | |
| 211 | + if (iCurUsedSize > 0) { | |
| 212 | + iBufferUsedSize += iCurUsedSize; | |
| 213 | + iBufferLeftSize -= iCurUsedSize; | |
| 214 | + } | |
| 199 | 215 | } |
| 200 | 216 | |
| 201 | 217 | #if defined(__GNUC__) |