From 9c53564af1e00486218a5d345744273ad242ea33 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Mon, 28 Dec 2020 17:53:52 +0100 Subject: [PATCH] fs: fix segfault in Walker.next `top` might point to invalid memory after a call to self.stack.append which causes a segfault. Prevent this by recomputing `top`. Fixes #7560 --- lib/std/fs.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 6880940c03..026bf21c5c 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -2183,7 +2183,7 @@ pub const Walker = struct { while (true) { if (self.stack.items.len == 0) return null; // `top` becomes invalid after appending to `self.stack`. - const top = &self.stack.items[self.stack.items.len - 1]; + var top = &self.stack.items[self.stack.items.len - 1]; const dirname_len = top.dirname_len; if (try top.dir_it.next()) |base| { self.name_buffer.shrink(dirname_len); @@ -2200,6 +2200,7 @@ pub const Walker = struct { .dir_it = new_dir.iterate(), .dirname_len = self.name_buffer.items.len, }); + top = &self.stack.items[self.stack.items.len - 1]; } } return Entry{