From f590cd1850027399ab7f0193ba97fa76b3a9cbab Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 20 Jan 2022 17:01:39 +0000 Subject: [PATCH] AK+Userland: Make AK::decode_hex() return ErrorOr This lets us propagate the reason why it failed up to the caller. :^) --- AK/Base64.h | 2 +- AK/Hex.cpp | 20 +++++--------- AK/Hex.h | 4 +-- AK/UUID.cpp | 26 +++++++++---------- .../Applications/HexEditor/FindDialog.cpp | 4 +-- Userland/Libraries/LibPDF/Filter.cpp | 8 ++++-- 6 files changed, 31 insertions(+), 33 deletions(-) diff --git a/AK/Base64.h b/AK/Base64.h index d4c6de40f8..6b7eae8d87 100644 --- a/AK/Base64.h +++ b/AK/Base64.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include #include diff --git a/AK/Hex.cpp b/AK/Hex.cpp index dca499f0d8..ca35477968 100644 --- a/AK/Hex.cpp +++ b/AK/Hex.cpp @@ -1,44 +1,38 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include -#include #include -#include #include -#include #include #include namespace AK { -Optional decode_hex(StringView input) +ErrorOr decode_hex(StringView input) { if ((input.length() % 2) != 0) - return {}; + return Error::from_string_literal("Hex string was not an even length"); - auto output_result = ByteBuffer::create_zeroed(input.length() / 2); - if (output_result.is_error()) - return {}; - - auto& output = output_result.value(); + auto output = TRY(ByteBuffer::create_zeroed(input.length() / 2)); for (size_t i = 0; i < input.length() / 2; ++i) { const auto c1 = decode_hex_digit(input[i * 2]); if (c1 >= 16) - return {}; + return Error::from_string_literal("Hex string contains invalid digit"); const auto c2 = decode_hex_digit(input[i * 2 + 1]); if (c2 >= 16) - return {}; + return Error::from_string_literal("Hex string contains invalid digit"); output[i] = (c1 << 4) + c2; } - return output_result.release_value(); + return { move(output) }; } String encode_hex(const ReadonlyBytes input) diff --git a/AK/Hex.h b/AK/Hex.h index 8c631e4582..ba21c3a761 100644 --- a/AK/Hex.h +++ b/AK/Hex.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include #include @@ -24,7 +24,7 @@ constexpr u8 decode_hex_digit(char digit) return 255; } -Optional decode_hex(StringView); +ErrorOr decode_hex(StringView); String encode_hex(ReadonlyBytes); diff --git a/AK/UUID.cpp b/AK/UUID.cpp index 835da053b4..4b30f33830 100644 --- a/AK/UUID.cpp +++ b/AK/UUID.cpp @@ -19,21 +19,21 @@ UUID::UUID(Array uuid_buffer) void UUID::convert_string_view_to_uuid(StringView uuid_string_view) { VERIFY(uuid_string_view.length() == 36); - auto first_unit = decode_hex(uuid_string_view.substring_view(0, 8)); - auto second_unit = decode_hex(uuid_string_view.substring_view(9, 4)); - auto third_unit = decode_hex(uuid_string_view.substring_view(14, 4)); - auto fourth_unit = decode_hex(uuid_string_view.substring_view(19, 4)); - auto fifth_unit = decode_hex(uuid_string_view.substring_view(24, 12)); + auto first_unit = MUST(decode_hex(uuid_string_view.substring_view(0, 8))); + auto second_unit = MUST(decode_hex(uuid_string_view.substring_view(9, 4))); + auto third_unit = MUST(decode_hex(uuid_string_view.substring_view(14, 4))); + auto fourth_unit = MUST(decode_hex(uuid_string_view.substring_view(19, 4))); + auto fifth_unit = MUST(decode_hex(uuid_string_view.substring_view(24, 12))); - VERIFY(first_unit.value().size() == 4 && second_unit.value().size() == 2 - && third_unit.value().size() == 2 && fourth_unit.value().size() == 2 - && fifth_unit.value().size() == 6); + VERIFY(first_unit.size() == 4 && second_unit.size() == 2 + && third_unit.size() == 2 && fourth_unit.size() == 2 + && fifth_unit.size() == 6); - m_uuid_buffer.span().overwrite(0, first_unit.value().data(), first_unit.value().size()); - m_uuid_buffer.span().overwrite(4, second_unit.value().data(), second_unit.value().size()); - m_uuid_buffer.span().overwrite(6, third_unit.value().data(), third_unit.value().size()); - m_uuid_buffer.span().overwrite(8, fourth_unit.value().data(), fourth_unit.value().size()); - m_uuid_buffer.span().overwrite(10, fifth_unit.value().data(), fifth_unit.value().size()); + m_uuid_buffer.span().overwrite(0, first_unit.data(), first_unit.size()); + m_uuid_buffer.span().overwrite(4, second_unit.data(), second_unit.size()); + m_uuid_buffer.span().overwrite(6, third_unit.data(), third_unit.size()); + m_uuid_buffer.span().overwrite(8, fourth_unit.data(), fourth_unit.size()); + m_uuid_buffer.span().overwrite(10, fifth_unit.data(), fifth_unit.size()); } UUID::UUID(StringView uuid_string_view) diff --git a/Userland/Applications/HexEditor/FindDialog.cpp b/Userland/Applications/HexEditor/FindDialog.cpp index c1cb3399bf..a2531b2a26 100644 --- a/Userland/Applications/HexEditor/FindDialog.cpp +++ b/Userland/Applications/HexEditor/FindDialog.cpp @@ -77,8 +77,8 @@ Result FindDialog::process_input(String text_value, OptionId case OPTION_HEX_VALUE: { auto decoded = decode_hex(text_value.replace(" ", "", true)); - if (!decoded.has_value()) - return String("Input contains invalid hex values."); + if (decoded.is_error()) + return String::formatted("Input is invalid: {}", decoded.error().string_literal()); return decoded.value(); } diff --git a/Userland/Libraries/LibPDF/Filter.cpp b/Userland/Libraries/LibPDF/Filter.cpp index f5c15fa326..929f78ffca 100644 --- a/Userland/Libraries/LibPDF/Filter.cpp +++ b/Userland/Libraries/LibPDF/Filter.cpp @@ -39,8 +39,12 @@ Optional Filter::decode(ReadonlyBytes bytes, FlyString const& encodi Optional Filter::decode_ascii_hex(ReadonlyBytes bytes) { - if (bytes.size() % 2 == 0) - return decode_hex(bytes); + if (bytes.size() % 2 == 0) { + auto decode_result = decode_hex(bytes); + if (decode_result.is_error()) + return {}; + return decode_result.release_value(); + } // FIXME: Integrate this padding into AK/Hex?