Fix crash when loading incompatible plugins

This commit is contained in:
Maschell 2026-02-11 11:44:52 +01:00
parent 157eca3c64
commit a7cd9ff36d
3 changed files with 36 additions and 17 deletions

View File

@ -48,7 +48,7 @@ PluginManagement::loadPlugins(const std::vector<PluginLoadWrapper> &pluginDataLi
plugins.emplace_back(std::move(*metaInfo), PluginLinkInformation::CreateStub(), pluginDataWrapper.getPluginData());
}
} else {
auto errMsg = string_format("Failed to load plugin: %s", *pluginDataWrapper.getPluginData()->getSource().c_str());
auto errMsg = string_format("Failed to load plugin: %s", pluginDataWrapper.getPluginData()->getSource().c_str());
if (error == PLUGIN_PARSE_ERROR_INCOMPATIBLE_VERSION) {
errMsg += ". Incompatible version.";
}

View File

@ -28,8 +28,41 @@
#include <string>
#include <memory>
#include <cstdarg>
#include <cstdio>
#include <cstring>
std::string string_format(const char *format, ...) {
char stack_buf[128];
va_list args;
va_start(args, format);
int len = std::vsnprintf(stack_buf, sizeof(stack_buf), format, args);
va_end(args);
if (len >= 0 && static_cast<size_t>(len) < sizeof(stack_buf)) {
return std::string(stack_buf, len);
}
// on error return the unformatted string
if (len < 0) {
return std::string(format);
}
const auto buf = make_unique_nothrow<char[]>(static_cast<size_t>(len));
if (!buf) {
DEBUG_FUNCTION_LINE_ERR("string_format failed, not enough memory");
OSFatal("WiiUPluginBackend: string_format failed, not enough memory");
return std::string("");
}
va_start(args, format);
std::vsnprintf(buf.get(), len, format, args);
va_end(args);
return std::string(buf.get(), len);
}
std::string StringTools::truncate(const std::string &str, const size_t width, const bool show_ellipsis) {
if (str.length() > width - 3) {
if (show_ellipsis) {

View File

@ -28,24 +28,10 @@
#include "logger.h"
#include "utils.h"
#include <coreinit/debug.h>
#include <memory>
#include <string>
template<typename... Args>
std::string string_format(const std::string_view format, Args... args) {
const int size_s = std::snprintf(nullptr, 0, format.data(), args...) + 1; // Extra space for '\0'
const auto size = static_cast<size_t>(size_s);
const auto buf = make_unique_nothrow<char[]>(size);
if (!buf) {
DEBUG_FUNCTION_LINE_ERR("string_format failed, not enough memory");
OSFatal("string_format failed, not enough memory");
return std::string("");
}
std::snprintf(buf.get(), size, format.data(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
std::string string_format(const char *format, ...)
WUT_FORMAT_PRINTF(1, 2);
class StringTools {
public: