Skip to content

Commit 6a9eb44

Browse files
committed
Fix potential overflow in FramesAreSimilar
Bug: 496807858 Change-Id: Idc9af6f86a171322dd09e197dafbef59d5e4aa53
1 parent 7ab12ce commit 6a9eb44

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

examples/anim_diff.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ static int FramesAreSimilar(const uint8_t* const rgba1,
7070
for (j = 0; j < height; ++j) {
7171
for (i = 0; i < width; ++i) {
7272
const int stride = width * 4;
73-
const size_t offset = j * stride + i;
74-
if (!PixelsAreSimilar(rgba1[offset], rgba2[offset], max_allowed_diff)) {
73+
size_t offset_row, offset;
74+
if (!CheckMultiplicationOverflow(j, stride, &offset_row) ||
75+
!CheckAdditionOverflow(offset_row, i, &offset) ||
76+
!PixelsAreSimilar(rgba1[offset], rgba2[offset], max_allowed_diff)) {
7577
return 0;
7678
}
7779
}

examples/anim_util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ int CheckMultiplicationOverflow(uint32_t val1, uint32_t val2, size_t* product) {
102102
return 0;
103103
}
104104

105-
#if defined(WEBP_HAVE_GIF)
106-
107105
WEBP_NODISCARD
108-
static int CheckAdditionOverflow(size_t val1, uint32_t val2, size_t* addition) {
106+
int CheckAdditionOverflow(size_t val1, uint32_t val2, size_t* addition) {
109107
const uint64_t size = (uint64_t)val1 + val2;
110108
if (CheckSizeForOverflow(size)) {
111109
*addition = (size_t)size;
@@ -114,6 +112,8 @@ static int CheckAdditionOverflow(size_t val1, uint32_t val2, size_t* addition) {
114112
return 0;
115113
}
116114

115+
#if defined(WEBP_HAVE_GIF)
116+
117117
// For the GIF functions below, the width, height, x_offset, y_offset fit on 16
118118
// bits (but can fill the 16 bits) as per the GIF specification.
119119
// Multiplications that can overflow are cast to 64 bits.

examples/anim_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ void GetAnimatedImageVersions(int* const decoder_version,
7373
// Check whether val1 * val2 fits in a size_t. Returns 1 on success.
7474
int CheckMultiplicationOverflow(uint32_t val1, uint32_t val2, size_t* product);
7575

76+
// Check whether val1 + val2 fits in a size_t. Returns 1 on success.
77+
int CheckAdditionOverflow(size_t val1, uint32_t val2, size_t* addition);
78+
7679
#ifdef __cplusplus
7780
} // extern "C"
7881
#endif

0 commit comments

Comments
 (0)