From 672c5ae24339ee2d85867e026e6faccbc236694d Mon Sep 17 00:00:00 2001 From: Antoine Lambert Date: Thu, 7 Sep 2023 21:42:01 +0200 Subject: [PATCH] cpp: Remove use of unsafe sprintf function and use snprintf --- library/talipot-core/src/PluginLibraryLoader.cpp | 5 +++-- utils/crash_handler/StackWalker.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/library/talipot-core/src/PluginLibraryLoader.cpp b/library/talipot-core/src/PluginLibraryLoader.cpp index 2c0f1f68ed..e166d19d5d 100644 --- a/library/talipot-core/src/PluginLibraryLoader.cpp +++ b/library/talipot-core/src/PluginLibraryLoader.cpp @@ -141,8 +141,9 @@ bool PluginLibraryLoader::loadPluginLibrary(const std::string &filename, PluginL nullptr); // no inserts if (!msg) { - char scode[128]; - sprintf(scode, "%s: unable to load(error %d)", filename.c_str(), int(dwErrCode)); + const size_t size = 128; + char scode[size]; + snprintf(scode, size, "%s: unable to load(error %d)", filename.c_str(), int(dwErrCode)); loader->aborted(filename, std::string(scode)); } else { loader->aborted(filename, filename + ": " + msg); diff --git a/utils/crash_handler/StackWalker.cpp b/utils/crash_handler/StackWalker.cpp index 118ed358af..ee75d9fa99 100644 --- a/utils/crash_handler/StackWalker.cpp +++ b/utils/crash_handler/StackWalker.cpp @@ -39,15 +39,16 @@ int backtrace(void **buffer, int size) { char *getStackFrameDetails(void *address) { Dl_info dli; - char tmp[1024]; + const size_t size = 1024; + char tmp[size]; if (dladdr(address, &dli)) { int64_t function_offset = reinterpret_cast(address) - reinterpret_cast(dli.dli_saddr); - sprintf(tmp, "%s(%s+%p)[%p]", dli.dli_fname, dli.dli_sname, - reinterpret_cast(function_offset), address); + snprintf(tmp, size, "%s(%s+%p)[%p]", dli.dli_fname, dli.dli_sname, + reinterpret_cast(function_offset), address); } else { - sprintf(tmp, "%s(%s+%s)[%p]", "???", "???", "???", address); + snprintf(tmp, size, "%s(%s+%s)[%p]", "???", "???", "???", address); } char *ret = new char[strlen(tmp) + 1];