Skip to content

Commit afb801b

Browse files
Piotr Pawłowskiktmf01
authored andcommitted
bitwriter_grow_() correctly test for out-of-legit-range values, allow maximum possible padding of 0xFFFFFF bytes to be written
1 parent 9547dbc commit afb801b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/libFLAC/bitwriter.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
112112
uint32_t new_capacity;
113113
bwword *new_buffer;
114114

115+
/* Refuse to allocate more words than this amount, based on largest possible metadata chunk size */
116+
const uint32_t max_capacity = (((1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1) * 8 + FLAC__STREAM_METADATA_LENGTH_LEN + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD;
117+
115118
FLAC__ASSERT(0 != bw);
116119
FLAC__ASSERT(0 != bw->buffer);
117120

@@ -124,7 +127,7 @@ FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
124127
if(bw->capacity >= new_capacity)
125128
return true;
126129

127-
if(new_capacity * sizeof(bwword) > (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
130+
if(new_capacity > max_capacity)
128131
/* Requested new capacity is larger than the largest possible metadata block,
129132
* which is also larger than the largest sane framesize. That means something
130133
* went very wrong somewhere and previous checks failed.

0 commit comments

Comments
 (0)