From ab698c3d584c9b468d2877bad9ac202214de8e2e Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Sat, 17 Feb 2024 17:36:01 +0000 Subject: [PATCH] Clear memory allocated to resized bitmaps This prevents valgrind errors when resizing the screen to a larger size on GFX systems. --- xrdp/xrdp_bitmap_common.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/xrdp/xrdp_bitmap_common.c b/xrdp/xrdp_bitmap_common.c index 836ba819..9d0625fc 100644 --- a/xrdp/xrdp_bitmap_common.c +++ b/xrdp/xrdp_bitmap_common.c @@ -301,8 +301,6 @@ xrdp_bitmap_resize(struct xrdp_bitmap *self, int width, int height) return 1; } - self->width = width; - self->height = height; Bpp = 4; switch (self->bpp) @@ -318,8 +316,28 @@ xrdp_bitmap_resize(struct xrdp_bitmap *self, int width, int height) break; } - g_free(self->data); - self->data = (char *)g_malloc(width * height * Bpp, 0); + /* To prevent valgrind errors (particularly on a screen resize), + clear extra memory */ + unsigned long old_size = self->width * self->height * Bpp; + unsigned long new_size = width * height * Bpp; + + char *new_data = (char *)realloc(self->data, new_size); + if (new_data == NULL) + { + return 1; + } + + self->width = width; + self->height = height; + if (new_data != self->data) + { + self->data = new_data; + memset(self->data, 0, new_size); + } + else if (new_size > old_size) + { + memset(self->data + old_size, 0, new_size - old_size); + } self->line_size = width * Bpp; return 0; }