Skip to content

Commit

Permalink
Don't unconditionally free() xdg_data_home_dir
Browse files Browse the repository at this point in the history
It may have been returned from getenv()...
  • Loading branch information
res2k committed Dec 31, 2024
1 parent f78d722 commit 03efe4a
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand All @@ -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 "
Expand Down

0 comments on commit 03efe4a

Please sign in to comment.