lib/image: Fix incorrect free of stbi-allocated buffer corrupting memmap

This commit is contained in:
Mintsuki
2026-04-13 00:08:55 +02:00
parent f3b031e37d
commit 0e6b0e9aac

View File

@@ -40,7 +40,7 @@ struct image *image_open(struct file_handle *file) {
if (image->img == NULL || x == 0 || y == 0) {
if (image->img != NULL) {
// stbi allocated but dimensions are degenerate
pmm_free(image->img, (size_t)x * (size_t)y * 4);
stbi_image_free(image->img);
}
pmm_free(image, sizeof(struct image));
return NULL;
@@ -49,7 +49,7 @@ struct image *image_open(struct file_handle *file) {
// Convert ABGR to XRGB
uint32_t *pptr = (void *)image->img;
size_t pixel_count = CHECKED_MUL((size_t)x, (size_t)y,
({ pmm_free(image, sizeof(struct image)); return NULL; }));
({ stbi_image_free(image->img); pmm_free(image, sizeof(struct image)); return NULL; }));
for (size_t i = 0; i < pixel_count; i++) {
pptr[i] = (pptr[i] & 0x0000ff00) | ((pptr[i] & 0x00ff0000) >> 16) | ((pptr[i] & 0x000000ff) << 16);
}
@@ -57,7 +57,7 @@ struct image *image_open(struct file_handle *file) {
image->x_size = x;
image->y_size = y;
image->pitch = (int)CHECKED_MUL((size_t)x, 4,
({ pmm_free(image, sizeof(struct image)); return NULL; }));
({ stbi_image_free(image->img); pmm_free(image, sizeof(struct image)); return NULL; }));
image->bpp = 32;
image->img_width = x;
image->img_height = y;