From 3a6d258eaeae9415a81ad9da734da1777d93a893 Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Tue, 2 Oct 2018 18:39:04 +0200 Subject: [PATCH] gamemode: Make game_mode_context_find_exe() thread-safe Let's use our new safe_snprintf() helper. It's designed to make thread-safe usage easy and will also be used by the next commits. Reported-by: Alex Smith Signed-off-by: Kai Krakow --- daemon/gamemode.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/daemon/gamemode.c b/daemon/gamemode.c index 6ced936..4e20aae 100644 --- a/daemon/gamemode.c +++ b/daemon/gamemode.c @@ -677,13 +677,21 @@ GameModeContext *game_mode_context_instance() */ static char *game_mode_context_find_exe(pid_t pid) { - static char proc_path[PATH_MAX] = { 0 }; + char buffer[PATH_MAX]; + char *proc_path = NULL; - if (snprintf(proc_path, sizeof(proc_path), "/proc/%d/exe", pid) < 0) { - LOG_ERROR("Unable to find executable for PID %d: %s\n", pid, strerror(errno)); - return NULL; - } + if (!(proc_path = safe_snprintf(buffer, "/proc/%d/exe", pid))) + goto fail; /* Allocate the realpath if possible */ - return realpath(proc_path, NULL); + char *exe = realpath(proc_path, NULL); + free(proc_path); + if (!exe) + goto fail; + + return exe; + +fail: + LOG_ERROR("Unable to find executable for PID %d: %s\n", pid, strerror(errno)); + return NULL; }