Jelajahi Sumber

LibIMAP: Propagate OOM errors from decode_quoted_printable()

Linus Groh 2 tahun lalu
induk
melakukan
f068ddb79f

+ 1 - 1
Meta/Lagom/Fuzzers/FuzzQuotedPrintableParser.cpp

@@ -11,6 +11,6 @@
 extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
 {
     auto quoted_printable_string = StringView(static_cast<unsigned char const*>(data), size);
-    IMAP::decode_quoted_printable(quoted_printable_string);
+    (void)IMAP::decode_quoted_printable(quoted_printable_string);
     return 0;
 }

+ 3 - 3
Tests/LibIMAP/TestQuotedPrintable.cpp

@@ -11,12 +11,12 @@
 TEST_CASE(test_decode)
 {
     auto decode_equal = [](StringView input, StringView expected) {
-        auto decoded = IMAP::decode_quoted_printable(input);
+        auto decoded = MUST(IMAP::decode_quoted_printable(input));
         EXPECT(decoded.bytes() == expected.bytes());
     };
 
     auto decode_equal_byte_buffer = [](StringView input, StringView expected) {
-        auto decoded = IMAP::decode_quoted_printable(input);
+        auto decoded = MUST(IMAP::decode_quoted_printable(input));
         EXPECT(decoded.bytes() == expected.bytes());
     };
 
@@ -44,6 +44,6 @@ TEST_CASE(test_decode)
             illegal_character_builder.append(byte);
     }
 
-    auto illegal_character_decode = IMAP::decode_quoted_printable(illegal_character_builder.to_deprecated_string());
+    auto illegal_character_decode = MUST(IMAP::decode_quoted_printable(illegal_character_builder.to_deprecated_string()));
     EXPECT(illegal_character_decode.is_empty());
 }

+ 1 - 1
Userland/Applications/Mail/MailWidget.cpp

@@ -500,7 +500,7 @@ void MailWidget::selected_email_to_load()
         if (!decoded_base64.is_error())
             decoded_data = decoded_base64.release_value();
     } else if (selected_alternative_encoding.equals_ignoring_case("quoted-printable"sv)) {
-        decoded_data = IMAP::decode_quoted_printable(encoded_data);
+        decoded_data = IMAP::decode_quoted_printable(encoded_data).release_value_but_fixme_should_propagate_errors();
     } else {
         dbgln("Mail: Unimplemented decoder for encoding: {}", selected_alternative_encoding);
         GUI::MessageBox::show(window(), DeprecatedString::formatted("The e-mail encoding '{}' is currently unsupported.", selected_alternative_encoding), "Unsupported"sv, GUI::MessageBox::Type::Information);

+ 6 - 6
Userland/Libraries/LibIMAP/QuotedPrintable.cpp

@@ -17,7 +17,7 @@ static constexpr bool is_illegal_character(char c)
 }
 
 // RFC 2045 Section 6.7 "Quoted-Printable Content-Transfer-Encoding", https://datatracker.ietf.org/doc/html/rfc2045#section-6.7
-ByteBuffer decode_quoted_printable(StringView input)
+ErrorOr<ByteBuffer> decode_quoted_printable(StringView input)
 {
     GenericLexer lexer(input);
     StringBuilder output;
@@ -50,7 +50,7 @@ ByteBuffer decode_quoted_printable(StringView input)
 
                 if (is_ascii_hex_digit(second_escape_character)) {
                     u8 actual_character = (parse_ascii_hex_digit(first_escape_character) << 4) | parse_ascii_hex_digit(second_escape_character);
-                    output.append(actual_character);
+                    TRY(output.try_append(actual_character));
                 } else {
                     TODO();
                 }
@@ -72,15 +72,15 @@ ByteBuffer decode_quoted_printable(StringView input)
                 }
 
                 // Invalid escape sequence. RFC 2045 says a reasonable solution is just to append '=' followed by the character.
-                output.append('=');
-                output.append(first_escape_character);
+                TRY(output.try_append('='));
+                TRY(output.try_append(first_escape_character));
             }
         } else {
-            output.append(potential_character);
+            TRY(output.try_append(potential_character));
         }
     }
 
-    return output.to_byte_buffer();
+    return output.try_to_byte_buffer();
 }
 
 }

+ 1 - 1
Userland/Libraries/LibIMAP/QuotedPrintable.h

@@ -10,6 +10,6 @@
 
 namespace IMAP {
 
-ByteBuffer decode_quoted_printable(StringView);
+ErrorOr<ByteBuffer> decode_quoted_printable(StringView);
 
 }