Skip to content

Commit

Permalink
Fix various instances of undefined behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Feb 10, 2024
1 parent e7e5c06 commit 57ea0d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class Encoder : private BasicEncoder<CharT, Traits> {
divide_floor(options.maximum, options.multiplier)};
#endif
assert(is_byte(enum_maximum - enum_minimum));
this->put_byte(
static_cast<std::uint8_t>((value / options.multiplier) - enum_minimum));
this->put_byte(static_cast<std::uint8_t>(
static_cast<std::int64_t>(value / options.multiplier) - enum_minimum));
}

auto FLOOR_MULTIPLE_ENUM_VARINT(const sourcemeta::jsontoolkit::JSON &document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ template <typename CharT, typename Traits> class BasicEncoder {
}

inline auto put_byte(const std::uint8_t byte) -> void {
this->stream.put(static_cast<std::int8_t>(byte));
this->stream.put(static_cast<CharT>(byte));
}

inline auto put_bytes(const std::uint16_t bytes) -> void {
Expand All @@ -53,7 +53,7 @@ template <typename CharT, typename Traits> class BasicEncoder {
// Do a manual for-loop based on the provided length instead of a range
// loop based on the string value to avoid accidental overflows
for (std::uint64_t index = 0; index < length; index++) {
this->put_byte(string[index]);
this->put_byte(static_cast<std::uint8_t>(string[index]));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_varint.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ auto varint_encode(std::basic_ostream<CharT, Traits> &stream,
std::uint64_t accumulator = value;

while (accumulator > LEAST_SIGNIFICANT_BITS) {
stream.put(static_cast<std::uint8_t>(
(accumulator & LEAST_SIGNIFICANT_BITS) | MOST_SIGNIFICANT_BIT));
stream.put(static_cast<CharT>((accumulator & LEAST_SIGNIFICANT_BITS) |
MOST_SIGNIFICANT_BIT));
accumulator >>= SHIFT;
}

stream.put(static_cast<std::uint8_t>(accumulator));
stream.put(static_cast<CharT>(accumulator));
return stream;
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_zigzag.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace sourcemeta::jsonbinpack {
constexpr auto zigzag_encode(const std::int64_t value) noexcept
-> std::uint64_t {
if (value >= 0) {
return value * 2;
return static_cast<std::uint64_t>(value * 2);
}

return (std::abs(value) * 2) - 1;
return (static_cast<std::uint64_t>(std::abs(value)) * 2) - 1;
}

constexpr auto zigzag_decode(const std::uint64_t value) noexcept
Expand Down
10 changes: 7 additions & 3 deletions test/runtime/encode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <string> // std::basic_string
#include <vector> // std::vector

static inline auto to_byte(std::uint8_t input) -> std::byte {
static inline auto to_byte(const std::uint8_t input) -> std::byte {
return std::byte{input};
}

Expand All @@ -24,7 +24,9 @@ class OutputByteStream : public std::basic_ostringstream<CharT> {
std::vector<std::byte> result{};
const std::basic_string<CharT> string{this->str()};
std::transform(string.cbegin(), string.cend(), std::back_inserter(result),
to_byte);
[](const auto character) {
return to_byte(static_cast<std::uint8_t>(character));
});
return result;
}
};
Expand All @@ -35,7 +37,9 @@ auto EXPECT_BYTES(const OutputByteStream<CharT> &stream,
const std::vector<std::byte> actual{stream.bytes()};
std::vector<std::byte> expected{};
std::transform(bytes.begin(), bytes.end(), std::back_inserter(expected),
to_byte);
[](const auto character) {
return to_byte(static_cast<std::uint8_t>(character));
});
EXPECT_EQ(actual, expected);
}

Expand Down

0 comments on commit 57ea0d2

Please sign in to comment.