From 3e467e220e4e407752381f264f405c50eaec1036 Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Wed, 3 Apr 2024 01:45:50 +0200 Subject: [PATCH] Logging: Prevent crash for nullptr strings --- src/Cafe/OS/common/OSUtil.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Cafe/OS/common/OSUtil.h b/src/Cafe/OS/common/OSUtil.h index 6801f6af..84e38a6c 100644 --- a/src/Cafe/OS/common/OSUtil.h +++ b/src/Cafe/OS/common/OSUtil.h @@ -140,6 +140,17 @@ static std::tuple cafeExportBuildArgTuple(PPCInterpreter_t* hCPU, R(fn) return std::tuple{ cafeExportGetParamWrapper(hCPU, gprIndex, fprIndex)... }; } +template +T cafeExportGetFormatParamWrapper(PPCInterpreter_t* hCPU, int& gprIndex, int& fprIndex) +{ + T v; + cafeExportParamWrapper::getParamWrapper(hCPU, gprIndex, fprIndex, v); + // if T is char* or const char*, return "null" instead of nullptr since newer fmtlib would throw otherwise + if constexpr (std::is_same_v || std::is_same_v) + return v ? v : (T)"null"; + return v; +} + template using _CAFE_FORMAT_ARG = std::conditional_t, std::conditional_t || std::is_same_v, T, MEMPTR>, T>; @@ -150,7 +161,7 @@ static auto cafeExportBuildFormatTuple(PPCInterpreter_t* hCPU, R(fn)(Args...)) int gprIndex = 0; int fprIndex = 0; return std::tuple<_CAFE_FORMAT_ARG...>{ - cafeExportGetParamWrapper<_CAFE_FORMAT_ARG>(hCPU, gprIndex, fprIndex)... + cafeExportGetFormatParamWrapper<_CAFE_FORMAT_ARG>(hCPU, gprIndex, fprIndex)... }; }