From 937ffb267c4c371405ec8910650ee8dbe020e079 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 22:53:42 +0200 Subject: [PATCH] Add and use Common::TruncateString --- src/common/string_util.h | 10 ++++++++++ src/core/hle/service/http/http_c.cpp | 5 ++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index 74c342e5bb..20cfec770d 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -84,6 +85,15 @@ std::string UTF16BufferToUTF8(const T& text) { return UTF16ToUTF8(buffer); } +/** + * Removes trailing null bytes from the string. + */ +[[nodiscard]] void TruncateString(std::string& str) { + while (str.size() && str.back() == '\0') { + str.pop_back(); + } +} + /** * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't * NUL-terminated then the string ends at max_len characters. diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index d0dbf410ba..4776cbc73f 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -10,6 +10,7 @@ #include #include "common/archives.h" #include "common/assert.h" +#include "common/string_util.h" #include "core/core.h" #include "core/file_sys/archive_ncch.h" #include "core/file_sys/file_backend.h" @@ -796,9 +797,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) { std::string header_name_str( reinterpret_cast(async_data->header_name.data()), async_data->name_len); - while (header_name_str.size() && header_name_str.back() == '\0') { - header_name_str.pop_back(); - } + Common::TruncateString(header_name_str); Context& http_context = async_data->own->GetContext(async_data->context_handle);