From 31f59855b4811c6ace7ae5051f7873b602208d77 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sun, 29 Jan 2023 13:05:06 +0100 Subject: [PATCH] AK: Don't restore the stream offset when failing to read an LEB128 Pretty much no other read function does this, and getting rid of the typename template parameter for the stream makes the transition to the new AK::Stream a bit easier. --- AK/LEB128.h | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/AK/LEB128.h b/AK/LEB128.h index f9258fd866..6f550ff26d 100644 --- a/AK/LEB128.h +++ b/AK/LEB128.h @@ -13,26 +13,19 @@ namespace AK { struct LEB128 { - template - static bool read_unsigned(StreamT& stream, ValueType& result) + template + static bool read_unsigned(DeprecatedInputStream& stream, ValueType& result) { - [[maybe_unused]] size_t backup_offset = 0; - if constexpr (requires { stream.offset(); }) - backup_offset = stream.offset(); - DeprecatedInputStream& input_stream { stream }; - result = 0; size_t num_bytes = 0; while (true) { - if (input_stream.unreliable_eof()) { - if constexpr (requires { stream.seek(backup_offset); }) - stream.seek(backup_offset); - input_stream.set_fatal_error(); + if (stream.unreliable_eof()) { + stream.set_fatal_error(); return false; } u8 byte = 0; - input_stream >> byte; - if (input_stream.has_any_error()) + stream >> byte; + if (stream.has_any_error()) return false; ValueType masked_byte = byte & ~(1 << 7); @@ -53,16 +46,12 @@ struct LEB128 { return true; } - template - static bool read_signed(StreamT& stream, ValueType& result) + template + static bool read_signed(DeprecatedInputStream& stream, ValueType& result) { // Note: We read into a u64 to simplify the parsing logic; // result is range checked into ValueType after parsing. static_assert(sizeof(ValueType) <= sizeof(u64), "Error checking logic assumes 64 bits or less!"); - [[maybe_unused]] size_t backup_offset = 0; - if constexpr (requires { stream.offset(); }) - backup_offset = stream.offset(); - DeprecatedInputStream& input_stream { stream }; i64 temp = 0; size_t num_bytes = 0; @@ -70,15 +59,13 @@ struct LEB128 { result = 0; do { - if (input_stream.unreliable_eof()) { - if constexpr (requires { stream.seek(backup_offset); }) - stream.seek(backup_offset); - input_stream.set_fatal_error(); + if (stream.unreliable_eof()) { + stream.set_fatal_error(); return false; } - input_stream >> byte; - if (input_stream.has_any_error()) + stream >> byte; + if (stream.has_any_error()) return false; // note: 64 bit assumptions!