Skip to content

feat(options): Add strict option to return false if reaching the end of input early #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions include/alpaca/detail/from_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>) {
Expand Down Expand Up @@ -136,8 +141,13 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>) {
Expand Down Expand Up @@ -189,8 +199,13 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>) {
Expand Down Expand Up @@ -232,16 +247,21 @@ typename std::enable_if<
std::is_same_v<T, std::size_t>),
bool>::type
from_bytes(T &value, Container &bytes, std::size_t &current_index,
std::size_t &end_index, std::error_code &) {
std::size_t &end_index, std::error_code &error_code) {

if (current_index >= end_index) {
// end of input

// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

constexpr auto use_fixed_length_encoding = (
Expand Down Expand Up @@ -281,16 +301,21 @@ typename std::enable_if<
std::is_same_v<T, std::size_t>),
bool>::type
from_bytes(T &value, Container &bytes, std::size_t &current_index,
std::size_t &end_index, std::error_code &) {
std::size_t &end_index, std::error_code &error_code) {

if (current_index >= end_index) {
// end of input

// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

constexpr auto use_fixed_length_encoding = (
Expand Down Expand Up @@ -330,16 +355,21 @@ typename std::enable_if<
std::is_same_v<T, std::size_t>),
bool>::type
from_bytes(T &value, Container &bytes, std::size_t &current_index,
std::size_t &end_index, std::error_code &) {
std::size_t &end_index, std::error_code &error_code) {

if (current_index >= end_index) {
// end of input

// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

constexpr auto use_fixed_length_encoding = (
Expand Down Expand Up @@ -382,8 +412,13 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,
// default initialize the value
value = T();

// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

using underlying_type = typename std::underlying_type<T>::type;
Expand All @@ -396,4 +431,4 @@ from_bytes(T &value, Container &bytes, std::size_t &current_index,

} // namespace detail

} // namespace alpaca
} // namespace alpaca
7 changes: 6 additions & 1 deletion include/alpaca/detail/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class options {
with_version = 4,
with_checksum = 8,
force_aligned_access = 16,
strict = 32,
};

template <typename E> struct enable_bitmask_operators {
Expand Down Expand Up @@ -58,10 +59,14 @@ template <options O> constexpr bool force_aligned_access() {
return enum_has_flag<options, O, options::force_aligned_access>();
}

template <options O> constexpr bool strict() {
return enum_has_flag<options, O, options::strict>();
}

} // namespace detail

template <> struct enable_bitmask_operators<options> {
static constexpr bool enable = true;
};

} // namespace alpaca
} // namespace alpaca
11 changes: 8 additions & 3 deletions include/alpaca/detail/types/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ bool from_bytes(std::array<U, N> &output, Container &bytes,

if (byte_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

from_bytes_to_array<O>(output, bytes, byte_index, end_index, error_code);
Expand All @@ -77,4 +82,4 @@ bool from_bytes(std::array<U, N> &output, Container &bytes,
} // namespace detail

} // namespace alpaca
#endif
#endif
9 changes: 7 additions & 2 deletions include/alpaca/detail/types/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ bool from_bytes_to_bitset(std::bitset<N> &value, Container &bytes,

if (current_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

// current byte is the size of the vector
Expand Down
11 changes: 8 additions & 3 deletions include/alpaca/detail/types/deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ bool from_bytes_to_deque(std::deque<T> &value, Container &bytes,

if (current_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

// current byte is the size of the vector
Expand Down Expand Up @@ -94,4 +99,4 @@ bool from_bytes(std::deque<T> &output, Container &bytes,
} // namespace detail

} // namespace alpaca
#endif
#endif
11 changes: 8 additions & 3 deletions include/alpaca/detail/types/duration.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ bool from_bytes(std::chrono::duration<Rep, Period> &output, Container &bytes,

if (byte_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

Rep count{0};
Expand All @@ -62,4 +67,4 @@ bool from_bytes(std::chrono::duration<Rep, Period> &output, Container &bytes,
} // namespace detail

} // namespace alpaca
#endif
#endif
9 changes: 7 additions & 2 deletions include/alpaca/detail/types/filesystem_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ bool from_bytes(std::filesystem::path &value, Container &bytes,

if (current_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

// current byte is the length of the string
Expand Down
11 changes: 8 additions & 3 deletions include/alpaca/detail/types/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ bool from_bytes_to_list(std::list<T> &value, Container &bytes,

if (current_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

// current byte is the size of the vector
Expand Down Expand Up @@ -93,4 +98,4 @@ bool from_bytes(std::list<T> &output, Container &bytes, std::size_t &byte_index,
} // namespace detail

} // namespace alpaca
#endif
#endif
20 changes: 15 additions & 5 deletions include/alpaca/detail/types/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ bool from_bytes(std::map<K, V> &output, Container &bytes,

if (byte_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

from_bytes_to_map<O>(output, bytes, byte_index, end_index, error_code);
Expand All @@ -133,8 +138,13 @@ bool from_bytes(std::unordered_map<K, V> &output, Container &bytes,

if (byte_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

from_bytes_to_map<O>(output, bytes, byte_index, end_index, error_code);
Expand All @@ -144,4 +154,4 @@ bool from_bytes(std::unordered_map<K, V> &output, Container &bytes,

} // namespace detail

} // namespace alpaca
} // namespace alpaca
11 changes: 8 additions & 3 deletions include/alpaca/detail/types/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ bool from_bytes(std::optional<T> &output, Container &bytes,

if (byte_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

auto current_byte = bytes[byte_index];
Expand Down Expand Up @@ -78,4 +83,4 @@ bool from_bytes(std::optional<T> &output, Container &bytes,
} // namespace detail

} // namespace alpaca
#endif
#endif
11 changes: 8 additions & 3 deletions include/alpaca/detail/types/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ bool from_bytes(std::pair<T, U> &output, Container &bytes,

if (byte_index >= end_index) {
// end of input
// return true for forward compatibility
return true;
// return true for forward compatibility, unless strict mode is enabled
if constexpr (detail::strict<O>()) {
error_code = std::make_error_code(std::errc::bad_message);
return false;
} else {
return true;
}
}

from_bytes_router<O>(output.first, bytes, byte_index, end_index, error_code);
Expand All @@ -57,4 +62,4 @@ bool from_bytes(std::pair<T, U> &output, Container &bytes,
} // namespace detail

} // namespace alpaca
#endif
#endif
Loading
Loading