Skip to content

Commit fa09871

Browse files
author
MarcoFalke
committed
refactor: Avoid sign-compare compiler warning in util/asmap
This reverts commit eac6a30 ("refactor: Rework asmap Interpret to avoid ptrdiff_t"), because it is UB to form a past-the-end iterator, even if it is never dereferenced. Then fix the compiler warning in a different way: Instead of comparing an uint32_t against a signed ptrdiff_t, just promote both to a type that can represent both types. Even though in this case the ptrdiff_t should never hold a negative value, the overhead from promotion should be negligible.
1 parent fb66dbe commit fa09871

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/util/asmap.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
9393
jump = DecodeJump(pos, endpos);
9494
if (jump == INVALID) break; // Jump offset straddles EOF
9595
if (bits == 0) break; // No input bits left
96-
if (pos + jump < pos) break; // overflow
97-
if (pos + jump >= endpos) break; // Jumping past EOF
96+
if (int64_t{jump} >= int64_t{endpos - pos}) break; // Jumping past EOF
9897
if (ip[ip.size() - bits]) {
9998
pos += jump;
10099
}
@@ -156,8 +155,7 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
156155
} else if (opcode == Instruction::JUMP) {
157156
uint32_t jump = DecodeJump(pos, endpos);
158157
if (jump == INVALID) return false; // Jump offset straddles EOF
159-
if (pos + jump < pos) return false; // overflow
160-
if (pos + jump > endpos) return false; // Jump out of range
158+
if (int64_t{jump} > int64_t{endpos - pos}) return false; // Jump out of range
161159
if (bits == 0) return false; // Consuming bits past the end of the input
162160
--bits;
163161
uint32_t jump_offset = pos - begin + jump;

0 commit comments

Comments
 (0)