authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-16 16:37:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-16 16:41:22-07:00
log233a0d399135cdb581c5c080726c4cacc71695fe
treeef09ca619a4430877414cc605525e648fe81cdeb
parentae119a9a8d5b8dec21bd314e91afcec122eb8631

CLI: remove cleanup logic for stdin temp file

In one of the happy paths, execve() is used to switch to clang in which case any cleanup logic that exists for this temporary file will not run and this temp file will be leaked. Oh well. It's a minor punishment for using `-x c` which nobody should be doing. Therefore, we make no effort to clean up. Using `-` for stdin as a source file always leaks a temp file. Note that the standard `zig build-exe` CLI does not support stdin as an input file. This is only for `zig cc` C compiler compatibility.

1 files changed, 16 insertions(+), 33 deletions(-)

src/main.zig+16-33
...@@ -705,17 +705,6 @@ const ArgsIterator = struct {...@@ -705,17 +705,6 @@ const ArgsIterator = struct {
705 }705 }
706};706};
707707
708fn cleanupTempStdinFile(
709 temp_stdin_file: ?[]const u8,
710 local_cache_directory: Compilation.Directory,
711) void {
712 if (temp_stdin_file) |file| {
713 // Some garbage may stay in the file system if removal fails; this
714 // is harmless so no warning is needed.
715 local_cache_directory.handle.deleteFile(file) catch {};
716 }
717}
718
719fn buildOutputType(708fn buildOutputType(
720 gpa: Allocator,709 gpa: Allocator,
721 arena: Allocator,710 arena: Allocator,
...@@ -3031,13 +3020,6 @@ fn buildOutputType(...@@ -3031,13 +3020,6 @@ fn buildOutputType(
3031 break :l global_cache_directory;3020 break :l global_cache_directory;
3032 };3021 };
30333022
3034 var temp_stdin_file: ?[]const u8 = null;
3035 // Note that in one of the happy paths, execve() is used to switch to clang
3036 // in which case this cleanup logic does not run and this temp file is
3037 // leaked. Oh well. It's a minor punishment for using `-x c` which nobody
3038 // should be doing.
3039 defer cleanupTempStdinFile(temp_stdin_file, local_cache_directory);
3040
3041 for (c_source_files.items) |*src| {3023 for (c_source_files.items) |*src| {
3042 if (!mem.eql(u8, src.src_path, "-")) continue;3024 if (!mem.eql(u8, src.src_path, "-")) continue;
30433025
...@@ -3045,21 +3027,22 @@ fn buildOutputType(...@@ -3045,21 +3027,22 @@ fn buildOutputType(
3045 fatal("-E or -x is required when reading from a non-regular file", .{});3027 fatal("-E or -x is required when reading from a non-regular file", .{});
30463028
3047 // "-" is stdin. Dump it to a real file.3029 // "-" is stdin. Dump it to a real file.
3048 const sub_path = blk: {3030 const sep = fs.path.sep_str;
3049 const sep = fs.path.sep_str;3031 const sub_path = try std.fmt.allocPrint(arena, "tmp" ++ sep ++ "{x}-stdin{s}", .{
3050 const sub_path = try std.fmt.allocPrint(arena, "tmp" ++ sep ++ "{x}-stdin{s}", .{3032 std.crypto.random.int(u64), ext.canonicalName(target_info.target),
3051 std.crypto.random.int(u64), ext.canonicalName(target_info.target),3033 });
3052 });3034 try local_cache_directory.handle.makePath("tmp");
3053 try local_cache_directory.handle.makePath("tmp");3035 // Note that in one of the happy paths, execve() is used to switch
3054 var f = try local_cache_directory.handle.createFile(sub_path, .{});3036 // to clang in which case any cleanup logic that exists for this
3055 defer f.close();3037 // temporary file will not run and this temp file will be leaked.
3056 errdefer local_cache_directory.handle.deleteFile(sub_path) catch {};3038 // Oh well. It's a minor punishment for using `-x c` which nobody
3057 try f.writeFileAll(io.getStdIn(), .{});3039 // should be doing. Therefore, we make no effort to clean up. Using
3058 break :blk sub_path;3040 // `-` for stdin as a source file always leaks a temp file.
3059 };3041 var f = try local_cache_directory.handle.createFile(sub_path, .{});
3060 // Relative to `local_cache_directory`.3042 defer f.close();
3061 temp_stdin_file = sub_path;3043 try f.writeFileAll(io.getStdIn(), .{});
3062 // Relative to current working directory.3044
3045 // Convert `sub_path` to be relative to current working directory.
3063 src.src_path = try local_cache_directory.join(arena, &.{sub_path});3046 src.src_path = try local_cache_directory.join(arena, &.{sub_path});
3064 }3047 }
30653048