]> granicus.if.org Git - libvpx/commitdiff
Avoid overflow in calc_iframe_target_size
authorJorge E. Moreira <jemoreira@google.com>
Wed, 30 Jun 2021 18:33:51 +0000 (11:33 -0700)
committerJorge E. Moreira <jemoreira@google.com>
Fri, 2 Jul 2021 03:32:22 +0000 (20:32 -0700)
The changed product was observed to attempt to multiply 1800 by 2500000,
which overflows unsigned 32 bits. Converting to unsigned 64 bits first
and testing whether the final result fits in 32 bits solves the problem.

BUG=b:179686142

Change-Id: I5d27317bf14b0311b739144c451d8e172db01945

vp8/encoder/ratectrl.c

index ba124c359e55808eecb743cea28cc4a4f1bef67c..d2b8dff06a51de86e72abab0d8be281cbbee2da5 100644 (file)
@@ -349,8 +349,12 @@ static void calc_iframe_target_size(VP8_COMP *cpi) {
   }
 
   if (cpi->oxcf.rc_max_intra_bitrate_pct) {
-    unsigned int max_rate =
-        cpi->per_frame_bandwidth * cpi->oxcf.rc_max_intra_bitrate_pct / 100;
+    unsigned int max_rate;
+    // This product may overflow unsigned int
+    uint64_t product = cpi->per_frame_bandwidth;
+    product *= cpi->oxcf.rc_max_intra_bitrate_pct;
+    product /= 100;
+    max_rate = (unsigned int)VPXMIN(INT_MAX, product);
 
     if (target > max_rate) target = max_rate;
   }