1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:17:44 +00:00

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.
This commit is contained in:
Tim Schumacher 2023-01-29 13:05:06 +01:00 committed by Andrew Kaster
parent 371c51f934
commit 31f59855b4

View file

@ -13,26 +13,19 @@
namespace AK { namespace AK {
struct LEB128 { struct LEB128 {
template<typename StreamT, typename ValueType = size_t> template<typename ValueType = size_t>
static bool read_unsigned(StreamT& stream, ValueType& result) 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; result = 0;
size_t num_bytes = 0; size_t num_bytes = 0;
while (true) { while (true) {
if (input_stream.unreliable_eof()) { if (stream.unreliable_eof()) {
if constexpr (requires { stream.seek(backup_offset); }) stream.set_fatal_error();
stream.seek(backup_offset);
input_stream.set_fatal_error();
return false; return false;
} }
u8 byte = 0; u8 byte = 0;
input_stream >> byte; stream >> byte;
if (input_stream.has_any_error()) if (stream.has_any_error())
return false; return false;
ValueType masked_byte = byte & ~(1 << 7); ValueType masked_byte = byte & ~(1 << 7);
@ -53,16 +46,12 @@ struct LEB128 {
return true; return true;
} }
template<typename StreamT, typename ValueType = ssize_t> template<typename ValueType = ssize_t>
static bool read_signed(StreamT& stream, ValueType& result) static bool read_signed(DeprecatedInputStream& stream, ValueType& result)
{ {
// Note: We read into a u64 to simplify the parsing logic; // Note: We read into a u64 to simplify the parsing logic;
// result is range checked into ValueType after parsing. // result is range checked into ValueType after parsing.
static_assert(sizeof(ValueType) <= sizeof(u64), "Error checking logic assumes 64 bits or less!"); 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; i64 temp = 0;
size_t num_bytes = 0; size_t num_bytes = 0;
@ -70,15 +59,13 @@ struct LEB128 {
result = 0; result = 0;
do { do {
if (input_stream.unreliable_eof()) { if (stream.unreliable_eof()) {
if constexpr (requires { stream.seek(backup_offset); }) stream.set_fatal_error();
stream.seek(backup_offset);
input_stream.set_fatal_error();
return false; return false;
} }
input_stream >> byte; stream >> byte;
if (input_stream.has_any_error()) if (stream.has_any_error())
return false; return false;
// note: 64 bit assumptions! // note: 64 bit assumptions!