[FFmpeg-devel] [PATCH] add put_bits_long to avoid undefined behaviour in put_bits
Michael Niedermayer
michaelni
Sun Aug 23 12:33:24 CEST 2009
On Sat, Aug 22, 2009 at 10:22:19PM +0200, matthieu castet wrote:
> Hi,
>
> the put_bits can be called with
> unsigned int bit_buf = 0;
> int bit_left = 32;
> int n = 32;
> unsigned int value = 0x1ba;
>
> in the case of ALT_BITSTREAM_WRITER and BITSTREAM_WRITER_LE is not defined,
> we
> will do "bit_buf<<=bit_left;"
> But sizeof(bit_buf) == 32 and bit_left==32, and we are in a undefined
> behavior[1].
>
> This patch add a put_bits_long for n = 32 and restrict n <= 31 for
> put_bits.
[..]
> @@ -259,6 +259,22 @@
> put_bits(pb, bits, val & ((1<<bits)-1));
> }
>
> +static inline void put_bits_long(PutBitContext *s, int n, unsigned int value)
> +{
> + if(n<=31)
> + put_bits(s, n, value);
> + else{
should have {} for simplicity of future patches
> +#ifdef ALT_BITSTREAM_READER_LE
> + put_bits(s, 16, value & 0xffff);
> + put_bits(s, 16, value >> 16);
> +#else
> + put_bits(s, 16, value >> 16);
> + put_bits(s, 16, value & 0xffff);
> +#endif
> + }
> +
> +}
> +
> /**
> * Returns the pointer to the byte where the bitstream writer will put
> * the next bit.
tabs are forbidden in svn
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Republics decline into democracies and democracies degenerate into
despotisms. -- Aristotle
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090823/34df5cf3/attachment.pgp>
More information about the ffmpeg-devel
mailing list