From 6c683cff8964ce6d2bab0f504ef75354bda978b5 Mon Sep 17 00:00:00 2001 From: Kuan-Wei Chiu Date: Tue, 3 Oct 2023 01:15:24 +0800 Subject: [PATCH] Fix memory leak in realloc failure handling In the qp_lvgl_attach function, when reallocating memory for the color_buffer, we were not properly handling the case when realloc fails, leading to potential memory leaks. This change ensures that memory is properly managed, and potential memory leaks are prevented in the qp_lvgl_attach function. --- quantum/painter/lvgl/qp_lvgl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/quantum/painter/lvgl/qp_lvgl.c b/quantum/painter/lvgl/qp_lvgl.c index 280aeaf91f53..58733313ea47 100644 --- a/quantum/painter/lvgl/qp_lvgl.c +++ b/quantum/painter/lvgl/qp_lvgl.c @@ -96,13 +96,14 @@ bool qp_lvgl_attach(painter_device_t device) { // Set up lvgl display buffer static lv_disp_draw_buf_t draw_buf; // Allocate a buffer for 1/10 screen size - const size_t count_required = driver->panel_width * driver->panel_height / 10; - color_buffer = color_buffer ? realloc(color_buffer, sizeof(lv_color_t) * count_required) : malloc(sizeof(lv_color_t) * count_required); - if (!color_buffer) { + const size_t count_required = driver->panel_width * driver->panel_height / 10; + void * new_color_buffer = realloc(color_buffer, sizeof(lv_color_t) * count_required); + if (!new_color_buffer) { qp_dprintf("qp_lvgl_attach: fail (could not set up memory buffer)\n"); qp_lvgl_detach(); return false; } + color_buffer = new_color_buffer; memset(color_buffer, 0, sizeof(lv_color_t) * count_required); // Initialize the display buffer. lv_disp_draw_buf_init(&draw_buf, color_buffer, NULL, count_required);