Fix scoreboard division overflow for minimum integer

Co-Authored-By: Codex gpt-6-astra <[email protected]>
This commit is contained in:
Florian ERNSTandCodex gpt-6-astra committed 2026-11-02 13:37:31 +01:00
1 parent 9eb0e230e3
commit 29a273d96e
2 files changed
+23 -2

No files matched your search

+2 -2
View File
@@ -776,7 +776,7 @@ fn players(id: &str, ctx: &Context, env: &mut dyn Env) -> CmdResult<i32> {
}
}
/// `OperationArgument`: the new (target, source) values.
/// `OperationArgument.getSimpleOperation`: the new (target, source) values.
pub fn apply_operation(op: &str, a: i32, b: i32) -> CmdResult<(i32, i32)> {
let div0 = || err("arguments.operation.div0", &[]);
Ok(match op {
@@ -789,7 +789,7 @@ pub fn apply_operation(op: &str, a: i32, b: i32) -> CmdResult<(i32, i32)> {
return Err(div0());
}
(
a.wrapping_div(b) - i32::from((a % b != 0) && ((a < 0) != (b < 0))),
a.wrapping_div(b) - i32::from((a.wrapping_rem(b) != 0) && ((a < 0) != (b < 0))),
b,
)
}
@@ -189,6 +189,27 @@ fn operations_use_floor_semantics() {
assert_eq!(apply_operation("*=", i32::MAX, 2).unwrap().0, -2);
}
#[test]
fn division_wraps_minimum_integer_by_negative_one() {
let mut m = mock();
run(&mut m, "scoreboard objectives add poc dummy").unwrap();
run(&mut m, "scoreboard players set target poc -2147483648").unwrap();
run(&mut m, "scoreboard players set divisor poc -1").unwrap();
run(
&mut m,
"scoreboard players operation target poc /= divisor poc",
)
.unwrap();
assert_eq!(
run(&mut m, "scoreboard players get target poc").unwrap(),
i32::MIN
);
assert_eq!(
run(&mut m, "scoreboard players get divisor poc").unwrap(),
-1
);
}
#[test]
fn triggers() {
let mut m = mock();