Skip to content

Commit

Permalink
Replace exceptions with HRESULTs
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Oct 12, 2021
1 parent e59eb93 commit 60312dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/terminal/parser/OutputStateMachineEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/terminal/parser/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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);
Expand All @@ -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.
//
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/terminal/parser/base64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

1 comment on commit 60312dd

@github-actions

This comment was marked as outdated.

Please sign in to comment.