libFLAC/stream_decoder.c: Fix undefined behaviour
authorErik de Castro Lopo <erikd@mega-nerd.com>
Mon, 24 Aug 2015 09:20:35 +0000 (19:20 +1000)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Mon, 24 Aug 2015 09:21:51 +0000 (19:21 +1000)
Found by compiling with -fsanitize=undefined and running the testsuite.

src/libFLAC/stream_decoder.c

index 519b0c3..77036ba 100644 (file)
@@ -2115,7 +2115,7 @@ FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FL
 #if 1
                                                mid = decoder->private_->output[0][i];
                                                side = decoder->private_->output[1][i];
-                                               mid <<= 1;
+                                               mid = ((uint32_t) mid) << 1;
                                                mid |= (side & 1); /* i.e. if 'side' is odd... */
                                                decoder->private_->output[0][i] = (mid + side) >> 1;
                                                decoder->private_->output[1][i] = (mid - side) >> 1;
@@ -2541,8 +2541,10 @@ FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsign
 
        if(wasted_bits && do_full_decode) {
                x = decoder->private_->frame.subframes[channel].wasted_bits;
-               for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
-                       decoder->private_->output[channel][i] <<= x;
+               for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
+                       uint32_t val = decoder->private_->output[channel][i];
+                       decoder->private_->output[channel][i] = (val << x);
+               }
        }
 
        return true;