From 53c5b6c0ebc2ebbbc53dbc3d45ab9bbffa1a24ff Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Wed, 17 Feb 2021 12:49:02 +0000 Subject: [PATCH] common: fix win32 event log source The Windows "get_process_name" function uses the input buffer to store the entire executable path, while the caller only expects the filename. The "get_process_name_cpp" function is using an insufficient buffer, for which reason it will return "(unknown)" when the executable path exceeds 32 characters. Windows event log entries have the wrong source because of this. We'll update "get_process_name" to use a separate buffer for the full executable path and avoid requesting a larger buffer than actually needed. Signed-off-by: Lucian Petrut (cherry picked from commit 86c7c2f5be96e80d492447d6582131f1b2d3438d) --- src/common/code_environment.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/common/code_environment.cc b/src/common/code_environment.cc index a821dfcdeace..c8cf82e55087 100644 --- a/src/common/code_environment.cc +++ b/src/common/code_environment.cc @@ -87,19 +87,23 @@ int get_process_name(char *buf, int len) return -EINVAL; } - int length = GetModuleFileNameA(nullptr, buf, len); + char full_path[MAX_PATH]; + int length = GetModuleFileNameA(nullptr, full_path, sizeof(full_path)); if (length <= 0) return -ENOSYS; - char* start = strrchr(buf, '\\'); + char* start = strrchr(full_path, '\\'); if (!start) return -ENOSYS; start++; char* end = strstr(start, ".exe"); if (!end) return -ENOSYS; + if (len <= end - start) { + return -ENAMETOOLONG; + } - memmove(buf, start, end - start); + memcpy(buf, start, end - start); buf[end - start] = '\0'; return 0; } -- 2.47.3