From 99591f280b3ca68946d894a9380871e9eb65c6d4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Oct 2023 09:50:29 -0700 Subject: [PATCH] terminal: addWithOverflow to detect max int --- src/terminal/Parser.zig | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/terminal/Parser.zig b/src/terminal/Parser.zig index 186b99608..785fd938e 100644 --- a/src/terminal/Parser.zig +++ b/src/terminal/Parser.zig @@ -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); + } +}