Skip to content

Commit 2473e57

Browse files
committed
Fix memory leaks in GIF animation loader
Incomplete cleanup on allocation failures is resolved.
1 parent 03fc2cb commit 2473e57

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/image-gif.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
576576
uint8_t *color, *frame;
577577
frame = malloc(gif->width * gif->height * 3);
578578
if (!frame) {
579+
free(anim->frame_delays);
580+
free(anim->frames);
579581
free(anim);
580582
gif_close(gif);
581583
return NULL;
@@ -584,6 +586,12 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
584586
anim->frames[i] =
585587
twin_pixmap_create(TWIN_ARGB32, gif->width, gif->height);
586588
if (!anim->frames[i]) {
589+
/* Free previously allocated pixmaps */
590+
for (twin_count_t j = 0; j < i; j++)
591+
twin_pixmap_destroy(anim->frames[j]);
592+
free(frame);
593+
free(anim->frame_delays);
594+
free(anim->frames);
587595
free(anim);
588596
gif_close(gif);
589597
return NULL;
@@ -617,7 +625,12 @@ static twin_animation_t *_twin_animation_from_gif_file(const char *path)
617625
}
618626
anim->iter = twin_animation_iter_init(anim);
619627
if (!anim->iter) {
628+
/* Free all allocated pixmaps */
629+
for (twin_count_t i = 0; i < frame_count; i++)
630+
twin_pixmap_destroy(anim->frames[i]);
620631
free(frame);
632+
free(anim->frame_delays);
633+
free(anim->frames);
621634
free(anim);
622635
gif_close(gif);
623636
return NULL;

0 commit comments

Comments
 (0)