authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-01 14:15:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-02 10:35:44+02:00
logb5badd112288b528d75085d82d5f7f1b109e87dc
treeb4f9cd29b0464457a4fa62fb4ba369e8da3938b1
parent64bd134818b77ba56deb613112ec8cdb0c967703

Fix memory corruption in Dir.Iterator test


1 files changed, 10 insertions(+), 4 deletions(-)

lib/std/fs/test.zig+10-4
...@@ -5,6 +5,7 @@ const fs = std.fs;...@@ -5,6 +5,7 @@ const fs = std.fs;
5const mem = std.mem;5const mem = std.mem;
6const wasi = std.os.wasi;6const wasi = std.os.wasi;
77
8const ArenaAllocator = std.heap.ArenaAllocator;
8const Dir = std.fs.Dir;9const Dir = std.fs.Dir;
9const File = std.fs.File;10const File = std.fs.File;
10const tmpDir = testing.tmpDir;11const tmpDir = testing.tmpDir;
...@@ -19,13 +20,18 @@ test "Dir.Iterator" {...@@ -19,13 +20,18 @@ test "Dir.Iterator" {
1920
20 try tmp_dir.dir.makeDir("some_dir");21 try tmp_dir.dir.makeDir("some_dir");
2122
23 var arena = ArenaAllocator.init(testing.allocator);
24 defer arena.deinit();
25
26 var entries = std.ArrayList(Dir.Entry).init(&arena.allocator);
27
22 // Create iterator.28 // Create iterator.
23 var iter = tmp_dir.dir.iterate();29 var iter = tmp_dir.dir.iterate();
24 var entries = std.ArrayList(Dir.Entry).init(testing.allocator);
25 defer entries.deinit();
26
27 while (try iter.next()) |entry| {30 while (try iter.next()) |entry| {
28 try entries.append(entry);31 // We cannot just store `entry` as on Windows, we're re-using the name buffer
32 // which means we'll actually share the `name` pointer between entries!
33 const name = try mem.dupe(&arena.allocator, u8, entry.name);
34 try entries.append(Dir.Entry{ .name = name, .kind = entry.kind });
29 }35 }
3036
31 testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'37 testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'