Skip to content

Commit

Permalink
Fix memory corruption in Dir.Iterator test
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jul 1, 2020
1 parent ba27e56 commit 9580c5a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/std/fs/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = std.fs;
const mem = std.mem;
const wasi = std.os.wasi;

const ArenaAllocator = std.heap.ArenaAllocator;
const Dir = std.fs.Dir;
const File = std.fs.File;
const tmpDir = testing.tmpDir;
Expand All @@ -19,13 +20,18 @@ test "Dir.Iterator" {

try tmp_dir.dir.makeDir("some_dir");

var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();

var entries = std.ArrayList(Dir.Entry).init(&arena.allocator);

// Create iterator.
var iter = tmp_dir.dir.iterate();
var entries = std.ArrayList(Dir.Entry).init(testing.allocator);
defer entries.deinit();

while (try iter.next()) |entry| {
try entries.append(entry);
// We cannot just store `entry` as on Windows, we're re-using the name buffer
// which means we'll actually share the `name` pointer between entries!
const name = try mem.dupe(&arena.allocator, u8, entry.name);
try entries.append(Dir.Entry{ .name = name, .kind = entry.kind });
}

testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'
Expand Down

0 comments on commit 9580c5a

Please sign in to comment.