summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjzern@google.com <jzern@google.com>2021-06-09 23:49:48 +0000
committerJames Zern <jzern@google.com>2021-06-10 15:26:44 -0700
commit685d073ee11bb1f6c75eecbeb01f76a0b7ba9afd (patch)
tree87d23a6f9fed21eafafbb9349aed03076d906b84
parentd38bd0dda0cca47777700771b275574cc23598ca (diff)
downloadlibwebp-685d073ee11bb1f6c75eecbeb01f76a0b7ba9afd.tar.gz
Init{RGB,YUV}Rescaler: fix int overflows in multiplication
with large sizes the intermediate calculations may exceed 32-bits src/dec/io_dec.c:491:17: runtime error: signed integer overflow: 3 * 788529152 cannot be represented in type 'int' #0 0x557a3ad972b2 in InitRGBRescaler src/dec/io_dec.c:491:17 #1 0x557a3ad972b2 in CustomSetup src/dec/io_dec.c:563:29 Bug: chromium:1196850 Change-Id: Iaf2e8a6de9481dfea31dcd7fccb2d4eca767bddf
-rw-r--r--src/dec/io_dec.c36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/dec/io_dec.c b/src/dec/io_dec.c
index 29dc6345..384cbced 100644
--- a/src/dec/io_dec.c
+++ b/src/dec/io_dec.c
@@ -300,24 +300,30 @@ static int InitYUVRescaler(const VP8Io* const io, WebPDecParams* const p) {
const int uv_in_height = (io->mb_h + 1) >> 1;
const size_t work_size = 2 * out_width; // scratch memory for luma rescaler
const size_t uv_work_size = 2 * uv_out_width; // and for each u/v ones
- size_t tmp_size, rescaler_size;
+ uint64_t total_size;
+ size_t rescaler_size;
rescaler_t* work;
WebPRescaler* scalers;
const int num_rescalers = has_alpha ? 4 : 3;
- tmp_size = (work_size + 2 * uv_work_size) * sizeof(*work);
+ total_size = ((uint64_t)work_size + 2 * uv_work_size) * sizeof(*work);
if (has_alpha) {
- tmp_size += work_size * sizeof(*work);
+ total_size += (uint64_t)work_size * sizeof(*work);
}
rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST;
+ total_size += rescaler_size;
+ if (total_size != (size_t)total_size) {
+ return 0;
+ }
- p->memory = WebPSafeMalloc(1ULL, tmp_size + rescaler_size);
+ p->memory = WebPSafeMalloc(1ULL, (size_t)total_size);
if (p->memory == NULL) {
return 0; // memory error
}
work = (rescaler_t*)p->memory;
- scalers = (WebPRescaler*)WEBP_ALIGN((const uint8_t*)work + tmp_size);
+ scalers = (WebPRescaler*)WEBP_ALIGN(
+ (const uint8_t*)work + total_size - rescaler_size);
p->scaler_y = &scalers[0];
p->scaler_u = &scalers[1];
p->scaler_v = &scalers[2];
@@ -483,27 +489,29 @@ static int InitRGBRescaler(const VP8Io* const io, WebPDecParams* const p) {
const size_t work_size = 2 * out_width; // scratch memory for one rescaler
rescaler_t* work; // rescalers work area
uint8_t* tmp; // tmp storage for scaled YUV444 samples before RGB conversion
- size_t tmp_size1, tmp_size2, total_size, rescaler_size;
+ uint64_t tmp_size1, tmp_size2, total_size;
+ size_t rescaler_size;
WebPRescaler* scalers;
const int num_rescalers = has_alpha ? 4 : 3;
- tmp_size1 = 3 * work_size;
- tmp_size2 = 3 * out_width;
- if (has_alpha) {
- tmp_size1 += work_size;
- tmp_size2 += out_width;
- }
+ tmp_size1 = (uint64_t)num_rescalers * work_size;
+ tmp_size2 = (uint64_t)num_rescalers * out_width;
total_size = tmp_size1 * sizeof(*work) + tmp_size2 * sizeof(*tmp);
rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST;
+ total_size += rescaler_size;
+ if (total_size != (size_t)total_size) {
+ return 0;
+ }
- p->memory = WebPSafeMalloc(1ULL, total_size + rescaler_size);
+ p->memory = WebPSafeMalloc(1ULL, (size_t)total_size);
if (p->memory == NULL) {
return 0; // memory error
}
work = (rescaler_t*)p->memory;
tmp = (uint8_t*)(work + tmp_size1);
- scalers = (WebPRescaler*)WEBP_ALIGN((const uint8_t*)work + total_size);
+ scalers = (WebPRescaler*)WEBP_ALIGN(
+ (const uint8_t*)work + total_size - rescaler_size);
p->scaler_y = &scalers[0];
p->scaler_u = &scalers[1];
p->scaler_v = &scalers[2];