Skip to content

Commit ec27ab3

Browse files
authored
Merge pull request #57 from r-ralph/fix-integer-overflow
Fix integer overflow issue
2 parents 0e26121 + 7546d53 commit ec27ab3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

apng-drawable/src/main/cpp/apng-drawbale/ApngDecoder.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,25 @@ std::unique_ptr<ApngImage> ApngDecoder::decode(
189189

190190
// Allocate buffers
191191
LOGV(" | allocate buffers");
192+
// Check unsigned integer wrapping
193+
if (height > SIZE_MAX / row_bytes) {
194+
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
195+
result = ERR_INVALID_FILE_FORMAT;
196+
return nullptr;
197+
}
192198
size_t size = height * row_bytes;
193199
std::unique_ptr<uint8_t[]> p_frame(new uint8_t[size]());
194200
std::unique_ptr<uint8_t[]> p_buffer(new uint8_t[size]());
195201
std::unique_ptr<uint8_t[]> p_previous_frame(new uint8_t[size]());
196-
std::unique_ptr<png_bytep[]> rows_frame(new png_bytep[height * sizeof(png_bytep)]);
197-
std::unique_ptr<png_bytep[]> rows_buffer(new png_bytep[height * sizeof(png_bytep)]);
202+
// Check unsigned integer wrapping
203+
if (height > SIZE_MAX / sizeof(png_bytep)) {
204+
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
205+
result = ERR_INVALID_FILE_FORMAT;
206+
return nullptr;
207+
}
208+
size_t row_ptr_array_size = height * sizeof(png_bytep);
209+
std::unique_ptr<png_bytep[]> rows_frame(new png_bytep[row_ptr_array_size]);
210+
std::unique_ptr<png_bytep[]> rows_buffer(new png_bytep[row_ptr_array_size]);
198211
if (!p_frame || !p_buffer || !p_previous_frame || !rows_frame || !rows_buffer) {
199212
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
200213
result = ERR_OUT_OF_MEMORY;

0 commit comments

Comments
 (0)