diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-05-16 16:37:07 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-05-16 16:41:22 -0700 |
| commit | 233a0d399135cdb581c5c080726c4cacc71695fe (patch) | |
| tree | ef09ca619a4430877414cc605525e648fe81cdeb /src | |
| parent | ae119a9a8d5b8dec21bd314e91afcec122eb8631 (diff) | |
| download | zig-233a0d399135cdb581c5c080726c4cacc71695fe.tar.gz zig-233a0d399135cdb581c5c080726c4cacc71695fe.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 49 |
1 files changed, 16 insertions, 33 deletions
diff --git a/src/main.zig b/src/main.zig index 46dbf1de1e..2fd314def8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -705,17 +705,6 @@ const ArgsIterator = struct { } }; -fn cleanupTempStdinFile( - temp_stdin_file: ?[]const u8, - local_cache_directory: Compilation.Directory, -) void { - if (temp_stdin_file) |file| { - // Some garbage may stay in the file system if removal fails; this - // is harmless so no warning is needed. - local_cache_directory.handle.deleteFile(file) catch {}; - } -} - fn buildOutputType( gpa: Allocator, arena: Allocator, @@ -3031,13 +3020,6 @@ fn buildOutputType( break :l global_cache_directory; }; - var temp_stdin_file: ?[]const u8 = null; - // Note that in one of the happy paths, execve() is used to switch to clang - // in which case this cleanup logic does not run and this temp file is - // leaked. Oh well. It's a minor punishment for using `-x c` which nobody - // should be doing. - defer cleanupTempStdinFile(temp_stdin_file, local_cache_directory); - for (c_source_files.items) |*src| { if (!mem.eql(u8, src.src_path, "-")) continue; @@ -3045,21 +3027,22 @@ fn buildOutputType( fatal("-E or -x is required when reading from a non-regular file", .{}); // "-" is stdin. Dump it to a real file. - const sub_path = blk: { - const sep = fs.path.sep_str; - const sub_path = try std.fmt.allocPrint(arena, "tmp" ++ sep ++ "{x}-stdin{s}", .{ - std.crypto.random.int(u64), ext.canonicalName(target_info.target), - }); - try local_cache_directory.handle.makePath("tmp"); - var f = try local_cache_directory.handle.createFile(sub_path, .{}); - defer f.close(); - errdefer local_cache_directory.handle.deleteFile(sub_path) catch {}; - try f.writeFileAll(io.getStdIn(), .{}); - break :blk sub_path; - }; - // Relative to `local_cache_directory`. - temp_stdin_file = sub_path; - // Relative to current working directory. + const sep = fs.path.sep_str; + const sub_path = try std.fmt.allocPrint(arena, "tmp" ++ sep ++ "{x}-stdin{s}", .{ + std.crypto.random.int(u64), ext.canonicalName(target_info.target), + }); + try local_cache_directory.handle.makePath("tmp"); + // Note that 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. + var f = try local_cache_directory.handle.createFile(sub_path, .{}); + defer f.close(); + try f.writeFileAll(io.getStdIn(), .{}); + + // Convert `sub_path` to be relative to current working directory. src.src_path = try local_cache_directory.join(arena, &.{sub_path}); } |
