Merge commit '15f1cc09a406cf6296818d475a256902235eefc4'
authorJames Almer <jamrial@gmail.com>
Tue, 7 Nov 2017 21:55:18 +0000 (18:55 -0300)
committerJames Almer <jamrial@gmail.com>
Tue, 7 Nov 2017 21:59:43 +0000 (18:59 -0300)
* commit '15f1cc09a406cf6296818d475a256902235eefc4':
  flac: Postpone unlikely condition checks

Merged-by: James Almer <jamrial@gmail.com>
1  2 
libavcodec/flacdec.c

@@@ -224,49 -203,37 +224,50 @@@ static int decode_residuals(FLACContex
      int rice_bits, rice_esc;
      int samples;
  
 -    method_type = bitstream_read(&s->bc, 2);
 -    rice_order  = bitstream_read(&s->bc, 4);
 +    method_type = get_bits(&s->gb, 2);
++    rice_order  = get_bits(&s->gb, 4);
+     samples   = s->blocksize >> rice_order;
+     rice_bits = 4 + method_type;
+     rice_esc  = (1 << rice_bits) - 1;
+     decoded += pred_order;
+     i        = pred_order;
      if (method_type > 1) {
          av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
                 method_type);
          return AVERROR_INVALIDDATA;
      }
  
-     rice_order = get_bits(&s->gb, 4);
-     samples= s->blocksize >> rice_order;
 +    if (samples << rice_order != s->blocksize) {
 +        av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
 +               rice_order, s->blocksize);
 +        return AVERROR_INVALIDDATA;
 +    }
 +
      if (pred_order > samples) {
          av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
                 pred_order, samples);
          return AVERROR_INVALIDDATA;
      }
  
-     rice_bits = 4 + method_type;
-     rice_esc  = (1 << rice_bits) - 1;
-     decoded += pred_order;
-     i= pred_order;
      for (partition = 0; partition < (1 << rice_order); partition++) {
 -        tmp = bitstream_read(&s->bc, rice_bits);
 +        tmp = get_bits(&s->gb, rice_bits);
          if (tmp == rice_esc) {
 -            tmp = bitstream_read(&s->bc, 5);
 +            tmp = get_bits(&s->gb, 5);
              for (; i < samples; i++)
 -                *decoded++ = bitstream_read_signed(&s->bc, tmp);
 +                *decoded++ = get_sbits_long(&s->gb, tmp);
          } else {
 +            int real_limit = tmp ? (INT_MAX >> tmp) + 2 : INT_MAX;
              for (; i < samples; i++) {
 -                *decoded++ = get_sr_golomb_flac(&s->bc, tmp, INT_MAX, 0);
 +                int v = get_sr_golomb_flac(&s->gb, tmp, real_limit, 0);
 +                if (v == 0x80000000){
 +                    av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
 +                    return AVERROR_INVALIDDATA;
 +                }
 +
 +                *decoded++ = v;
              }
          }
          i= 0;