Skip to content

Commit 29dd9bb

Browse files
committed
More code hardening
1 parent 2018516 commit 29dd9bb

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/bitstream/DefaultInputBitStream.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ uint DefaultInputBitStream::readBits(byte bits[], uint count)
7979
remaining -= (availBytes << 3);
8080
_position = _maxPosition + 1;
8181

82-
if (readFromInputStream(_bufferSize) < int(_bufferSize))
83-
break;
84-
82+
const int read = readFromInputStream(_bufferSize);
8583
availBytes = uint(_maxPosition + 1 - _position);
84+
85+
if (read < int(_bufferSize))
86+
break;
8687
}
8788

88-
const uint r = (remaining >> 6) << 3;
89+
const uint r = min((remaining >> 6) << 3, availBytes);
8990

9091
if (r > 0) {
9192
memcpy(&bits[start], &_buffer[_position], r);

src/bitstream/DefaultOutputBitStream.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ namespace kanzi
153153
// Round down to byte alignment
154154
const uint a = _availBits - (_availBits & 7);
155155

156-
for (uint i = 56; i >= a; i -= 8) {
157-
_buffer[_position++] = byte(_current >> i);
156+
for (int i = 56; i >= int(a); i -= 8) {
157+
_buffer[_position++] = byte(_current >> uint(i));
158158

159159
if (_position >= _bufferSize)
160160
flush();
@@ -170,4 +170,3 @@ namespace kanzi
170170

171171
}
172172
#endif
173-

src/entropy/ANSRangeDecoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ bool ANSRangeDecoder::decodeChunk(byte block[], uint count)
220220
// Read chunk size
221221
const uint sz = uint(EntropyUtils::readVarInt(_bitstream));
222222

223-
if (sz >= MAX_CHUNK_SIZE)
223+
if ((sz >= MAX_CHUNK_SIZE) || (sz > _bufferSize - 2))
224224
return false;
225225

226226
// Read initial ANS states

src/entropy/HuffmanDecoder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ bool HuffmanDecoder::decodeChunk(byte block[], uint count)
212212
if ((szBits0 < 0) || (szBits1 < 0) || (szBits2 < 0) || (szBits3 < 0))
213213
return false;
214214

215+
// Each of the 4 streams is stored in one quarter of _buffer.
216+
const int maxFragBits = int((_bufferSize >> 2) << 3);
217+
218+
if ((szBits0 > maxFragBits) || (szBits1 > maxFragBits) || (szBits2 > maxFragBits) || (szBits3 > maxFragBits))
219+
return false;
220+
215221
memset(_buffer, 0, _bufferSize);
216222

217223
int idx0 = 0 * (_bufferSize / 4);

0 commit comments

Comments
 (0)