From 61ff213685e35cda081a93be137c6c409df1968d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 15 Jun 2024 21:35:01 -0700 Subject: [PATCH] Use std::string for the disconnect event message --- al/event.cpp | 7 ++----- alc/alu.cpp | 16 ++++++++++++---- core/async_event.h | 3 ++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/al/event.cpp b/al/event.cpp index 5b8f4458..5d6a1c38 100644 --- a/al/event.cpp +++ b/al/event.cpp @@ -23,7 +23,6 @@ #include "AL/alext.h" #include "alc/context.h" -#include "alc/inprogext.h" #include "alsem.h" #include "alspan.h" #include "core/async_event.h" @@ -118,15 +117,13 @@ int EventThread(ALCcontext *context) }; auto proc_disconnect = [context,enabledevts](AsyncDisconnectEvent &evt) { - const std::string_view message{evt.msg.data()}; - context->debugMessage(DebugSource::System, DebugType::Error, 0, - DebugSeverity::High, message); + DebugSeverity::High, evt.msg); if(context->mEventCb && enabledevts.test(al::to_underlying(AsyncEnableBits::Disconnected))) context->mEventCb(AL_EVENT_TYPE_DISCONNECTED_SOFT, 0, 0, - static_cast(message.length()), message.data(), + static_cast(evt.msg.length()), evt.msg.c_str(), context->mEventParam); }; diff --git a/alc/alu.cpp b/alc/alu.cpp index 065250fa..7f8503dc 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -2284,14 +2284,22 @@ void DeviceBase::handleDisconnect(const char *msg, ...) auto &disconnect = std::get(evt); /* NOLINTBEGIN(*-array-to-pointer-decay) */ - va_list args; + va_list args, args2; va_start(args, msg); - int msglen{vsnprintf(disconnect.msg.data(), disconnect.msg.size(), msg, args)}; + va_copy(args2, args); + if(int msglen{vsnprintf(nullptr, 0, msg, args)}; msglen > 0) + { + disconnect.msg.resize(static_cast(msglen)+1_uz); + vsnprintf(disconnect.msg.data(), disconnect.msg.size(), msg, args2); + } + else + disconnect.msg = ""; + va_end(args2); va_end(args); /* NOLINTEND(*-array-to-pointer-decay) */ - if(msglen < 0 || static_cast(msglen) >= disconnect.msg.size()) - disconnect.msg[sizeof(disconnect.msg)-1] = 0; + while(!disconnect.msg.empty() && disconnect.msg.back() == '\0') + disconnect.msg.pop_back(); for(ContextBase *ctx : *mContexts.load()) { diff --git a/core/async_event.h b/core/async_event.h index 47b73da9..f865fd8a 100644 --- a/core/async_event.h +++ b/core/async_event.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "almalloc.h" @@ -40,7 +41,7 @@ struct AsyncBufferCompleteEvent { }; struct AsyncDisconnectEvent { - std::array msg; + std::string msg; }; struct AsyncEffectReleaseEvent { -- 2.11.4.GIT