Skip to content

Commit d45e7a6

Browse files
committed
lib/image: Fix incorrect free of stbi-allocated buffer corrupting memmap
1 parent 4f701f9 commit d45e7a6

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

common/lib/image.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct image *image_open(struct file_handle *file) {
4040
if (image->img == NULL || x == 0 || y == 0) {
4141
if (image->img != NULL) {
4242
// stbi allocated but dimensions are degenerate
43-
pmm_free(image->img, (size_t)x * (size_t)y * 4);
43+
stbi_image_free(image->img);
4444
}
4545
pmm_free(image, sizeof(struct image));
4646
return NULL;
@@ -49,15 +49,15 @@ struct image *image_open(struct file_handle *file) {
4949
// Convert ABGR to XRGB
5050
uint32_t *pptr = (void *)image->img;
5151
size_t pixel_count = CHECKED_MUL((size_t)x, (size_t)y,
52-
({ pmm_free(image, sizeof(struct image)); return NULL; }));
52+
({ stbi_image_free(image->img); pmm_free(image, sizeof(struct image)); return NULL; }));
5353
for (size_t i = 0; i < pixel_count; i++) {
5454
pptr[i] = (pptr[i] & 0x0000ff00) | ((pptr[i] & 0x00ff0000) >> 16) | ((pptr[i] & 0x000000ff) << 16);
5555
}
5656

5757
image->x_size = x;
5858
image->y_size = y;
5959
image->pitch = (int)CHECKED_MUL((size_t)x, 4,
60-
({ pmm_free(image, sizeof(struct image)); return NULL; }));
60+
({ stbi_image_free(image->img); pmm_free(image, sizeof(struct image)); return NULL; }));
6161
image->bpp = 32;
6262
image->img_width = x;
6363
image->img_height = y;

0 commit comments

Comments
 (0)