Skip to content

Commit bb5b0c6

Browse files
committed
fix(db): leak on allocation failure
1 parent 7013710 commit bb5b0c6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/database.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub const DB = struct {
6565
errdefer cf_map.destroy();
6666
for (cf_list, 0..) |*cf, i| {
6767
const name = try allocator.dupe(u8, column_families[i].name);
68+
errdefer allocator.free(name);
6869
cf.* = .{
6970
.name = name,
7071
.handle = cf_handles[i].?,
@@ -352,6 +353,35 @@ pub const DBOptions = struct {
352353
}
353354
};
354355

356+
test "DB clean init and deinit" {
357+
const ns = struct {
358+
pub fn run(allocator: Allocator) !void {
359+
var dir = std.testing.tmpDir(.{});
360+
defer dir.cleanup();
361+
const path = try dir.dir.realpathAlloc(allocator, ".");
362+
defer allocator.free(path);
363+
364+
var data: ?Data = null;
365+
const db, const cfs = try DB.open(
366+
allocator,
367+
path,
368+
.{
369+
.create_if_missing = true,
370+
.create_missing_column_families = true,
371+
},
372+
null,
373+
&data,
374+
);
375+
376+
db.deinit();
377+
allocator.free(cfs);
378+
}
379+
};
380+
381+
try ns.run(std.testing.allocator);
382+
try std.testing.checkAllAllocationFailures(std.testing.allocator, ns.run, .{});
383+
}
384+
355385
test "DBOptions defaults" {
356386
try testDBOptions(DBOptions{}, rdb.rocksdb_options_create().?);
357387
}

0 commit comments

Comments
 (0)