From ff4ed323e080905ad766efb4f4ec9fbd947fa364 Mon Sep 17 00:00:00 2001 From: jyelon Date: Mon, 11 Sep 2023 11:58:26 -0400 Subject: [PATCH] Some changes to the design of base-writer.hpp --- luprex/cpp/core/drivenengine.cpp | 8 ++++++-- luprex/cpp/core/streambuffer.hpp | 6 ++---- luprex/ext/base-writer.hpp | 26 ++++++++++++++++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/luprex/cpp/core/drivenengine.cpp b/luprex/cpp/core/drivenengine.cpp index d9fb5ad2..eee4c0ac 100644 --- a/luprex/cpp/core/drivenengine.cpp +++ b/luprex/cpp/core/drivenengine.cpp @@ -235,8 +235,12 @@ class ReplayLogfile : public BaseReader, public std::ifstream { using std::ifstream::ifstream; public: using read_string_type = std::string; - void read_bytes_into(char *n, size_t size) { read(n, size); } - bool read_beyond_eof() { return !good(); } + void read_bytes_into(char *n, size_t size) { + read(n, size); + if (!good()) { + memset(n, 0, size); + } + } void raise_string_too_long() { fprintf(stderr, "string in logfile is too long"); std::abort(); diff --git a/luprex/cpp/core/streambuffer.hpp b/luprex/cpp/core/streambuffer.hpp index 3759c5c3..a1b42017 100644 --- a/luprex/cpp/core/streambuffer.hpp +++ b/luprex/cpp/core/streambuffer.hpp @@ -302,6 +302,8 @@ public: // Copy bytes from the StreamBuffer into an external buffer. // + // Returns true if the bytes were successfully read. + // void read_bytes_into(char *target, int64_t len); // Read a string as a string_view. @@ -387,10 +389,6 @@ public: void raise_truncated() { throw StreamCorruption(); } void raise_string_too_long() { throw StreamCorruption(); } - // This is always false, because this module throws exceptions - // when reading beyond EOF. - bool read_beyond_eof() { return false; } - private: // Start and end of the allocated block. char *buf_lo_; diff --git a/luprex/ext/base-writer.hpp b/luprex/ext/base-writer.hpp index 8156dd5f..0ef75760 100644 --- a/luprex/ext/base-writer.hpp +++ b/luprex/ext/base-writer.hpp @@ -118,8 +118,21 @@ public: // // using read_string_type = std::string; // or compatible // void read_bytes_into(char *n, size_t size) -// void raise_string_too_long(); -// bool read_beyond_eof(); +// void handle_string_too_long(); +// +// Error Handling: +// +// It is up to the derived class whether it wants +// to report errors using exceptions or flags. +// +// If read_bytes_into discovers there's not enough bytes, +// there are two valid options: throw an exception, OR, +// set an error flag and fill the buffer with zeros. +// +// If read_string discovers that the string is longer than +// the allowed limit, it will call handle_string_too_long. +// This function may either throw an exception, or set an +// error flag. // /////////////////////////////////////////////////////////////// @@ -131,7 +144,6 @@ protected: T result; Derived *dthis = static_cast(this); dthis->read_bytes_into((char *)(&result), sizeof(result)); - if (dthis->read_beyond_eof()) result = 0; return result; } @@ -146,7 +158,7 @@ public: int32_t read_int32() { return read_value_core(); } int64_t read_int64() { return read_value_core(); } - bool read_bool() { return read_uint8(); } + bool read_bool() { return (bool)read_uint8(); } char read_char() { return read_value_core(); } float read_float() { return read_value_core(); } double read_double() { return read_value_core(); } @@ -162,10 +174,12 @@ public: auto read_string_limit(uint64_t limit) { size_t len = read_length(); Derived *dthis = static_cast(this); - if (len > limit) dthis->raise_string_too_long(); + if (len > limit) { + dthis->raise_string_too_long(); + len = 0; + } typename Derived::read_string_type result(len, ' '); dthis->read_bytes_into(&(result[0]), len); - if (dthis->read_beyond_eof()) result.clear(); return result; }