Skip to content

Commit

Permalink
AVRO-2717: Fix UB in ZigZag encoding (pre C++-20) (#2744)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkmkme authored Feb 19, 2024
1 parent 2767cdb commit 071db19
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lang/c++/api/Zigzag.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@
namespace avro {

AVRO_DECL constexpr uint64_t encodeZigzag64(int64_t input) noexcept {
// cppcheck-suppress shiftTooManyBitsSigned
return ((input << 1) ^ (input >> 63));
return ((static_cast<uint64_t>(input) << 1) ^ (input >> 63));
}
AVRO_DECL constexpr int64_t decodeZigzag64(uint64_t input) noexcept {
return static_cast<int64_t>(((input >> 1) ^ -(static_cast<int64_t>(input) & 1)));
}

AVRO_DECL constexpr uint32_t encodeZigzag32(int32_t input) noexcept {
// cppcheck-suppress shiftTooManyBitsSigned
return ((input << 1) ^ (input >> 31));
return (static_cast<uint32_t>(input) << 1) ^ (input >> 31);
}
AVRO_DECL constexpr int32_t decodeZigzag32(uint32_t input) noexcept {
return static_cast<int32_t>(((input >> 1) ^ -(static_cast<int64_t>(input) & 1)));
Expand Down

0 comments on commit 071db19

Please sign in to comment.