a modern tui library written in zig

Fix InternalScreen bounds checks

authored by john xu and committed by rockorager.dev 3943a6f4 c2f00c2b

Changed files
+18 -4
src
+18 -4
src/InternalScreen.zig
··· 83 83 row: u16, 84 84 cell: Cell, 85 85 ) void { 86 - if (self.width < col) { 86 + if (col >= self.width) { 87 87 // column out of bounds 88 88 return; 89 89 } 90 - if (self.height < row) { 90 + if (row >= self.height) { 91 91 // height out of bounds 92 92 return; 93 93 } ··· 110 110 } 111 111 112 112 pub fn readCell(self: *InternalScreen, col: u16, row: u16) ?Cell { 113 - if (self.width < col) { 113 + if (col >= self.width) { 114 114 // column out of bounds 115 115 return null; 116 116 } 117 - if (self.height < row) { 117 + if (row >= self.height) { 118 118 // height out of bounds 119 119 return null; 120 120 } ··· 131 131 .default = cell.default, 132 132 }; 133 133 } 134 + 135 + test "InternalScreen: out-of-bounds read/write are ignored" { 136 + var screen = try InternalScreen.init(std.testing.allocator, 2, 2); 137 + defer screen.deinit(std.testing.allocator); 138 + 139 + const sentinel: Cell = .{ .char = .{ .grapheme = "A", .width = 1 } }; 140 + screen.writeCell(0, 1, sentinel); 141 + 142 + const oob_cell: Cell = .{ .char = .{ .grapheme = "X", .width = 1 } }; 143 + screen.writeCell(2, 0, oob_cell); 144 + const read_back = screen.readCell(0, 1) orelse return error.TestUnexpectedResult; 145 + try std.testing.expect(std.mem.eql(u8, read_back.char.grapheme, "A")); 146 + try std.testing.expect(screen.readCell(2, 0) == null); 147 + }