diff --git a/src/utils/StringTools.h b/src/utils/StringTools.h index a6ef4db..2c0e8f0 100644 --- a/src/utils/StringTools.h +++ b/src/utils/StringTools.h @@ -1,6 +1,7 @@ #pragma once #include "logger.h" +#include "utils.h" #include #include #include @@ -10,7 +11,12 @@ template std::string string_format(const std::string &format, Args... args) { int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0' auto size = static_cast(size_s); - auto buf = std::make_unique(size); + auto buf = make_unique_nothrow(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.c_str(), args...); return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside } diff --git a/src/utils/utils.h b/src/utils/utils.h index d8b796f..c480ee5 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -11,6 +11,11 @@ std::unique_ptr make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std:: return std::unique_ptr(new (std::nothrow) T(std::forward(args)...)); } +template +inline typename std::_MakeUniq::__array make_unique_nothrow(size_t num) noexcept { + return std::unique_ptr(new (std::nothrow) std::remove_extent_t[num]()); +} + template std::shared_ptr make_shared_nothrow(Args &&...args) noexcept(noexcept(T(std::forward(args)...))) { return std::shared_ptr(new (std::nothrow) T(std::forward(args)...));