Commit 81d824bf80

Jakub Konka <kubkon@jakubkonka.com>
2020-05-05 17:23:49
Clear PreopenList on every populate call
1 parent 07a968b
Changed files (1)
lib
std
lib/std/fs/wasi.zig
@@ -71,7 +71,16 @@ pub const PreopenList = struct {
 
     /// Populate the list with the preopens by issuing `std.os.wasi.fd_prestat_get`
     /// and `std.os.wasi.fd_prestat_dir_name` syscalls to the runtime.
+    ///
+    /// If called more than once, it will clear its contents every time before
+    /// issuing the syscalls.
     pub fn populate(self: *Self) Error!void {
+        // Clear contents if we're being called again
+        for (self.toOwnedSlice()) |preopen| {
+            switch (preopen.@"type") {
+                PreopenType.Dir => |path| self.buffer.allocator.free(path),
+            }
+        }
         errdefer self.deinit();
         var fd: fd_t = 3; // start fd has to be beyond stdio fds
 
@@ -123,6 +132,11 @@ pub const PreopenList = struct {
     pub fn asSlice(self: *const Self) []const Preopen {
         return self.buffer.items;
     }
+
+    /// The caller owns the returned memory. ArrayList becomes empty.
+    pub fn toOwnedSlice(self: *Self) []Preopen {
+        return self.buffer.toOwnedSlice();
+    }
 };
 
 /// Convenience wrapper for `std.os.wasi.path_open` syscall.