From 60312ddf56c7179d28b57c2bd1ee887f813c66d9 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Wed, 13 Oct 2021 01:06:50 +0200 Subject: [PATCH] Replace exceptions with HRESULTs --- src/terminal/parser/OutputStateMachineEngine.cpp | 7 +------ src/terminal/parser/base64.cpp | 13 ++++++++----- src/terminal/parser/base64.hpp | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/terminal/parser/OutputStateMachineEngine.cpp b/src/terminal/parser/OutputStateMachineEngine.cpp index 71d8752c0c8..9695efcc865 100644 --- a/src/terminal/parser/OutputStateMachineEngine.cpp +++ b/src/terminal/parser/OutputStateMachineEngine.cpp @@ -1112,12 +1112,7 @@ bool OutputStateMachineEngine::_GetOscSetClipboard(const std::wstring_view strin } else { - try - { - Base64::Decode(substr, content); - return true; - } - CATCH_LOG() + LOG_IF_FAILED(Base64::Decode(substr, content)); } } diff --git a/src/terminal/parser/base64.cpp b/src/terminal/parser/base64.cpp index 38c1c1e8757..499be69d9ea 100644 --- a/src/terminal/parser/base64.cpp +++ b/src/terminal/parser/base64.cpp @@ -4,6 +4,9 @@ #include "precomp.h" #include "base64.hpp" +// Regarding C4297: I didn't want to handle OOM errors. There's no reasonable mode +// of operation for this application without the ability to allocate memory anyways. +#pragma warning(disable : 4297) // '...': function assumed not to throw an exception but does #pragma warning(disable : 26446) // Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4). #pragma warning(disable : 26481) // Don't use pointer arithmetic. Use span instead (bounds.1). #pragma warning(disable : 26482) // Only index into arrays using constant expressions (bounds.2). @@ -30,7 +33,7 @@ static constexpr uint8_t decodeTable[128] = { // * Doesn't support whitespace and will throw an exception for such strings. // * Doesn't validate the number of trailing "=". Those are basically ignored. // Strings like "YQ===" will be accepted as valid input and simply result in "a". -void Base64::Decode(const std::wstring_view& src, std::wstring& dst) +HRESULT Base64::Decode(const std::wstring_view& src, std::wstring& dst) noexcept { std::string result; result.resize(((src.size() + 3) / 4) * 3); @@ -39,7 +42,7 @@ void Base64::Decode(const std::wstring_view& src, std::wstring& dst) // The remaining code in this function ensures not to read from in if src.empty(). #pragma warning(suppress : 26429) // Symbol 'in' is never tested for nullness, it can be marked as not_null (f.23). auto in = src.data(); - auto inEnd = in + src.size(); + const auto inEnd = in + src.size(); // Sometimes in programming you have to ask yourself what the right offset for a pointer is. // Is 4 enough? Certainly not. 6 on the other hand is just way too much. Clearly 5 is just right. // @@ -144,12 +147,12 @@ void Base64::Decode(const std::wstring_view& src, std::wstring& dst) break; } } - + if (error) { - throw std::runtime_error("invalid base64"); + return HRESULT_FROM_WIN32(ERROR_INVALID_DATA); } result.resize(out - outBeg); - THROW_IF_FAILED(til::u8u16(result, dst)); + return til::u8u16(result, dst); } diff --git a/src/terminal/parser/base64.hpp b/src/terminal/parser/base64.hpp index 5ad7b714f62..da8f22ec509 100644 --- a/src/terminal/parser/base64.hpp +++ b/src/terminal/parser/base64.hpp @@ -16,6 +16,6 @@ namespace Microsoft::Console::VirtualTerminal class Base64 { public: - static void Decode(const std::wstring_view& src, std::wstring& dst); + static HRESULT Decode(const std::wstring_view& src, std::wstring& dst) noexcept; }; }