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
25 changes: 20 additions & 5 deletions src/libImaging/QuantPngQuant.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
*/

#include <stdint.h>
#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 Expand Up @@ -126,7 +134,14 @@ const char *
ImagingImageQuantVersion(void) {
static char version[20];
int number = liq_version();
sprintf(version, "%d.%d.%d", number / 10000, (number / 100) % 100, number % 100);
snprintf(
version,
sizeof(version),
"%d.%d.%d",
number / 10000,
(number / 100) % 100,
number % 100
);
Copy link
Member

Choose a reason for hiding this comment

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

This change is included in #9476. See that issue for comments.

return version;
}

Expand Down
Loading