avcodec/avpacket: Perform fewer reallocations in repeated av_grow_packet()

Fixes: Timeout
Fixes: 41446/clusterfuzz-testcase-minimized-ffmpeg_dem_SAMI_fuzzer-4667644540747776

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer 2021-12-04 20:33:30 +01:00
parent 59b4e7cbd8
commit 235ac7b492
1 changed files with 8 additions and 1 deletions

View File

@ -142,7 +142,14 @@ int av_grow_packet(AVPacket *pkt, int grow_by)
if (new_size + data_offset > pkt->buf->size ||
!av_buffer_is_writable(pkt->buf)) {
int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
int ret;
// allocate slightly more than requested to avoid excessive
// reallocations
if (new_size + data_offset < INT_MAX - new_size/16)
new_size += new_size/16;
ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
if (ret < 0) {
pkt->data = old_data;
return ret;