Skip to content

Commit 7c8ae7d

Browse files
alecrbrownDaniel Kiper
authored and
Daniel Kiper
committed
gfxmenu/gui_image: Fix double free of bitmap
In grub-core/gfxmenu/gui_image.c, Coverity detected a double free in the function load_image(). The function checks if self->bitmap and self->raw_bitmap aren't NULL and then frees them. In the case self->bitmap and self->raw_bitmap are the same, only self->raw_bitmap is freed which would also free the memory used by self->bitmap. However, in this case self->bitmap isn't being set to NULL which could lead to a double free later in the code. After self->raw_bitmap is freed, it gets set to the variable bitmap. If this variable is NULL, the code could have a path that would free self->bitmap a second time in the function rescale_image(). Fixes: CID 292472 Signed-off-by: Alec Brown <[email protected]> Reviewed-by: Daniel Kiper <[email protected]>
1 parent 63fc253 commit 7c8ae7d

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

grub-core/gfxmenu/gui_image.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,16 @@ load_image (grub_gui_image_t self, const char *path)
195195
return grub_errno;
196196

197197
if (self->bitmap && (self->bitmap != self->raw_bitmap))
198-
{
199-
grub_video_bitmap_destroy (self->bitmap);
200-
self->bitmap = 0;
201-
}
198+
grub_video_bitmap_destroy (self->bitmap);
202199
if (self->raw_bitmap)
203200
grub_video_bitmap_destroy (self->raw_bitmap);
204201

202+
/*
203+
* Either self->bitmap is being freed or it shares memory with
204+
* self->raw_bitmap which is being freed. To ensure self->bitmap doesn't
205+
* point to memory that has been freed, we can set it to NULL.
206+
*/
207+
self->bitmap = NULL;
205208
self->raw_bitmap = bitmap;
206209
return rescale_image (self);
207210
}

0 commit comments

Comments
 (0)