1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

lavc/bitstream: avoid UB in bits_{read,peek}_signed(0)

bits_*_signed(0) will currently invoke an undefined shift by
8 * sizeof(int).

Add bits_*_signed_nz() that only works for n>0, analogous to
bits_read_nz(). Add an explicit check for n=0 in bits_*_signed().

Found-by: James Almer
This commit is contained in:
Anton Khirnov 2023-01-15 10:21:36 +01:00
parent bf23d530d2
commit 0b33310cd0
3 changed files with 39 additions and 3 deletions

View File

@ -89,9 +89,11 @@
# define bits_read_63 bits_read_63_le
# define bits_read_64 bits_read_64_le
# define bits_read_signed bits_read_signed_le
# define bits_read_signed_nz bits_read_signed_nz_le
# define bits_peek_nz bits_peek_nz_le
# define bits_peek bits_peek_le
# define bits_peek_signed bits_peek_signed_le
# define bits_peek_signed_nz bits_peek_signed_nz_le
# define bits_skip bits_skip_le
# define bits_seek bits_seek_le
# define bits_align bits_align_le
@ -115,9 +117,11 @@
# define bits_read_63 bits_read_63_be
# define bits_read_64 bits_read_64_be
# define bits_read_signed bits_read_signed_be
# define bits_read_signed_nz bits_read_signed_nz_be
# define bits_peek_nz bits_peek_nz_be
# define bits_peek bits_peek_be
# define bits_peek_signed bits_peek_signed_be
# define bits_peek_signed_nz bits_peek_signed_nz_be
# define bits_skip bits_skip_be
# define bits_seek bits_seek_be
# define bits_align bits_align_be

View File

@ -290,13 +290,29 @@ static inline uint64_t BS_FUNC(read_64)(BSCTX *bc, unsigned int n)
return BS_FUNC(read_63)(bc, n);
}
/**
* Return n bits from the buffer as a signed integer, n has to be in the 1-32
* range. May be faster than bits_read_signed() when n is not a compile-time
* constant and is known to be non-zero;
*/
static inline int32_t BS_FUNC(read_signed_nz)(BSCTX *bc, unsigned int n)
{
av_assert2(n > 0 && n <= 32);
return sign_extend(BS_FUNC(read_nz)(bc, n), n);
}
/**
* Return n bits from the buffer as a signed integer.
* n has to be in the 0-32 range.
*/
static inline int32_t BS_FUNC(read_signed)(BSCTX *bc, unsigned int n)
{
return sign_extend(BS_FUNC(read)(bc, n), n);
av_assert2(n <= 32);
if (!n)
return 0;
return BS_FUNC(read_signed_nz)(bc, n);
}
/**
@ -327,6 +343,17 @@ static inline uint32_t BS_FUNC(peek)(BSCTX *bc, unsigned int n)
return BS_FUNC(peek_nz)(bc, n);
}
/**
* Return n bits from the buffer as a signed integer, do not change the buffer
* state. n has to be in the 1-32 range. May be faster than bits_peek_signed()
* when n is not a compile-time constant and is known to be non-zero;
*/
static inline int BS_FUNC(peek_signed_nz)(BSCTX *bc, unsigned int n)
{
av_assert2(n > 0 && n <= 32);
return sign_extend(BS_FUNC(peek_nz)(bc, n), n);
}
/**
* Return n bits from the buffer as a signed integer,
* do not change the buffer state.
@ -334,7 +361,12 @@ static inline uint32_t BS_FUNC(peek)(BSCTX *bc, unsigned int n)
*/
static inline int BS_FUNC(peek_signed)(BSCTX *bc, unsigned int n)
{
return sign_extend(BS_FUNC(peek)(bc, n), n);
av_assert2(n <= 32);
if (!n)
return 0;
return BS_FUNC(peek_signed_nz)(bc, n);
}
/**

View File

@ -85,7 +85,7 @@ typedef BitstreamContext GetBitContext;
#define get_bits1 bits_read_bit
#define get_bits64 bits_read_64
#define get_xbits bits_read_xbits
#define get_sbits bits_read_signed
#define get_sbits bits_read_signed_nz
#define get_sbits_long bits_read_signed
#define show_bits bits_peek
#define show_bits_long bits_peek