@@ -189,12 +189,25 @@ std::unique_ptr<ApngImage> ApngDecoder::decode(
189
189
190
190
// Allocate buffers
191
191
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
+ }
192
198
size_t size = height * row_bytes;
193
199
std::unique_ptr<uint8_t []> p_frame (new uint8_t [size]());
194
200
std::unique_ptr<uint8_t []> p_buffer (new uint8_t [size]());
195
201
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]);
198
211
if (!p_frame || !p_buffer || !p_previous_frame || !rows_frame || !rows_buffer) {
199
212
png_destroy_read_struct (&png_ptr, &info_ptr, nullptr );
200
213
result = ERR_OUT_OF_MEMORY;
0 commit comments