Skip to content

Commit 2d506d2

Browse files
committed
File.close invalidates
1 parent 57e1f6a commit 2d506d2

26 files changed

+86
-85
lines changed

lib/std/Thread.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
8080
var buf: [32]u8 = undefined;
8181
const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
8282

83-
const file = try std.fs.cwd().openFile(path, .{ .write = true });
83+
var file = try std.fs.cwd().openFile(path, .{ .write = true });
8484
defer file.close();
8585

8686
try file.writer().writeAll(name);
@@ -168,7 +168,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
168168
var buf: [32]u8 = undefined;
169169
const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
170170

171-
const file = try std.fs.cwd().openFile(path, .{});
171+
var file = try std.fs.cwd().openFile(path, .{});
172172
defer file.close();
173173

174174
const data_len = try file.reader().readAll(buffer_ptr[0 .. max_name_len + 1]);

lib/std/build.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ fn findVcpkgRoot(allocator: *Allocator) !?[]const u8 {
31293129
const path_file = try fs.path.join(allocator, &[_][]const u8{ appdata_path, "vcpkg.path.txt" });
31303130
defer allocator.free(path_file);
31313131

3132-
const file = fs.cwd().openFile(path_file, .{}) catch return null;
3132+
var file = fs.cwd().openFile(path_file, .{}) catch return null;
31333133
defer file.close();
31343134

31353135
const size = @intCast(usize, try file.getEndPos());

lib/std/debug.zig

+10-10
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,12 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
690690
/// it themselves, even on error.
691691
/// TODO resources https://github.com/ziglang/zig/issues/4353
692692
/// TODO it's weird to take ownership even on error, rework this code.
693-
fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: File) !ModuleDebugInfo {
693+
fn readCoffDebugInfo(allocator: *mem.Allocator, coff_file: *File) !ModuleDebugInfo {
694694
nosuspend {
695695
errdefer coff_file.close();
696696

697697
const coff_obj = try allocator.create(coff.Coff);
698-
coff_obj.* = coff.Coff.init(allocator, coff_file);
698+
coff_obj.* = coff.Coff.init(allocator, coff_file.*);
699699

700700
var di = ModuleDebugInfo{
701701
.base_address = undefined,
@@ -757,7 +757,7 @@ fn chopSlice(ptr: []const u8, offset: u64, size: u64) ![]const u8 {
757757
/// it themselves, even on error.
758758
/// TODO resources https://github.com/ziglang/zig/issues/4353
759759
/// TODO it's weird to take ownership even on error, rework this code.
760-
pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugInfo {
760+
pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: *File) !ModuleDebugInfo {
761761
nosuspend {
762762
const mapped_mem = try mapWholeFile(elf_file);
763763
const hdr = @ptrCast(*const elf.Ehdr, &mapped_mem[0]);
@@ -829,7 +829,7 @@ pub fn readElfDebugInfo(allocator: *mem.Allocator, elf_file: File) !ModuleDebugI
829829
/// This takes ownership of macho_file: users of this function should not close
830830
/// it themselves, even on error.
831831
/// TODO it's weird to take ownership even on error, rework this code.
832-
fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: File) !ModuleDebugInfo {
832+
fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: *File) !ModuleDebugInfo {
833833
const mapped_mem = try mapWholeFile(macho_file);
834834

835835
const hdr = @ptrCast(
@@ -966,7 +966,7 @@ const MachoSymbol = struct {
966966
/// `file` is expected to have been opened with .intended_io_mode == .blocking.
967967
/// Takes ownership of file, even on error.
968968
/// TODO it's weird to take ownership even on error, rework this code.
969-
fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
969+
fn mapWholeFile(file: *File) ![]align(mem.page_size) const u8 {
970970
nosuspend {
971971
defer file.close();
972972

@@ -1135,11 +1135,11 @@ pub const DebugInfo = struct {
11351135
const obj_di = try self.allocator.create(ModuleDebugInfo);
11361136
errdefer self.allocator.destroy(obj_di);
11371137

1138-
const coff_file = fs.openFileAbsoluteW(name_buffer[0 .. len + 4 :0], .{}) catch |err| switch (err) {
1138+
var coff_file = fs.openFileAbsoluteW(name_buffer[0 .. len + 4 :0], .{}) catch |err| switch (err) {
11391139
error.FileNotFound => return error.MissingDebugInfo,
11401140
else => return err,
11411141
};
1142-
obj_di.* = try readCoffDebugInfo(self.allocator, coff_file);
1142+
obj_di.* = try readCoffDebugInfo(self.allocator, &coff_file);
11431143
obj_di.base_address = seg_start;
11441144

11451145
try self.address_map.putNoClobber(seg_start, obj_di);
@@ -1205,12 +1205,12 @@ pub const DebugInfo = struct {
12051205
else
12061206
fs.openSelfExe(.{ .intended_io_mode = .blocking });
12071207

1208-
const elf_file = copy catch |err| switch (err) {
1208+
var elf_file = copy catch |err| switch (err) {
12091209
error.FileNotFound => return error.MissingDebugInfo,
12101210
else => return err,
12111211
};
12121212

1213-
obj_di.* = try readElfDebugInfo(self.allocator, elf_file);
1213+
obj_di.* = try readElfDebugInfo(self.allocator, &elf_file);
12141214
obj_di.base_address = ctx.base_address;
12151215

12161216
try self.address_map.putNoClobber(ctx.base_address, obj_di);
@@ -1240,7 +1240,7 @@ pub const ModuleDebugInfo = switch (native_os) {
12401240
}
12411241

12421242
fn loadOFile(self: *@This(), o_file_path: []const u8) !DW.DwarfInfo {
1243-
const o_file = try fs.cwd().openFile(o_file_path, .{ .intended_io_mode = .blocking });
1243+
var o_file = try fs.cwd().openFile(o_file_path, .{ .intended_io_mode = .blocking });
12441244
const mapped_mem = try mapWholeFile(o_file);
12451245

12461246
const hdr = @ptrCast(

lib/std/fs/file.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ pub const File = struct {
179179

180180
/// Upon success, the stream is in an uninitialized state. To continue using it,
181181
/// you must use the open() function.
182-
pub fn close(self: File) void {
182+
pub fn close(self: *File) void {
183183
if (is_windows) {
184184
windows.CloseHandle(self.handle);
185185
} else if (self.capable_io_mode != self.intended_io_mode) {
186186
std.event.Loop.instance.?.close(self.handle);
187187
} else {
188188
os.close(self.handle);
189189
}
190+
self.* = undefined;
190191
}
191192

192193
/// Test whether the file refers to a terminal.

lib/std/fs/test.zig

+19-19
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ test "Dir.Iterator" {
151151
defer tmp_dir.cleanup();
152152

153153
// First, create a couple of entries to iterate over.
154-
const file = try tmp_dir.dir.createFile("some_file", .{});
154+
var file = try tmp_dir.dir.createFile("some_file", .{});
155155
file.close();
156156

157157
try tmp_dir.dir.makeDir("some_dir");
@@ -523,7 +523,7 @@ test "renameAbsolute" {
523523
test "openSelfExe" {
524524
if (builtin.os.tag == .wasi) return error.SkipZigTest;
525525

526-
const self_exe_file = try std.fs.openSelfExe(.{});
526+
var self_exe_file = try std.fs.openSelfExe(.{});
527527
self_exe_file.close();
528528
}
529529

@@ -803,10 +803,10 @@ test "open file with exclusive nonblocking lock twice" {
803803
var tmp = tmpDir(.{});
804804
defer tmp.cleanup();
805805

806-
const file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
806+
var file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
807807
defer file1.close();
808808

809-
const file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
809+
var file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
810810
try testing.expectError(error.WouldBlock, file2);
811811
}
812812

@@ -818,10 +818,10 @@ test "open file with shared and exclusive nonblocking lock" {
818818
var tmp = tmpDir(.{});
819819
defer tmp.cleanup();
820820

821-
const file1 = try tmp.dir.createFile(filename, .{ .lock = .Shared, .lock_nonblocking = true });
821+
var file1 = try tmp.dir.createFile(filename, .{ .lock = .Shared, .lock_nonblocking = true });
822822
defer file1.close();
823823

824-
const file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
824+
var file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
825825
try testing.expectError(error.WouldBlock, file2);
826826
}
827827

@@ -833,10 +833,10 @@ test "open file with exclusive and shared nonblocking lock" {
833833
var tmp = tmpDir(.{});
834834
defer tmp.cleanup();
835835

836-
const file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
836+
var file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
837837
defer file1.close();
838838

839-
const file2 = tmp.dir.createFile(filename, .{ .lock = .Shared, .lock_nonblocking = true });
839+
var file2 = tmp.dir.createFile(filename, .{ .lock = .Shared, .lock_nonblocking = true });
840840
try testing.expectError(error.WouldBlock, file2);
841841
}
842842

@@ -853,12 +853,12 @@ test "open file with exclusive lock twice, make sure it waits" {
853853
var tmp = tmpDir(.{});
854854
defer tmp.cleanup();
855855

856-
const file = try tmp.dir.createFile(filename, .{ .lock = .Exclusive });
856+
var file = try tmp.dir.createFile(filename, .{ .lock = .Exclusive });
857857
errdefer file.close();
858858

859859
const S = struct {
860860
fn checkFn(dir: *fs.Dir, evt: *std.Thread.ResetEvent) !void {
861-
const file1 = try dir.createFile(filename, .{ .lock = .Exclusive });
861+
var file1 = try dir.createFile(filename, .{ .lock = .Exclusive });
862862
defer file1.close();
863863
evt.set();
864864
}
@@ -892,9 +892,9 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
892892
const filename = try fs.path.resolve(allocator, &file_paths);
893893
defer allocator.free(filename);
894894

895-
const file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
895+
var file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
896896

897-
const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
897+
var file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
898898
file1.close();
899899
try testing.expectError(error.WouldBlock, file2);
900900

@@ -962,13 +962,13 @@ test ". and .. in fs.Dir functions" {
962962
var created_subdir = try tmp.dir.openDir("./subdir", .{});
963963
created_subdir.close();
964964

965-
const created_file = try tmp.dir.createFile("./subdir/../file", .{});
965+
var created_file = try tmp.dir.createFile("./subdir/../file", .{});
966966
created_file.close();
967967
try tmp.dir.access("./subdir/../file", .{});
968968

969969
try tmp.dir.copyFile("./subdir/../file", tmp.dir, "./subdir/../copy", .{});
970970
try tmp.dir.rename("./subdir/../copy", "./subdir/../rename");
971-
const renamed_file = try tmp.dir.openFile("./subdir/../rename", .{});
971+
var renamed_file = try tmp.dir.openFile("./subdir/../rename", .{});
972972
renamed_file.close();
973973
try tmp.dir.deleteFile("./subdir/../rename");
974974

@@ -1001,20 +1001,20 @@ test ". and .. in absolute functions" {
10011001
created_subdir.close();
10021002

10031003
const created_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../file" });
1004-
const created_file = try fs.createFileAbsolute(created_file_path, .{});
1004+
var created_file = try fs.createFileAbsolute(created_file_path, .{});
10051005
created_file.close();
10061006
try fs.accessAbsolute(created_file_path, .{});
10071007

10081008
const copied_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../copy" });
10091009
try fs.copyFileAbsolute(created_file_path, copied_file_path, .{});
10101010
const renamed_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../rename" });
10111011
try fs.renameAbsolute(copied_file_path, renamed_file_path);
1012-
const renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});
1012+
var renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});
10131013
renamed_file.close();
10141014
try fs.deleteFileAbsolute(renamed_file_path);
10151015

10161016
const update_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../update" });
1017-
const update_file = try fs.createFileAbsolute(update_file_path, .{});
1017+
var update_file = try fs.createFileAbsolute(update_file_path, .{});
10181018
try update_file.writeAll("something");
10191019
update_file.close();
10201020
const prev_status = try fs.updateFileAbsolute(created_file_path, update_file_path, .{});
@@ -1030,7 +1030,7 @@ test "chmod" {
10301030
var tmp = tmpDir(.{});
10311031
defer tmp.cleanup();
10321032

1033-
const file = try tmp.dir.createFile("test_file", .{ .mode = 0o600 });
1033+
var file = try tmp.dir.createFile("test_file", .{ .mode = 0o600 });
10341034
defer file.close();
10351035
try testing.expect((try file.stat()).mode & 0o7777 == 0o600);
10361036

@@ -1052,7 +1052,7 @@ test "chown" {
10521052
var tmp = tmpDir(.{});
10531053
defer tmp.cleanup();
10541054

1055-
const file = try tmp.dir.createFile("test_file", .{});
1055+
var file = try tmp.dir.createFile("test_file", .{});
10561056
defer file.close();
10571057
try file.chown(null, null);
10581058

lib/std/net.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ fn linuxLookupNameFromHosts(
11141114
family: os.sa_family_t,
11151115
port: u16,
11161116
) !void {
1117-
const file = fs.openFileAbsoluteZ("/etc/hosts", .{}) catch |err| switch (err) {
1117+
var file = fs.openFileAbsoluteZ("/etc/hosts", .{}) catch |err| switch (err) {
11181118
error.FileNotFound,
11191119
error.NotDir,
11201120
error.AccessDenied,
@@ -1313,7 +1313,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {
13131313
};
13141314
errdefer rc.deinit();
13151315

1316-
const file = fs.openFileAbsoluteZ("/etc/resolv.conf", .{}) catch |err| switch (err) {
1316+
var file = fs.openFileAbsoluteZ("/etc/resolv.conf", .{}) catch |err| switch (err) {
13171317
error.FileNotFound,
13181318
error.NotDir,
13191319
error.AccessDenied,

lib/std/os/linux/io_uring.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ test "writev/fsync/readv" {
14131413
defer ring.deinit();
14141414

14151415
const path = "test_io_uring_writev_fsync_readv";
1416-
const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
1416+
var file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
14171417
defer file.close();
14181418
defer std.fs.cwd().deleteFile(path) catch {};
14191419
const fd = file.handle;
@@ -1481,7 +1481,7 @@ test "write/read" {
14811481
defer ring.deinit();
14821482

14831483
const path = "test_io_uring_write_read";
1484-
const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
1484+
var file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
14851485
defer file.close();
14861486
defer std.fs.cwd().deleteFile(path) catch {};
14871487
const fd = file.handle;
@@ -1527,7 +1527,7 @@ test "write_fixed/read_fixed" {
15271527
defer ring.deinit();
15281528

15291529
const path = "test_io_uring_write_read_fixed";
1530-
const file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
1530+
var file = try std.fs.cwd().createFile(path, .{ .read = true, .truncate = true });
15311531
defer file.close();
15321532
defer std.fs.cwd().deleteFile(path) catch {};
15331533
const fd = file.handle;
@@ -1633,7 +1633,7 @@ test "close" {
16331633
defer ring.deinit();
16341634

16351635
const path = "test_io_uring_close";
1636-
const file = try std.fs.cwd().createFile(path, .{});
1636+
var file = try std.fs.cwd().createFile(path, .{});
16371637
errdefer file.close();
16381638
defer std.fs.cwd().deleteFile(path) catch {};
16391639

@@ -1914,7 +1914,7 @@ test "fallocate" {
19141914
defer ring.deinit();
19151915

19161916
const path = "test_io_uring_fallocate";
1917-
const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
1917+
var file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
19181918
defer file.close();
19191919
defer std.fs.cwd().deleteFile(path) catch {};
19201920

@@ -1958,7 +1958,7 @@ test "statx" {
19581958
defer ring.deinit();
19591959

19601960
const path = "test_io_uring_statx";
1961-
const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
1961+
var file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
19621962
defer file.close();
19631963
defer std.fs.cwd().deleteFile(path) catch {};
19641964

lib/std/os/linux/test.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = std.fs;
99

1010
test "fallocate" {
1111
const path = "test_fallocate";
12-
const file = try fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
12+
var file = try fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
1313
defer file.close();
1414
defer fs.cwd().deleteFile(path) catch {};
1515

0 commit comments

Comments
 (0)