authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-05 22:59:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
logde341b8fb86374ac62a36c987e11d1dd0b8c3358
tree4264e90d536fecf80564bc2318fe52cb4aa4ec20
parent8a77c1c637b07a2ff4f8f8981f9f732756cc38f6

Fix memory leak in cache_hash


1 files changed, 49 insertions(+), 3 deletions(-)

lib/std/cache_hash.zig+49-3
...@@ -130,9 +130,14 @@ pub const CacheHash = struct {...@@ -130,9 +130,14 @@ pub const CacheHash = struct {
130 // TODO: Open file with a file lock130 // TODO: Open file with a file lock
131 self.manifest_file = try cwd.createFile(self.manifest_file_path.?, .{ .read = true, .truncate = false });131 self.manifest_file = try cwd.createFile(self.manifest_file_path.?, .{ .read = true, .truncate = false });
132132
133 // create a buffer instead of using readAllAlloc
134 // See: https://github.com/ziglang/zig/issues/4656
135 var file_buffer = try Buffer.initCapacity(self.alloc, 16 * 1024);
136 defer file_buffer.deinit();
137
133 // TODO: Figure out a good max value?138 // TODO: Figure out a good max value?
134 const file_contents = try self.manifest_file.?.inStream().stream.readAllAlloc(self.alloc, 16 * 1024);139 try self.manifest_file.?.inStream().stream.readAllBuffer(&file_buffer, 16 * 1024);
135 defer self.alloc.free(file_contents);140 const file_contents = file_buffer.toSliceConst();
136141
137 const input_file_count = self.files.len;142 const input_file_count = self.files.len;
138 var any_file_changed = false;143 var any_file_changed = false;
...@@ -165,7 +170,6 @@ pub const CacheHash = struct {...@@ -165,7 +170,6 @@ pub const CacheHash = struct {
165 if (cache_hash_file.path != null and !mem.eql(u8, file_path, cache_hash_file.path.?)) {170 if (cache_hash_file.path != null and !mem.eql(u8, file_path, cache_hash_file.path.?)) {
166 return error.InvalidFormat;171 return error.InvalidFormat;
167 }172 }
168 cache_hash_file.path = try mem.dupe(self.alloc, u8, file_path);
169173
170 const this_file = cwd.openFile(cache_hash_file.path.?, .{ .read = true }) catch {174 const this_file = cwd.openFile(cache_hash_file.path.?, .{ .read = true }) catch {
171 self.manifest_file.?.close();175 self.manifest_file.?.close();
...@@ -300,3 +304,45 @@ fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const File) !void {...@@ -300,3 +304,45 @@ fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const File) !void {
300304
301 blake3.final(bin_digest);305 blake3.final(bin_digest);
302}306}
307
308test "cache file and the recall it" {
309 const cwd = fs.cwd();
310
311 const temp_manifest_dir = "temp_manifest_dir";
312
313 try cwd.writeFile("test.txt", "Hello, world!\n");
314
315 var digest1 = try ArrayList(u8).initCapacity(testing.allocator, 32);
316 defer digest1.deinit();
317 var digest2 = try ArrayList(u8).initCapacity(testing.allocator, 32);
318 defer digest2.deinit();
319
320 {
321 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
322 defer ch.release();
323
324 try ch.cache(@as(u16, 1234));
325 try ch.cache_buf("1234");
326 try ch.cache_file("test.txt");
327
328 // There should be nothing in the cache
329 debug.assert((try ch.hit(&digest1)) == false);
330
331 try ch.final(&digest1);
332 }
333 {
334 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
335 defer ch.release();
336
337 try ch.cache(@as(u16, 1234));
338 try ch.cache_buf("1234");
339 try ch.cache_file("test.txt");
340
341 // Cache hit! We just "built" the same file
342 debug.assert((try ch.hit(&digest2)) == true);
343 }
344
345 debug.assert(mem.eql(u8, digest1.toSlice(), digest2.toSlice()));
346
347 try cwd.deleteTree(temp_manifest_dir);
348}