From 81f191e4a63c967932e58229db2bf812b0f3588a Mon Sep 17 00:00:00 2001 From: Scott Lagler Date: Sun, 28 Jun 2026 07:57:39 -0400 Subject: [PATCH 1/2] fix: resolve strdup memory leak when calling DebugModule::GetPathBaseName and ModuleNameAndOffset::GetPathBaseName --- core/debugadapter.cpp | 7 +++---- core/debuggercommon.h | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/debugadapter.cpp b/core/debugadapter.cpp index 2390f183..f835a964 100644 --- a/core/debugadapter.cpp +++ b/core/debugadapter.cpp @@ -16,12 +16,10 @@ limitations under the License. #include #include +#include #include #include #include -#ifndef WIN32 - #include "libgen.h" -#endif #include "debugadapter.h" #include "debuggercontroller.h" @@ -80,7 +78,8 @@ std::string DebugModule::GetPathBaseName(const std::string& path) _splitpath_s(path.c_str(), NULL, 0, NULL, 0, baseName, MAX_PATH, ext, MAX_PATH); return std::string(baseName) + std::string(ext); #else - return basename(strdup(path.c_str())); + std::filesystem::path fs_path(path); + return fs_path.stem(); #endif } diff --git a/core/debuggercommon.h b/core/debuggercommon.h index 85a99362..70e08f51 100644 --- a/core/debuggercommon.h +++ b/core/debuggercommon.h @@ -15,11 +15,12 @@ limitations under the License. */ #pragma once +#include +#include #include #include -#ifndef WIN32 - #include "libgen.h" -#endif +#include +#include namespace BinaryNinjaDebugger { struct ModuleNameAndOffset @@ -63,7 +64,8 @@ namespace BinaryNinjaDebugger { _splitpath_s(path.c_str(), NULL, 0, NULL, 0, baseName, MAX_PATH, ext, MAX_PATH); return std::string(baseName) + std::string(ext); #else - return basename(strdup(path.c_str())); + std::filesystem::path fs_path(path); + return fs_path.stem(); #endif } From 627f0fadddc91452b3dd1960acc5e59d14ebefe5 Mon Sep 17 00:00:00 2001 From: Xusheng Date: Fri, 3 Jul 2026 12:11:13 -0400 Subject: [PATCH 2/2] Use path::filename() instead of stem() in GetPathBaseName stem() strips the final extension, changing behavior from the original POSIX basename() and the Windows branch (which both keep the extension). This would mangle module names like libc.so.6 -> libc.so and break module-name matching. filename() preserves the full final component. Co-Authored-By: Claude Opus 4.8 (1M context) --- core/debugadapter.cpp | 2 +- core/debuggercommon.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/debugadapter.cpp b/core/debugadapter.cpp index f835a964..2dd7cf7b 100644 --- a/core/debugadapter.cpp +++ b/core/debugadapter.cpp @@ -79,7 +79,7 @@ std::string DebugModule::GetPathBaseName(const std::string& path) return std::string(baseName) + std::string(ext); #else std::filesystem::path fs_path(path); - return fs_path.stem(); + return fs_path.filename().string(); #endif } diff --git a/core/debuggercommon.h b/core/debuggercommon.h index 70e08f51..6207f612 100644 --- a/core/debuggercommon.h +++ b/core/debuggercommon.h @@ -65,7 +65,7 @@ namespace BinaryNinjaDebugger { return std::string(baseName) + std::string(ext); #else std::filesystem::path fs_path(path); - return fs_path.stem(); + return fs_path.filename().string(); #endif }