Skip to content

Commit 7845c6e

Browse files
committed
coverage: Avoid overflow when the MC/DC condition limit is exceeded
If we perform this subtraction and then add 1, the subtraction can sometimes overflow to -1 before the addition can bring its value back to 0. That behaviour seems to be benign, but it nevertheless causes test failures in compiler configurations that check for overflow. We can avoid the overflow by instead subtracting (N - 1), which is algebraically equivalent, and more closely matches what the code is actually trying to do.
1 parent da159eb commit 7845c6e

File tree

1 file changed

+3
-2
lines changed
  • compiler/rustc_mir_build/src/build/coverageinfo

1 file changed

+3
-2
lines changed

compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,13 @@ impl MCDCInfoBuilder {
217217
}
218218
_ => {
219219
// Do not generate mcdc mappings and statements for decisions with too many conditions.
220-
let rebase_idx = self.branch_spans.len() - decision.conditions_num + 1;
220+
// Therefore, first erase the condition info of the (N-1) previous branch spans.
221+
let rebase_idx = self.branch_spans.len() - (decision.conditions_num - 1);
221222
for branch in &mut self.branch_spans[rebase_idx..] {
222223
branch.condition_info = None;
223224
}
224225

225-
// ConditionInfo of this branch shall also be reset.
226+
// Then, erase this last branch span's info too, for a total of N.
226227
condition_info = None;
227228

228229
tcx.dcx().emit_warn(MCDCExceedsConditionNumLimit {

0 commit comments

Comments
 (0)