Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/libImaging/QuantPngQuant.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
*/

#include <stdint.h>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <stdint.h>

This should already be included by

#ifdef _MSC_VER
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -39,6 +40,13 @@ quantize_pngquant(
*paletteLength = 0;
*quantizedPixels = NULL;

/* Check for integer overflow in width * height to prevent
* undersized allocations leading to heap buffer overflow. */
if (height != 0 && (size_t)width > SIZE_MAX / (size_t)height) {
goto err;
}
size_t total_pixels = (size_t)width * (size_t)height;

/* configure pngquant */
attr = liq_attr_create();
if (!attr) {
Expand Down Expand Up @@ -77,7 +85,7 @@ quantize_pngquant(
}

/* write output pixels (pngquant uses char array) */
charMatrix = malloc(width * height);
charMatrix = malloc(total_pixels);
if (!charMatrix) {
goto err;
}
Expand All @@ -86,18 +94,18 @@ quantize_pngquant(
goto err;
}
for (y = 0; y < height; y++) {
charMatrixRows[y] = &charMatrix[y * width];
charMatrixRows[y] = &charMatrix[(size_t)y * width];
}
if (LIQ_OK != liq_write_remapped_image_rows(remap, image, charMatrixRows)) {
goto err;
}

/* transcribe output pixels (pillow uses uint32_t array) */
*quantizedPixels = malloc(sizeof(uint32_t) * width * height);
*quantizedPixels = malloc(sizeof(uint32_t) * total_pixels);
if (!*quantizedPixels) {
goto err;
}
for (i = 0; i < width * height; i++) {
for (i = 0; i < total_pixels; i++) {
(*quantizedPixels)[i] = charMatrix[i];
}

Expand Down
Loading