summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/normal.zig13
1 files changed, 13 insertions, 0 deletions
diff --git a/core/normal.zig b/core/normal.zig
index 66df9608..ef7618b8 100644
--- a/core/normal.zig
+++ b/core/normal.zig
@@ -143,6 +143,7 @@ pub const SearchRegex = struct {
pub fn compile(arena: std.mem.Allocator, pattern: []const u8) !SearchRegex {
if (std.mem.indexOf(u8, pattern, "\\b") == null) {
const branches = try arena.alloc(Branch, 1);
+ errdefer arena.free(branches);
branches[0] = try branch(pattern);
return .{ .branches = branches };
}
@@ -1632,3 +1633,15 @@ test "tree-sitter textobject keys do in Python, JSON and Zig what hx 25.07.1 doe
};
}
}
+
+test "SearchRegex: a pattern that does not compile leaves nothing allocated" {
+ // Places compiles with the pane's own allocator, not an arena: a typed
+ // pattern that is no expression yet must not leak its one branch.
+ var refused: usize = 0;
+ for ([_][]const u8{ "(", "[", "*a", "\\", "\\bfoo(" }) |bad| {
+ if (SearchRegex.compile(std.testing.allocator, bad)) |re| {
+ std.testing.allocator.free(re.branches);
+ } else |_| refused += 1;
+ }
+ try std.testing.expectEqual(5, refused);
+}