fix: memory leak in img_load_webp (#135)

if `multi.cap` is >0 that means `multi.frames` has already been malloc-ed. by
unconditionally malloc-ing again, we're losing all the old memory.

this makes it so we're only malloc-ing (or realloc-ing) when needed.
This commit is contained in:
N-R-K 2021-10-26 23:41:11 +06:00 committed by GitHub
parent bbebd45ce6
commit 1f01c670c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

14
image.c
View File

@ -380,12 +380,18 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS); flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
img->multi.cap = info.frame_count;
img->multi.sel = 0; if (img->multi.cap == 0) {
img->multi.frames = emalloc(info.frame_count * sizeof(img_frame_t)); img->multi.cap = info.frame_count;
img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
} else if (info.frame_count > img->multi.cap) {
img->multi.cap = info.frame_count;
img->multi.frames = erealloc(img->multi.frames,
img->multi.cap * sizeof(img_frame_t));
}
/* Load and decode frames (also works on images with only 1 frame) */ /* Load and decode frames (also works on images with only 1 frame) */
img->multi.cnt = 0; img->multi.cnt = img->multi.sel = 0;
while (WebPAnimDecoderGetNext(dec, &buf, &ts)) { while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
im = imlib_create_image_using_copied_data( im = imlib_create_image_using_copied_data(
info.canvas_width, info.canvas_height, (DATA32*)buf); info.canvas_width, info.canvas_height, (DATA32*)buf);