Skip to content

Commit 3b70f57

Browse files
committed
Fix UB shifts
1 parent 192314f commit 3b70f57

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

proto.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ static inline constexpr uint64_t to_varint(uint64_t value) {
3434
uint64_t result = 0;
3535
uint64_t shift = 0;
3636
while (value > 0x7f) {
37-
result |= (static_cast<uint8_t>(value) | varint_continuation) << shift;
37+
result |= static_cast<uint64_t>(static_cast<uint8_t>(value) | varint_continuation) << shift;
3838
value >>= 7;
3939
shift += 8;
4040
}
41-
result |= static_cast<uint8_t>(value) << shift;
41+
result |= static_cast<uint64_t>(value) << shift;
4242
return result;
4343
}
4444

@@ -57,7 +57,7 @@ static inline size_t read_varint(uint64_t& result, const uint8_t* data, size_t s
5757
uint64_t shift = 0;
5858
for (size_t i = 1; i <= size; ++i) {
5959
uint8_t b = *data++;
60-
result |= (b & 0x7f) << shift;
60+
result |= static_cast<uint64_t>(b & 0x7f) << shift;
6161
shift += 7;
6262
if ((b & 0x80) == 0) return i;
6363
}

0 commit comments

Comments
 (0)