From 902066930cac6f18c5987a223ba97d36c6432131 Mon Sep 17 00:00:00 2001 From: blattersturm Date: Sat, 23 Dec 2023 19:03:55 +0100 Subject: [PATCH] fix(steam): truncate presence to a valid UTF-8 sequence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With a host name such as "Êîðïîðàöèÿ Ìàéêðîñîôò; Âñå ïðàâà çàùèùåíû", we will try to set a presence name like "test: Êîðïîðàöèÿ Ìàéêðîñîôò; Âñå ïðàâà çàùèùåíû", assuming `steamname.txt` contains "test". Steam, internally, truncates app names to fit in a 64-byte buffer with a null terminator. However, the code which truncates does not ensure the resulting UTF-8 sequence is valid, which might lead to the update being rejected further down the line, which is especially likely with a name consisting solely of multi-byte sequences, like the example name. In this changeset, we circumvent this by truncating the name to 64 bytes ourselves, and subsequently chopping off at the first invalid UTF-8 sequence. --- code/components/steam/component.lua | 3 +++ code/components/steam/src/SteamComponent.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/code/components/steam/component.lua b/code/components/steam/component.lua index e69de29bb2..0228009c14 100644 --- a/code/components/steam/component.lua +++ b/code/components/steam/component.lua @@ -0,0 +1,3 @@ +return function() + add_dependencies { "vendor:utfcpp" } +end \ No newline at end of file diff --git a/code/components/steam/src/SteamComponent.cpp b/code/components/steam/src/SteamComponent.cpp index 85d86e952e..f19c04aeb9 100644 --- a/code/components/steam/src/SteamComponent.cpp +++ b/code/components/steam/src/SteamComponent.cpp @@ -19,6 +19,8 @@ #include #include +#include + struct CfxPresenceState { char gameName[512]; @@ -403,6 +405,17 @@ void SteamComponent::InitializePresence() productName += fmt::sprintf(": %s", gameData->gameName); } + // Steam requires the name to fit in a 64-byte buffer, so we try to make sure there's no unfinished UTF-8 sequences in that case + if (productName.length() >= 64) + { + productName = productName.substr(0, 63); + + if (auto invalidPos = utf8::find_invalid(productName); invalidPos != std::string::npos) + { + productName = productName.substr(0, invalidPos); + } + } + // set our pipe appid InterfaceMapper steamUtils(m_clientEngine->GetIClientUtils(m_steamPipe, "CLIENTUTILS_INTERFACE_VERSION001"));