diff mbox series

[FFmpeg-devel,v2,1/2] avcodec/cbs_av1: Fix writing uvlc numbers >= INT_MAX

Message ID 20200618191909.2253-1-andreas.rheinhardt@gmail.com
State Accepted
Commit 6f06c17a55137855c67ba4a7b6778ca34ddbbe6b
Headers show
Series [FFmpeg-devel,v2,1/2] avcodec/cbs_av1: Fix writing uvlc numbers >= INT_MAX | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt June 18, 2020, 7:19 p.m. UTC
Fixes: assertion failure
Fixes: left shift of 1 by 31 places cannot be represented in type 'int'
Fixes: 23264/clusterfuzz-testcase-minimized-ffmpeg_BSF_AV1_METADATA_fuzzer-6308429248593920

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
The earlier version did fix the undefined behaviour when shifting, but
one nevertheless ran into an av_assert2 in put_bits because put_bits
can't write 32 bits at once*. So write via three calls to put_bits.

*: If one wants to write 32 bits at once and if there are 32 bits left
(i.e. the cache is empty), then bit_buf will be shifted by 32 places
which is undefined behaviour (even though bit_buf is zero in this case).

 libavcodec/cbs_av1.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c
index 29b316e011..0abcba9c60 100644
--- a/libavcodec/cbs_av1.c
+++ b/libavcodec/cbs_av1.c
@@ -125,8 +125,9 @@  static int cbs_av1_write_uvlc(CodedBitstreamContext *ctx, PutBitContext *pbc,
         put_bits(pbc, 1, 1);
     } else {
         zeroes = av_log2(value + 1);
-        v = value - (1 << zeroes) + 1;
-        put_bits(pbc, zeroes + 1, 1);
+        v = value - (1U << zeroes) + 1;
+        put_bits(pbc, zeroes, 0);
+        put_bits(pbc, 1, 1);
         put_bits(pbc, zeroes, v);
     }