From fadd268a609aefa155bbe5c7fe9d7c07956e9cda Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 29 Aug 2025 20:34:20 -0700 Subject: [PATCH] upgrade more old API uses --- build.zig | 2 +- lib/compiler/aro/aro/Compilation.zig | 22 ++------------- lib/compiler/aro/aro/Driver.zig | 4 +-- lib/std/Build/Step/CheckObject.zig | 40 ++++++++++++++-------------- lib/std/Io/Writer.zig | 9 ++++--- lib/std/array_list.zig | 1 + src/link/MachO/CodeSignature.zig | 4 +-- src/main.zig | 3 ++- test/src/Cases.zig | 2 +- 9 files changed, 36 insertions(+), 51 deletions(-) diff --git a/build.zig b/build.zig index 5696dd44f3..316a2c6ccf 100644 --- a/build.zig +++ b/build.zig @@ -304,7 +304,7 @@ pub fn build(b: *std.Build) !void { if (enable_llvm) { const cmake_cfg = if (static_llvm) null else blk: { if (findConfigH(b, config_h_path_option)) |config_h_path| { - const file_contents = fs.cwd().readFileAlloc(b.allocator, config_h_path, max_config_h_bytes) catch unreachable; + const file_contents = fs.cwd().readFileAlloc(config_h_path, b.allocator, .limited(max_config_h_bytes)) catch unreachable; break :blk parseConfigH(b, file_contents); } else { std.log.warn("config.h could not be located automatically. Consider providing it explicitly via \"-Dconfig_h\"", .{}); diff --git a/lib/compiler/aro/aro/Compilation.zig b/lib/compiler/aro/aro/Compilation.zig index b7bc2d8d09..48015077c7 100644 --- a/lib/compiler/aro/aro/Compilation.zig +++ b/lib/compiler/aro/aro/Compilation.zig @@ -1308,13 +1308,7 @@ fn addSourceFromPathExtra(comp: *Compilation, path: []const u8, kind: Source.Kin return error.FileNotFound; } - const file = try comp.cwd.openFile(path, .{}); - defer file.close(); - - const contents = file.readToEndAlloc(comp.gpa, std.math.maxInt(u32)) catch |err| switch (err) { - error.FileTooBig => return error.StreamTooLong, - else => |e| return e, - }; + const contents = try comp.cwd.readFileAlloc(path, comp.gpa, .limited(std.math.maxInt(u32))); errdefer comp.gpa.free(contents); return comp.addSourceFromOwnedBuffer(contents, path, kind); @@ -1433,19 +1427,7 @@ fn getFileContents(comp: *Compilation, path: []const u8, limit: ?u32) ![]const u return error.FileNotFound; } - const file = try comp.cwd.openFile(path, .{}); - defer file.close(); - - var buf = std.array_list.Managed(u8).init(comp.gpa); - defer buf.deinit(); - - const max = limit orelse std.math.maxInt(u32); - file.deprecatedReader().readAllArrayList(&buf, max) catch |e| switch (e) { - error.StreamTooLong => if (limit == null) return e, - else => return e, - }; - - return buf.toOwnedSlice(); + return comp.cwd.readFileAlloc(path, comp.gpa, .limited(limit orelse std.math.maxInt(u32))); } pub fn findEmbed( diff --git a/lib/compiler/aro/aro/Driver.zig b/lib/compiler/aro/aro/Driver.zig index cb6b3e95e1..a6e3503b72 100644 --- a/lib/compiler/aro/aro/Driver.zig +++ b/lib/compiler/aro/aro/Driver.zig @@ -519,8 +519,8 @@ fn option(arg: []const u8, name: []const u8) ?[]const u8 { fn addSource(d: *Driver, path: []const u8) !Source { if (mem.eql(u8, "-", path)) { - const stdin = std.fs.File.stdin().deprecatedReader(); - const input = try stdin.readAllAlloc(d.comp.gpa, std.math.maxInt(u32)); + var stdin_reader: std.fs.File.Reader = .initStreaming(.stdin(), &.{}); + const input = try stdin_reader.interface.allocRemaining(d.comp.gpa, .limited(std.math.maxInt(u32))); defer d.comp.gpa.free(input); return d.comp.addSourceFromBuffer("", input); } diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index 2f7afa8dba..4a2efde407 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -1703,7 +1703,7 @@ const ElfDumper = struct { var reader: std.Io.Reader = .fixed(bytes); const magic = try reader.takeArray(elf.ARMAG.len); - if (!mem.eql(u8, &magic, elf.ARMAG)) { + if (!mem.eql(u8, magic, elf.ARMAG)) { return error.InvalidArchiveMagicNumber; } @@ -2623,10 +2623,10 @@ const WasmDumper = struct { return step.fail("invalid wasm opcode '{d}'", .{byte}); }; switch (opcode) { - .i32_const => try writer.print("i32.const {x}\n", .{try std.leb.readIleb128(i32)}), - .i64_const => try writer.print("i64.const {x}\n", .{try std.leb.readIleb128(i64)}), - .f32_const => try writer.print("f32.const {x}\n", .{@as(f32, @bitCast(try reader.readInt(u32, .little)))}), - .f64_const => try writer.print("f64.const {x}\n", .{@as(f64, @bitCast(try reader.readInt(u64, .little)))}), + .i32_const => try writer.print("i32.const {x}\n", .{try reader.takeLeb128(i32)}), + .i64_const => try writer.print("i64.const {x}\n", .{try reader.takeLeb128(i64)}), + .f32_const => try writer.print("f32.const {x}\n", .{@as(f32, @bitCast(try reader.takeInt(u32, .little)))}), + .f64_const => try writer.print("f64.const {x}\n", .{@as(f64, @bitCast(try reader.takeInt(u64, .little)))}), .global_get => try writer.print("global.get {x}\n", .{try reader.takeLeb128(u32)}), else => unreachable, } @@ -2638,7 +2638,7 @@ const WasmDumper = struct { /// https://webassembly.github.io/spec/core/appendix/custom.html fn parseDumpNames(step: *Step, reader: *std.Io.Reader, writer: *std.Io.Writer, data: []const u8) !void { - while (reader.context.pos < data.len) { + while (reader.seek < data.len) { switch (try parseDumpType(step, std.wasm.NameSubsection, reader, writer)) { // The module name subsection ... consists of a single name // that is assigned to the module itself. @@ -2646,9 +2646,9 @@ const WasmDumper = struct { const size = try reader.takeLeb128(u32); const name_len = try reader.takeLeb128(u32); if (size != name_len + 1) return error.BadSubsectionSize; - if (reader.context.pos + name_len > data.len) return error.UnexpectedEndOfStream; - try writer.print("name {s}\n", .{data[reader.context.pos..][0..name_len]}); - reader.context.pos += name_len; + if (reader.seek + name_len > data.len) return error.UnexpectedEndOfStream; + try writer.print("name {s}\n", .{data[reader.seek..][0..name_len]}); + reader.seek += name_len; }, // The function name subsection ... consists of a name map @@ -2664,9 +2664,9 @@ const WasmDumper = struct { for (0..entries) |_| { const index = try reader.takeLeb128(u32); const name_len = try reader.takeLeb128(u32); - if (reader.context.pos + name_len > data.len) return error.UnexpectedEndOfStream; - const name = data[reader.context.pos..][0..name_len]; - reader.context.pos += name.len; + if (reader.seek + name_len > data.len) return error.UnexpectedEndOfStream; + const name = data[reader.seek..][0..name_len]; + reader.seek += name.len; try writer.print( \\index {d} @@ -2694,8 +2694,8 @@ const WasmDumper = struct { var current_field: u32 = 0; while (current_field < field_count) : (current_field += 1) { const field_name_length = try reader.takeLeb128(u32); - const field_name = data[reader.context.pos..][0..field_name_length]; - reader.context.pos += field_name_length; + const field_name = data[reader.seek..][0..field_name_length]; + reader.seek += field_name_length; const value_count = try reader.takeLeb128(u32); try writer.print( @@ -2706,12 +2706,12 @@ const WasmDumper = struct { var current_value: u32 = 0; while (current_value < value_count) : (current_value += 1) { const value_length = try reader.takeLeb128(u32); - const value = data[reader.context.pos..][0..value_length]; - reader.context.pos += value_length; + const value = data[reader.seek..][0..value_length]; + reader.seek += value_length; const version_length = try reader.takeLeb128(u32); - const version = data[reader.context.pos..][0..version_length]; - reader.context.pos += version_length; + const version = data[reader.seek..][0..version_length]; + reader.seek += version_length; try writer.print( \\value_name {s} @@ -2730,8 +2730,8 @@ const WasmDumper = struct { while (index < feature_count) : (index += 1) { const prefix_byte = try reader.takeLeb128(u8); const name_length = try reader.takeLeb128(u32); - const feature_name = data[reader.context.pos..][0..name_length]; - reader.context.pos += name_length; + const feature_name = data[reader.seek..][0..name_length]; + reader.seek += name_length; try writer.print("{c} {s}\n", .{ prefix_byte, feature_name }); } diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index a7a92bd180..8bfb1bb75e 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -2636,7 +2636,7 @@ pub const Allocating = struct { assert(a.alignment == alignment); // Required for Allocator correctness. const w = &a.writer; const result: std.array_list.Aligned(u8, alignment) = .{ - .items = w.buffer[0..w.end], + .items = @alignCast(w.buffer[0..w.end]), .capacity = w.buffer.len, }; w.buffer = &.{}; @@ -2645,12 +2645,15 @@ pub const Allocating = struct { } pub fn ensureUnusedCapacity(a: *Allocating, additional_count: usize) Allocator.Error!void { - const new_capacity = std.math.add(usize, a.writer.buffer.len, additional_count) catch return error.OutOfMemory; + const new_capacity = std.math.add(usize, a.writer.end, additional_count) catch return error.OutOfMemory; return ensureTotalCapacity(a, new_capacity); } pub fn ensureTotalCapacity(a: *Allocating, new_capacity: usize) Allocator.Error!void { - return ensureTotalCapacityPrecise(a, ArrayList(u8).growCapacity(a.writer.buffer.len, new_capacity)); + // Protects growing unnecessarily since better_capacity will be larger. + if (a.writer.buffer.len >= new_capacity) return; + const better_capacity = ArrayList(u8).growCapacity(a.writer.buffer.len, new_capacity); + return ensureTotalCapacityPrecise(a, better_capacity); } pub fn ensureTotalCapacityPrecise(a: *Allocating, new_capacity: usize) Allocator.Error!void { diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index a218e47276..9718d19c7e 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -405,6 +405,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type return; } + // Protects growing unnecessarily since better_capacity will be larger. if (self.capacity >= new_capacity) return; const better_capacity = Aligned(T, alignment).growCapacity(self.capacity, new_capacity); diff --git a/src/link/MachO/CodeSignature.zig b/src/link/MachO/CodeSignature.zig index 25addd8cdc..00b01a6eab 100644 --- a/src/link/MachO/CodeSignature.zig +++ b/src/link/MachO/CodeSignature.zig @@ -245,9 +245,7 @@ pub fn deinit(self: *CodeSignature, allocator: Allocator) void { } pub fn addEntitlements(self: *CodeSignature, allocator: Allocator, path: []const u8) !void { - const file = try fs.cwd().openFile(path, .{}); - defer file.close(); - const inner = try file.readToEndAlloc(allocator, std.math.maxInt(u32)); + const inner = try fs.cwd().readFileAlloc(path, allocator, .limited(std.math.maxInt(u32))); self.entitlements = .{ .inner = inner }; } diff --git a/src/main.zig b/src/main.zig index da27b70668..b502d780c2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5693,7 +5693,8 @@ fn jitCmd( try child.spawn(); if (options.capture) |ptr| { - ptr.* = try child.stdout.?.readToEndAlloc(arena, std.math.maxInt(u32)); + var stdout_reader = child.stdout.?.readerStreaming(&.{}); + ptr.* = try stdout_reader.interface.allocRemaining(arena, .limited(std.math.maxInt(u32))); } const term = try child.wait(); diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 18bca9841e..fcdade09a3 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -386,7 +386,7 @@ fn addFromDirInner( current_file.* = filename; const max_file_size = 10 * 1024 * 1024; - const src = try iterable_dir.readFileAllocOptions(ctx.arena, filename, max_file_size, null, .@"1", 0); + const src = try iterable_dir.readFileAllocOptions(filename, ctx.arena, .limited(max_file_size), .@"1", 0); // Parse the manifest var manifest = try TestManifest.parse(ctx.arena, src);