From 9b892eeb918a7416a62506f4c618a9de75e99165 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 12 Apr 2022 16:08:50 +0200 Subject: [PATCH] Fix webp_load data lifetime issues * src/image.c (webp_load): Take care of lifetime issues of the image data we're iterating over for animated images. --- src/image.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/image.c b/src/image.c index 3afb8324078..530819eab9d 100644 --- a/src/image.c +++ b/src/image.c @@ -9525,7 +9525,19 @@ webp_load (struct frame *f, struct image *img) WebPAnimDecoderDelete (cache->handle); WebPData webp_data; - webp_data.bytes = contents; + if (NILP (specified_data)) + /* If we got the data from a file, then we don't need to + copy the data. */ + webp_data.bytes = cache->temp = contents; + else + /* We got the data from a string, so copy it over so that + it doesn't get garbage-collected. */ + { + webp_data.bytes = xmalloc (size); + memcpy ((void*) webp_data.bytes, contents, size); + } + /* In any case, we release the allocated memory when we + purge the anim cache. */ webp_data.size = size; /* Get the width/height of the total image. */ @@ -9662,7 +9674,7 @@ webp_load (struct frame *f, struct image *img) /* Clean up. */ if (!anim) WebPFree (decoded); - if (NILP (specified_data)) + if (NILP (specified_data) && !anim) xfree (contents); return true; -- 2.39.5