Skip to content

Commit 2b3dbfa

Browse files
committed
Deal with decoding failures explicitly in asmap Interpret
1 parent 1479007 commit 2b3dbfa

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/util/asmap.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace {
1010

11+
constexpr uint32_t INVALID = 0xFFFFFFFF;
12+
1113
uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes)
1214
{
1315
uint32_t val = minval;
@@ -25,15 +27,15 @@ uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector
2527
val += (1 << *bit_sizes_it);
2628
} else {
2729
for (int b = 0; b < *bit_sizes_it; b++) {
28-
if (bitpos == endpos) break;
30+
if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
2931
bit = *bitpos;
3032
bitpos++;
3133
val += bit << (*bit_sizes_it - 1 - b);
3234
}
3335
return val;
3436
}
3537
}
36-
return -1;
38+
return INVALID; // Reached EOF in exponent
3739
}
3840

3941
enum class Instruction : uint32_t
@@ -83,9 +85,12 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
8385
while (pos != endpos) {
8486
opcode = DecodeType(pos, endpos);
8587
if (opcode == Instruction::RETURN) {
86-
return DecodeASN(pos, endpos);
88+
default_asn = DecodeASN(pos, endpos);
89+
if (default_asn == INVALID) break; // ASN straddles EOF
90+
return default_asn;
8791
} else if (opcode == Instruction::JUMP) {
8892
jump = DecodeJump(pos, endpos);
93+
if (jump == INVALID) break; // Jump offset straddles EOF
8994
if (bits == 0) break;
9095
if (ip[ip.size() - bits]) {
9196
if (jump >= endpos - pos) break;
@@ -94,6 +99,7 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
9499
bits--;
95100
} else if (opcode == Instruction::MATCH) {
96101
match = DecodeMatch(pos, endpos);
102+
if (match == INVALID) break; // Match bits straddle EOF
97103
matchlen = CountBits(match) - 1;
98104
for (uint32_t bit = 0; bit < matchlen; bit++) {
99105
if (bits == 0) break;
@@ -104,8 +110,9 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
104110
}
105111
} else if (opcode == Instruction::DEFAULT) {
106112
default_asn = DecodeASN(pos, endpos);
113+
if (default_asn == INVALID) break; // ASN straddles EOF
107114
} else {
108-
break;
115+
break; // Instruction straddles EOF
109116
}
110117
}
111118
return 0; // 0 is not a valid ASN

0 commit comments

Comments
 (0)