stream_decoder: fix integer underflow due to malformed wasted_bits
authorMax Kellermann <max@duempel.org>
Fri, 8 Jul 2016 19:29:41 +0000 (21:29 +0200)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sat, 9 Jul 2016 22:19:26 +0000 (08:19 +1000)
It is pretty easy for a malformed FLAC file to underflow the "bps"
variable.  In the debug build, this results in an assertion failure in
FLAC__bitreader_read_raw_uint32():

    FLAC__ASSERT(bits <= 32);

In non-debug builds, this simply makes
FLAC__bitreader_read_raw_uint32() fail because
bitreader_read_from_client_() doesn't find enough buffer space for
2**32-1 bits.  But since the failing FLAC_ASSERT() is reasonable, this
should be caught in the FLAC__bitreader_read_raw_uint32() caller.

Signed-off-by: Erik de Castro Lopo <erikd@mega-nerd.com>
Closes: https://github.com/xiph/flac/pull/13

src/libFLAC/stream_decoder.c

index fa0ef2c..c3a903f 100644 (file)
@@ -2481,6 +2481,8 @@ FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsign
                if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
                        return false; /* read_callback_ sets the state for us */
                decoder->private_->frame.subframes[channel].wasted_bits = u+1;
+               if (decoder->private_->frame.subframes[channel].wasted_bits >= bps)
+                       return false;
                bps -= decoder->private_->frame.subframes[channel].wasted_bits;
        }
        else