From bd122c738d17f8ce24bf62803e33edc9dbf29adc Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Fri, 20 Sep 2019 10:04:35 +0200 Subject: [PATCH 1/4] backend: driver: fix memory leak (randr_version not freed) Fixes the following memory leaks reported by valgrind: 32 bytes in 1 blocks are definitely lost in loss record 15 of 111 at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x49DB0DD: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49D8FB4: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA63E: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA751: xcb_wait_for_reply (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x14B16F: detect_driver (driver.c:20) by 0x119949: session_init (compton.c:1879) by 0x11AEF0: main (compton.c:2285) 32 bytes in 1 blocks are definitely lost in loss record 16 of 111 at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x49DB0DD: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49D8FB4: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA63E: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA751: xcb_wait_for_reply (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x14B16F: detect_driver (driver.c:20) by 0x11795E: redir_start (compton.c:1202) by 0x115C76: paint_preprocess (compton.c:629) by 0x1182BD: _draw_callback (compton.c:1380) by 0x1184D0: draw_callback (compton.c:1426) by 0x49BF292: ev_invoke_pending (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) by 0x49C3344: ev_run (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) Also, free(randr_version) in the error path as well. --- src/backend/driver.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/backend/driver.c b/src/backend/driver.c index 5346067..d7c62d7 100644 --- a/src/backend/driver.c +++ b/src/backend/driver.c @@ -26,6 +26,7 @@ enum driver detect_driver(xcb_connection_t *c, backend_t *backend_data, xcb_wind c, xcb_randr_get_providers(c, window), NULL); if (r == NULL) { log_warn("Failed to get RANDR providers"); + free(randr_version); return 0; } @@ -61,6 +62,7 @@ enum driver detect_driver(xcb_connection_t *c, backend_t *backend_data, xcb_wind } free(r); } + free(randr_version); // If the backend supports driver detection, use that as well if (backend_data && backend_data->ops->detect_driver) { From a7170cef81e9027b6ade44ea060441bca7eb40ca Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Fri, 20 Sep 2019 10:07:31 +0200 Subject: [PATCH 2/4] backend: driver: fix memory leak (r2 not freed) Fixes the following memory leaks reported by valgrind: 96 bytes in 1 blocks are definitely lost in loss record 54 of 111 at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x49DB0DD: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49D8FB4: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA63E: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA751: xcb_wait_for_reply (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x14B266: detect_driver (driver.c:34) by 0x119949: session_init (compton.c:1879) by 0x11AEF0: main (compton.c:2285) 96 bytes in 1 blocks are definitely lost in loss record 55 of 111 at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x49DB0DD: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49D8FB4: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA63E: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA751: xcb_wait_for_reply (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x14B266: detect_driver (driver.c:34) by 0x11795E: redir_start (compton.c:1202) by 0x115C76: paint_preprocess (compton.c:629) by 0x1182BD: _draw_callback (compton.c:1380) by 0x1184D0: draw_callback (compton.c:1426) by 0x49BF292: ev_invoke_pending (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) by 0x49C3344: ev_run (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) --- src/backend/driver.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend/driver.c b/src/backend/driver.c index d7c62d7..a91fd53 100644 --- a/src/backend/driver.c +++ b/src/backend/driver.c @@ -59,6 +59,7 @@ enum driver detect_driver(xcb_connection_t *c, backend_t *backend_data, xcb_wind ret |= DRIVER_INTEL; } free(name); + free(r2); } free(r); } From fcec9fcd3958f10d822c511b30d92e55b3491d23 Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Fri, 20 Sep 2019 10:10:37 +0200 Subject: [PATCH 3/4] core: fix memory leak in _draw_callback Fixes the following memory leak reported by valgrind: 992 bytes in 31 blocks are definitely lost in loss record 81 of 111 at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x49DB0DD: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49D8FB4: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA63E: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA751: xcb_wait_for_reply (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x118094: _draw_callback (compton.c:1335) by 0x1184D0: draw_callback (compton.c:1426) by 0x49BF292: ev_invoke_pending (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) by 0x49C3344: ev_run (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) by 0x11AD65: session_run (compton.c:2226) by 0x11B005: main (compton.c:2308) To prevent accidentaly reusing the freed structure, wrap it in a lexical scope. --- src/compton.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/compton.c b/src/compton.c index 8a544bc..d927674 100644 --- a/src/compton.c +++ b/src/compton.c @@ -1332,9 +1332,12 @@ static void _draw_callback(EV_P_ session_t *ps, int revents attr_unused) { // Call fill_win on new windows handle_new_windows(ps); - auto r = xcb_get_input_focus_reply(ps->c, xcb_get_input_focus(ps->c), NULL); - if (!ps->active_win || (r && r->focus != ps->active_win->base.id)) { - recheck_focus(ps); + { + auto r = xcb_get_input_focus_reply(ps->c, xcb_get_input_focus(ps->c), NULL); + if (!ps->active_win || (r && r->focus != ps->active_win->base.id)) { + recheck_focus(ps); + } + free(r); } // Refresh pixmaps From 23421b2ff8dad03d3991997ee6c9c72c53abfc69 Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Fri, 20 Sep 2019 10:13:07 +0200 Subject: [PATCH 4/4] win: fix memory leak in fill_win Fixes the following memory leak reported by valgrind: 1,056 bytes in 24 blocks are definitely lost in loss record 83 of 111 at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x49DB0DD: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49D8FB4: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA63E: ??? (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x49DA751: xcb_wait_for_reply (in /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0) by 0x11FD5B: fill_win (win.c:1165) by 0x117CE6: handle_new_windows (compton.c:1266) by 0x118065: _draw_callback (compton.c:1333) by 0x1184D0: draw_callback (compton.c:1426) by 0x49BF292: ev_invoke_pending (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) by 0x49C3344: ev_run (in /usr/lib/x86_64-linux-gnu/libev.so.4.0.0) by 0x11AD65: session_run (compton.c:2226) This one out of multiple code paths forgot to free the structure. Yeah, manual memory management is hard. :-) --- src/win.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/win.c b/src/win.c index 1c41214..9c1294d 100644 --- a/src/win.c +++ b/src/win.c @@ -1176,6 +1176,7 @@ struct win *fill_win(session_t *ps, struct win *w) { if (a->_class == XCB_WINDOW_CLASS_INPUT_ONLY) { // No need to manage this window, but we still keep it on the window stack w->managed = false; + free(a); return w; }