xonecas.io
79113
Ma

mini-coding-agent / commit de76c76

fix(sanitize): fix core dump

xonecas2026-10-07 de76c765fcf150a12567d551b1a23b5131987d5a parent b8640d4

2 files changed

notes.md+0−2
@@ -6,8 +6,6 @@ Some of my notes might be because of lack of Zig experience.
66
77 ## Notes
88
9-**highlighting updates** - Let's update the code so we use tree-sitter to highlight the diffs, instead of the manual process we do now. We are also going to start highlighting the committed bash calls arguments (not on the preview).
10-
119 `src/config.zig` - When we fixed the issue with the model specific api being at provider level for opencode in a recent commit, apparently we didn't do it for custom providers. Again, the models need to carry the metadata, the provider is just a label grouping.
1210
1311 ```jsonc
src/tui/render.zig+24−12
@@ -52,6 +52,15 @@ fn isCombining(code: u21) bool {
5252
5353 const Piece = struct { text: []const u8, width: usize };
5454
55+const Decoded = struct { len: usize, cp: u21 };
56+
57+fn decodeAt(text: []const u8, i: usize) ?Decoded {
58+ const n = std.unicode.utf8ByteSequenceLength(text[i]) catch return null;
59+ if (i + n > text.len) return null;
60+ const cp = std.unicode.utf8Decode(text[i .. i + n]) catch return null;
61+ return .{ .len = n, .cp = cp };
62+}
63+
5564 fn nextPiece(text: []const u8, i: *usize) ?Piece {
5665 if (i.* >= text.len) return null;
5766 if (isSgrAt(text, i.*)) |n| {
@@ -59,11 +68,13 @@ fn nextPiece(text: []const u8, i: *usize) ?Piece {
5968 i.* += n;
6069 return piece;
6170 }
62- const n = std.unicode.utf8ByteSequenceLength(text[i.*]) catch 1;
63- const len = @min(@as(usize, n), text.len - i.*);
64- const cp = std.unicode.utf8Decode(text[i.* .. i.* + len]) catch text[i.*];
65- const piece = Piece{ .text = text[i.* .. i.* + len], .width = if (cp == '\t') 0 else charWidth(cp) };
66- i.* += len;
71+ if (decodeAt(text, i.*)) |d| {
72+ const piece = Piece{ .text = text[i.* .. i.* + d.len], .width = if (d.cp == '\t') 0 else charWidth(d.cp) };
73+ i.* += d.len;
74+ return piece;
75+ }
76+ const piece = Piece{ .text = text[i.* .. i.* + 1], .width = charWidth(text[i.*]) };
77+ i.* += 1;
6778 return piece;
6879 }
6980
@@ -113,8 +124,6 @@ pub fn sanitize(a: std.mem.Allocator, text: []const u8) []const u8 {
113124 i += 1;
114125 continue;
115126 }
116- const seq_len = std.unicode.utf8ByteSequenceLength(c) catch 1;
117- const len = @min(@as(usize, seq_len), text.len - i);
118127 if (c < 0x80) {
119128 if ((c < 0x20 and c != 0x09 and c != 0x0a) or c == 0x7f) {
120129 i += 1;
@@ -124,13 +133,16 @@ pub fn sanitize(a: std.mem.Allocator, text: []const u8) []const u8 {
124133 i += 1;
125134 continue;
126135 }
127- const cp = std.unicode.utf8Decode(text[i .. i + len]) catch 0;
128- if (cp >= 0x80 and cp <= 0x9f) {
129- i += len;
136+ const d = decodeAt(text, i) orelse {
137+ i += 1;
138+ continue;
139+ };
140+ if (d.cp >= 0x80 and d.cp <= 0x9f) {
141+ i += d.len;
130142 continue;
131143 }
132- out.appendSlice(a, text[i .. i + len]) catch {};
133- i += len;
144+ out.appendSlice(a, text[i .. i + d.len]) catch {};
145+ i += d.len;
134146 }
135147 return out.items;
136148 }