diff options
author | Michael Niedermayer <michael@niedermayer.cc> | 2021-12-04 22:33:30 +0300 |
---|---|---|
committer | Michael Niedermayer <michael@niedermayer.cc> | 2021-12-06 16:25:55 +0300 |
commit | 235ac7b4925ab963afd7719577a8fd6ce25b4d5f (patch) | |
tree | fd62a7e128cd01c5aaa6ac83d69d76fa4ef87a2f | |
parent | 59b4e7cbd87889c0bac710ac7f62782b637419a1 (diff) |
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>
-rw-r--r-- | libavcodec/avpacket.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index d8d8fef3b9..a0134e405c 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -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; |