From 03efe4a2ae09643649883bf2912e86c3c957d81e Mon Sep 17 00:00:00 2001 From: Frank Richter Date: Tue, 31 Dec 2024 12:32:51 +0100 Subject: [PATCH] Don't unconditionally free() xdg_data_home_dir It may have been returned from getenv()... --- src/unix/system.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/unix/system.c b/src/unix/system.c index 2fd4f3d8a..f3887f6a4 100644 --- a/src/unix/system.c +++ b/src/unix/system.c @@ -255,10 +255,12 @@ void Sys_Init(void) } // Attempt to respect user's XDG_DATA_HOME environment variable xdg_data_home_dir = getenv("XDG_DATA_HOME"); + bool xdg_data_home_dir_allocated = false; if (!xdg_data_home_dir) { size_t xdg_unset_len = strlen(homedir) + strlen("/.local/share") + 1; xdg_data_home_dir = malloc(xdg_unset_len); + xdg_data_home_dir_allocated = true; if (xdg_data_home_dir == NULL) { Sys_Error("%s:xdg_data_home_dir: malloc() failed.\n", __func__); @@ -274,7 +276,8 @@ void Sys_Init(void) check_snprintf = snprintf(homegamedir, sizeof(homegamedir), "%s/%s", xdg_data_home_dir, "quake2rtx"); - free(xdg_data_home_dir); + if (xdg_data_home_dir_allocated) + free(xdg_data_home_dir); if (check_snprintf < 0) { Sys_Error("%s:homegamedir: snprintf() failed with return "