terminal: addWithOverflow to detect max int

This commit is contained in:
Mitchell Hashimoto
2023-10-26 09:50:29 -07:00
parent 2fd2693693
commit 99591f280b

View File

@ -373,15 +373,16 @@ fn doAction(self: *Parser, action: TransitionAction, c: u8) ?Action {
break :param null;
}
// Ignore parameters that are too long
if (self.param_acc_idx == std.math.maxInt(u8)) break :param null;
// A numeric value. Add it to our accumulator.
if (self.param_acc_idx > 0) {
self.param_acc *|= 10;
}
self.param_acc +|= c - '0';
self.param_acc_idx += 1;
// Increment our accumulator index. If we overflow then
// we're out of bounds and we exit immediately.
self.param_acc_idx, const overflow = @addWithOverflow(self.param_acc_idx, 1);
if (overflow > 0) break :param null;
// The client is expected to perform no action.
break :param null;
@ -910,3 +911,22 @@ test "csi followed by utf8" {
try testing.expect(a[2] == null);
}
}
test "csi: too many params" {
var p = init();
_ = p.next(0x1B);
_ = p.next('[');
for (0..100) |_| {
_ = p.next('1');
_ = p.next(';');
}
_ = p.next('1');
{
const a = p.next('C');
try testing.expect(p.state == .ground);
try testing.expect(a[0] == null);
try testing.expect(a[1] == null);
try testing.expect(a[2] == null);
}
}