mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
Audit usages of toOwnedSlice (#32001)
Followup to #30769 I grepped for `try .*toOwnedSlice` and checked all of them by hand. Fixes a bunch of memory leaks removes usages or `errdefer` and `vars` in some places. I also switched array_list.Managed to ArrayList where it was convenient. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/32001 Reviewed-by: Andrew Kelley <andrew@ziglang.org>
This commit is contained in:
committed by
Andrew Kelley
parent
03955476ad
commit
bbab366b78
Vendored
+5
-2
@@ -863,11 +863,14 @@ pub fn render(gpa: Allocator, nodes: []const Node) !std.zig.Ast {
|
||||
.start = @as(u32, @intCast(ctx.buf.items.len)),
|
||||
});
|
||||
|
||||
try ctx.buf.shrinkToLenSentinel(gpa);
|
||||
try ctx.extra_data.shrinkToLen(gpa);
|
||||
|
||||
return .{
|
||||
.source = try ctx.buf.toOwnedSliceSentinel(gpa, 0),
|
||||
.source = ctx.buf.toOwnedSliceSentinelAssert(0),
|
||||
.tokens = ctx.tokens.toOwnedSlice(),
|
||||
.nodes = ctx.nodes.toOwnedSlice(),
|
||||
.extra_data = try ctx.extra_data.toOwnedSlice(gpa),
|
||||
.extra_data = ctx.extra_data.toOwnedSliceAssert(),
|
||||
.errors = &.{},
|
||||
.mode = .zig,
|
||||
};
|
||||
|
||||
@@ -311,17 +311,13 @@ pub fn endInput(p: *Parser) Allocator.Error!Document {
|
||||
p.scratch_string.items.len = 0;
|
||||
p.scratch_extra.items.len = 0;
|
||||
|
||||
var nodes = p.nodes.toOwnedSlice();
|
||||
errdefer nodes.deinit(p.allocator);
|
||||
const extra = try p.extra.toOwnedSlice(p.allocator);
|
||||
errdefer p.allocator.free(extra);
|
||||
const string_bytes = try p.string_bytes.toOwnedSlice(p.allocator);
|
||||
errdefer p.allocator.free(string_bytes);
|
||||
try p.extra.shrinkToLen(p.allocator);
|
||||
try p.string_bytes.shrinkToLen(p.allocator);
|
||||
|
||||
return .{
|
||||
.nodes = nodes,
|
||||
.extra = extra,
|
||||
.string_bytes = string_bytes,
|
||||
.nodes = p.nodes.toOwnedSlice(),
|
||||
.extra = p.extra.toOwnedSliceAssert(),
|
||||
.string_bytes = p.string_bytes.toOwnedSliceAssert(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -778,43 +778,46 @@ pub fn runPkgConfig(step: *Step, lib_name: []const u8) !PkgConfigResult {
|
||||
else => return err,
|
||||
};
|
||||
|
||||
var zig_cflags = std.array_list.Managed([]const u8).init(b.allocator);
|
||||
defer zig_cflags.deinit();
|
||||
var zig_libs = std.array_list.Managed([]const u8).init(b.allocator);
|
||||
defer zig_libs.deinit();
|
||||
var zig_cflags: std.ArrayList([]const u8) = .empty;
|
||||
defer zig_cflags.deinit(b.allocator);
|
||||
var zig_libs: std.ArrayList([]const u8) = .empty;
|
||||
defer zig_libs.deinit(b.allocator);
|
||||
|
||||
var arg_it = mem.tokenizeAny(u8, stdout, " \r\n\t");
|
||||
while (arg_it.next()) |arg| {
|
||||
if (mem.eql(u8, arg, "-I")) {
|
||||
const dir = arg_it.next() orelse return error.PkgConfigInvalidOutput;
|
||||
try zig_cflags.appendSlice(&[_][]const u8{ "-I", dir });
|
||||
try zig_cflags.appendSlice(b.allocator, &.{ "-I", dir });
|
||||
} else if (mem.startsWith(u8, arg, "-I")) {
|
||||
try zig_cflags.append(arg);
|
||||
try zig_cflags.append(b.allocator, arg);
|
||||
} else if (mem.eql(u8, arg, "-L")) {
|
||||
const dir = arg_it.next() orelse return error.PkgConfigInvalidOutput;
|
||||
try zig_libs.appendSlice(&[_][]const u8{ "-L", dir });
|
||||
try zig_libs.appendSlice(b.allocator, &.{ "-L", dir });
|
||||
} else if (mem.startsWith(u8, arg, "-L")) {
|
||||
try zig_libs.append(arg);
|
||||
try zig_libs.append(b.allocator, arg);
|
||||
} else if (mem.eql(u8, arg, "-l")) {
|
||||
const lib = arg_it.next() orelse return error.PkgConfigInvalidOutput;
|
||||
try zig_libs.appendSlice(&[_][]const u8{ "-l", lib });
|
||||
try zig_libs.appendSlice(b.allocator, &.{ "-l", lib });
|
||||
} else if (mem.startsWith(u8, arg, "-l")) {
|
||||
try zig_libs.append(arg);
|
||||
try zig_libs.append(b.allocator, arg);
|
||||
} else if (mem.eql(u8, arg, "-D")) {
|
||||
const macro = arg_it.next() orelse return error.PkgConfigInvalidOutput;
|
||||
try zig_cflags.appendSlice(&[_][]const u8{ "-D", macro });
|
||||
try zig_cflags.appendSlice(b.allocator, &.{ "-D", macro });
|
||||
} else if (mem.startsWith(u8, arg, "-D")) {
|
||||
try zig_cflags.append(arg);
|
||||
try zig_cflags.append(b.allocator, arg);
|
||||
} else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {
|
||||
try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });
|
||||
try zig_cflags.appendSlice(b.allocator, &.{ "-rpath", arg[wl_rpath_prefix.len..] });
|
||||
} else if (b.debug_pkg_config) {
|
||||
return step.fail("unknown pkg-config flag '{s}'", .{arg});
|
||||
}
|
||||
}
|
||||
|
||||
try zig_cflags.shrinkToLen(b.allocator);
|
||||
try zig_libs.shrinkToLen(b.allocator);
|
||||
|
||||
return .{
|
||||
.cflags = try zig_cflags.toOwnedSlice(),
|
||||
.libs = try zig_libs.toOwnedSlice(),
|
||||
.cflags = zig_cflags.toOwnedSliceAssert(),
|
||||
.libs = zig_libs.toOwnedSliceAssert(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2699,7 +2699,9 @@ fn evalGeneric(run: *Run, spawn_options: process.SpawnOptions) !EvalGenericResul
|
||||
|
||||
try multi_reader.checkAnyError();
|
||||
|
||||
// TODO: this string can leak since alloc below can return error.
|
||||
stdout_bytes = try multi_reader.toOwnedSlice(0);
|
||||
// TODO: this string can leak since its allocated using gpa and `try child.wait(io)` below can fail.
|
||||
stderr_bytes = try multi_reader.toOwnedSlice(1);
|
||||
} else {
|
||||
var stdout_reader = stdout.readerStreaming(io, &.{});
|
||||
|
||||
+14
-11
@@ -879,16 +879,16 @@ fn parseAbbrevTable(di: *Dwarf, gpa: Allocator, offset: u64) !Abbrev.Table {
|
||||
var fr: Reader = .fixed(di.section(.debug_abbrev).?);
|
||||
fr.seek = cast(usize, offset) orelse return bad();
|
||||
|
||||
var abbrevs = std.array_list.Managed(Abbrev).init(gpa);
|
||||
var abbrevs: std.ArrayList(Abbrev) = .empty;
|
||||
defer {
|
||||
for (abbrevs.items) |*abbrev| {
|
||||
abbrev.deinit(gpa);
|
||||
}
|
||||
abbrevs.deinit();
|
||||
abbrevs.deinit(gpa);
|
||||
}
|
||||
|
||||
var attrs = std.array_list.Managed(Abbrev.Attr).init(gpa);
|
||||
defer attrs.deinit();
|
||||
var attrs: std.ArrayList(Abbrev.Attr) = .empty;
|
||||
defer attrs.deinit(gpa);
|
||||
|
||||
while (true) {
|
||||
const code = try fr.takeLeb128(u64);
|
||||
@@ -900,7 +900,7 @@ fn parseAbbrevTable(di: *Dwarf, gpa: Allocator, offset: u64) !Abbrev.Table {
|
||||
const attr_id = try fr.takeLeb128(u64);
|
||||
const form_id = try fr.takeLeb128(u64);
|
||||
if (attr_id == 0 and form_id == 0) break;
|
||||
try attrs.append(.{
|
||||
try attrs.append(gpa, .{
|
||||
.id = attr_id,
|
||||
.form_id = form_id,
|
||||
.payload = switch (form_id) {
|
||||
@@ -909,18 +909,18 @@ fn parseAbbrevTable(di: *Dwarf, gpa: Allocator, offset: u64) !Abbrev.Table {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
try abbrevs.append(.{
|
||||
try abbrevs.ensureUnusedCapacity(gpa, 1);
|
||||
abbrevs.appendAssumeCapacity(.{
|
||||
.code = code,
|
||||
.tag_id = tag_id,
|
||||
.has_children = has_children,
|
||||
.attrs = try attrs.toOwnedSlice(),
|
||||
.attrs = try attrs.toOwnedSlice(gpa),
|
||||
});
|
||||
}
|
||||
|
||||
return .{
|
||||
.offset = offset,
|
||||
.abbrevs = try abbrevs.toOwnedSlice(),
|
||||
.abbrevs = try abbrevs.toOwnedSlice(gpa),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1204,10 +1204,13 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, endian: Endian, compile_unit:
|
||||
}
|
||||
}{ .keys = line_table.keys() });
|
||||
|
||||
try directories.shrinkToLen(gpa);
|
||||
try file_entries.shrinkToLen(gpa);
|
||||
|
||||
return .{
|
||||
.line_table = line_table,
|
||||
.directories = try directories.toOwnedSlice(gpa),
|
||||
.files = try file_entries.toOwnedSlice(gpa),
|
||||
.directories = directories.toOwnedSliceAssert(),
|
||||
.files = file_entries.toOwnedSliceAssert(),
|
||||
.version = version,
|
||||
};
|
||||
}
|
||||
|
||||
+24
-16
@@ -83,8 +83,8 @@ pub fn parseDbiStream(self: *Pdb) !void {
|
||||
const mod_info_size = header.mod_info_size;
|
||||
const section_contrib_size = header.section_contribution_size;
|
||||
|
||||
var modules = std.array_list.Managed(Module).init(gpa);
|
||||
errdefer modules.deinit();
|
||||
var modules: std.ArrayList(Module) = .empty;
|
||||
defer modules.deinit(gpa);
|
||||
|
||||
// Module Info Substream
|
||||
var mod_info_offset: usize = 0;
|
||||
@@ -113,11 +113,16 @@ pub fn parseDbiStream(self: *Pdb) !void {
|
||||
this_record_len += march_forward_bytes;
|
||||
}
|
||||
|
||||
try modules.append(.{
|
||||
.mod_info = mod_info,
|
||||
.module_name = try module_name.toOwnedSlice(),
|
||||
.obj_file_name = try obj_file_name.toOwnedSlice(),
|
||||
try modules.ensureUnusedCapacity(gpa, 1);
|
||||
const module_name_slice = try module_name.toOwnedSlice();
|
||||
errdefer gpa.free(module_name_slice);
|
||||
const obj_file_name_slice = try obj_file_name.toOwnedSlice();
|
||||
errdefer gpa.free(obj_file_name_slice);
|
||||
|
||||
modules.appendAssumeCapacity(.{
|
||||
.mod_info = mod_info,
|
||||
.module_name = module_name_slice,
|
||||
.obj_file_name = obj_file_name_slice,
|
||||
.populated = false,
|
||||
.symbols = undefined,
|
||||
.subsect_info = undefined,
|
||||
@@ -131,8 +136,8 @@ pub fn parseDbiStream(self: *Pdb) !void {
|
||||
}
|
||||
|
||||
// Section Contribution Substream
|
||||
var sect_contribs = std.array_list.Managed(pdb.SectionContribEntry).init(gpa);
|
||||
errdefer sect_contribs.deinit();
|
||||
var sect_contribs: std.ArrayList(pdb.SectionContribEntry) = .empty;
|
||||
defer sect_contribs.deinit(gpa);
|
||||
|
||||
var sect_cont_offset: usize = 0;
|
||||
if (section_contrib_size != 0) {
|
||||
@@ -144,7 +149,7 @@ pub fn parseDbiStream(self: *Pdb) !void {
|
||||
sect_cont_offset += @sizeOf(u32);
|
||||
}
|
||||
while (sect_cont_offset != section_contrib_size) {
|
||||
const entry = try sect_contribs.addOne();
|
||||
const entry = try sect_contribs.addOne(gpa);
|
||||
entry.* = try reader.takeStruct(pdb.SectionContribEntry, .little);
|
||||
sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
|
||||
|
||||
@@ -152,8 +157,11 @@ pub fn parseDbiStream(self: *Pdb) !void {
|
||||
return error.InvalidDebugInfo;
|
||||
}
|
||||
|
||||
self.modules = try modules.toOwnedSlice();
|
||||
self.sect_contribs = try sect_contribs.toOwnedSlice();
|
||||
try sect_contribs.shrinkToLen(gpa);
|
||||
try modules.shrinkToLen(gpa);
|
||||
|
||||
self.sect_contribs = sect_contribs.toOwnedSliceAssert();
|
||||
self.modules = modules.toOwnedSliceAssert();
|
||||
}
|
||||
|
||||
pub fn parseIpiStream(self: *Pdb) !void {
|
||||
@@ -1098,22 +1106,22 @@ const MsfStream = struct {
|
||||
}
|
||||
};
|
||||
|
||||
fn readSparseBitVector(reader: *Io.Reader, allocator: Allocator) ![]u32 {
|
||||
fn readSparseBitVector(reader: *Io.Reader, gpa: Allocator) ![]u32 {
|
||||
const num_words = try reader.takeInt(u32, .little);
|
||||
var list = std.array_list.Managed(u32).init(allocator);
|
||||
errdefer list.deinit();
|
||||
var list: std.ArrayList(u32) = .empty;
|
||||
defer list.deinit(gpa);
|
||||
var word_i: u32 = 0;
|
||||
while (word_i != num_words) : (word_i += 1) {
|
||||
const word = try reader.takeInt(u32, .little);
|
||||
var bit_i: u5 = 0;
|
||||
while (true) : (bit_i += 1) {
|
||||
if (word & (@as(u32, 1) << bit_i) != 0) {
|
||||
try list.append(word_i * 32 + bit_i);
|
||||
try list.append(gpa, word_i * 32 + bit_i);
|
||||
}
|
||||
if (bit_i == std.math.maxInt(u5)) break;
|
||||
}
|
||||
}
|
||||
return try list.toOwnedSlice();
|
||||
return try list.toOwnedSlice(gpa);
|
||||
}
|
||||
|
||||
fn blockCountFromSize(size: u32, block_size: u32) u32 {
|
||||
|
||||
+5
-7
@@ -196,19 +196,17 @@ pub fn parseTokens(
|
||||
.zon => try parser.parseZon(),
|
||||
}
|
||||
|
||||
const extra_data = try parser.extra_data.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(extra_data);
|
||||
const errors = try parser.errors.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(errors);
|
||||
try parser.extra_data.shrinkToLen(gpa);
|
||||
try parser.errors.shrinkToLen(gpa);
|
||||
|
||||
// TODO experiment with compacting the MultiArrayList slices here
|
||||
return Ast{
|
||||
return .{
|
||||
.source = source,
|
||||
.mode = mode,
|
||||
.tokens = tokens,
|
||||
.nodes = parser.nodes.toOwnedSlice(),
|
||||
.extra_data = extra_data,
|
||||
.errors = errors,
|
||||
.extra_data = parser.extra_data.toOwnedSliceAssert(),
|
||||
.errors = parser.errors.toOwnedSliceAssert(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -243,10 +243,13 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
|
||||
}
|
||||
}
|
||||
|
||||
try astgen.extra.shrinkToLen(gpa);
|
||||
try astgen.string_bytes.shrinkToLen(gpa);
|
||||
|
||||
return .{
|
||||
.instructions = if (fatal) .empty else astgen.instructions.toOwnedSlice(),
|
||||
.string_bytes = try astgen.string_bytes.toOwnedSlice(gpa),
|
||||
.extra = try astgen.extra.toOwnedSlice(gpa),
|
||||
.string_bytes = astgen.string_bytes.toOwnedSliceAssert(),
|
||||
.extra = astgen.extra.toOwnedSliceAssert(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -397,9 +397,13 @@ pub const Wip = struct {
|
||||
});
|
||||
try wip.extra.appendSlice(gpa, @as([]const u32, @ptrCast(wip.root_list.items)));
|
||||
wip.root_list.clearAndFree(gpa);
|
||||
|
||||
try wip.string_bytes.shrinkToLen(gpa);
|
||||
try wip.extra.shrinkToLen(gpa);
|
||||
|
||||
return .{
|
||||
.string_bytes = try wip.string_bytes.toOwnedSlice(gpa),
|
||||
.extra = try wip.extra.toOwnedSlice(gpa),
|
||||
.string_bytes = wip.string_bytes.toOwnedSliceAssert(),
|
||||
.extra = wip.extra.toOwnedSliceAssert(),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+13
-21
@@ -67,38 +67,30 @@ pub fn generate(gpa: Allocator, tree: Ast, options: Options) Allocator.Error!Zoi
|
||||
}
|
||||
|
||||
if (zg.compile_errors.items.len > 0) {
|
||||
const string_bytes = try zg.string_bytes.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(string_bytes);
|
||||
const compile_errors = try zg.compile_errors.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(compile_errors);
|
||||
const error_notes = try zg.error_notes.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(error_notes);
|
||||
try zg.string_bytes.shrinkToLen(gpa);
|
||||
try zg.compile_errors.shrinkToLen(gpa);
|
||||
try zg.error_notes.shrinkToLen(gpa);
|
||||
|
||||
return .{
|
||||
.nodes = .empty,
|
||||
.extra = &.{},
|
||||
.limbs = &.{},
|
||||
.string_bytes = string_bytes,
|
||||
.compile_errors = compile_errors,
|
||||
.error_notes = error_notes,
|
||||
.string_bytes = zg.string_bytes.toOwnedSliceAssert(),
|
||||
.compile_errors = zg.compile_errors.toOwnedSliceAssert(),
|
||||
.error_notes = zg.error_notes.toOwnedSliceAssert(),
|
||||
};
|
||||
} else {
|
||||
assert(zg.error_notes.items.len == 0);
|
||||
|
||||
var nodes = zg.nodes.toOwnedSlice();
|
||||
errdefer nodes.deinit(gpa);
|
||||
const extra = try zg.extra.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(extra);
|
||||
const limbs = try zg.limbs.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(limbs);
|
||||
const string_bytes = try zg.string_bytes.toOwnedSlice(gpa);
|
||||
errdefer gpa.free(string_bytes);
|
||||
try zg.extra.shrinkToLen(gpa);
|
||||
try zg.limbs.shrinkToLen(gpa);
|
||||
try zg.string_bytes.shrinkToLen(gpa);
|
||||
|
||||
return .{
|
||||
.nodes = nodes,
|
||||
.extra = extra,
|
||||
.limbs = limbs,
|
||||
.string_bytes = string_bytes,
|
||||
.nodes = zg.nodes.toOwnedSlice(),
|
||||
.extra = zg.extra.toOwnedSliceAssert(),
|
||||
.limbs = zg.limbs.toOwnedSliceAssert(),
|
||||
.string_bytes = zg.string_bytes.toOwnedSliceAssert(),
|
||||
.compile_errors = &.{},
|
||||
.error_notes = &.{},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user