srctree

Gregory Mullen parent ea622e85 6790d1bb
fix some more exec memory leaks

filename was Deleted added: 38, removed: 19, total 19
@@ -0,0 +1,16 @@
const std = @import("std");
 
const Config = @This();
 
var alloc: std.mem.Allocator = undefined;
 
pub const Option = enum{
};
 
 
pub fn init(a: std.mem.Allocator) void {
alloc = a;
}
 
pub fn get(
 
src/exec.zig added: 38, removed: 19, total 19
@@ -131,9 +131,11 @@ pub fn makeAbsExecutable(a: Allocator, str: []const u8) Error![]u8 {
/// Caller will own memory
fn makeExeZ(a: Allocator, str: []const u8) Error!ARG {
var exe = try makeAbsExecutable(a, str);
if (!a.resize(exe, exe.len + 1)) {
if (a.resize(exe, exe.len + 1)) {
exe.len += 1;
} else {
exe = a.realloc(exe, exe.len + 1) catch return Error.Memory;
} else exe.len += 1;
}
exe[exe.len - 1] = 0;
return exe[0 .. exe.len - 1 :0];
}
@@ -149,11 +151,10 @@ fn builtin(a: Allocator, parsed: ParsedIterator) Error!Builtin {
 
/// Caller owns memory of argv, and the open fds
fn binary(a: Allocator, titr: ParsedIterator) Error!Binary {
var exeZ: ?ARG = null;
var argv = ArrayList(?ARG).init(a);
var itr = titr;
 
exeZ = makeExeZ(a, itr.first().cannon()) catch |e| {
var exeZ: ?ARG = makeExeZ(a, itr.first().cannon()) catch |e| {
log.err("path missing {s}\n", .{itr.first().cannon()});
return e;
};
@@ -269,10 +270,12 @@ fn free(a: Allocator, s: *CallableStack) void {
.builtin => {},
.exec => |e| {
// TODO validate this clears all pointers correctly
for (e.argv) |*p| {
a.free(std.mem.sliceTo(p.*.?, 0));
for (e.argv) |*marg| {
if (marg.*) |argz| {
var arg = std.mem.span(argz);
a.free(arg);
}
}
a.free(std.mem.sliceTo(e.arg, 0));
a.free(e.argv);
},
}
@@ -301,19 +304,18 @@ pub fn exec(h: *HSH, titr: *TokenIterator) Error!void {
 
var fpid: std.os.pid_t = 0;
for (stack) |*s| {
defer free(h.alloc, s);
if (s.conditional) |cond| {
if (fpid == 0) unreachable;
switch (cond) {
.After => _ = jobs.waitFor(h, fpid) catch {},
.Failure => {
if (jobs.waitFor(h, fpid) catch true) {
free(h.alloc, s);
continue;
}
},
.Success => {
if (!(jobs.waitFor(h, fpid) catch return Error.PipelineError)) {
free(h.alloc, s);
continue;
}
},
@@ -367,7 +369,6 @@ pub fn exec(h: *HSH, titr: *TokenIterator) Error!void {
if (s.stdio.in != std.os.STDIN_FILENO) std.os.close(s.stdio.in);
if (s.stdio.out != std.os.STDOUT_FILENO) std.os.close(s.stdio.out);
if (s.stdio.err != std.os.STDERR_FILENO) std.os.close(s.stdio.err);
//free(h.alloc, s);
}
}
 
 
src/parse.zig added: 38, removed: 19, total 19
@@ -74,7 +74,7 @@ pub const ParsedIterator = struct {
return token;
}
 
fn subtokensDel(self: *Self) void {
fn subtokensDel(self: *Self) bool {
if (self.subtokens) |subtkns| {
const l = subtkns.len;
self.alloc.free(subtkns[0].raw);
@@ -83,6 +83,10 @@ pub const ParsedIterator = struct {
}
self.subtokens = self.alloc.realloc(subtkns, subtkns.len - 1) catch unreachable;
}
if (self.subtokens) |st| {
return st.len > 0;
}
return false;
}
 
fn subtokensDupe(self: *Self, str: []const u8) !void {
@@ -110,7 +114,7 @@ pub const ParsedIterator = struct {
if (subtkns[0].next()) |n| {
return n;
} else {
self.subtokensDel();
_ = self.subtokensDel();
return self.nextSubtoken(token);
}
} else {
@@ -186,10 +190,8 @@ pub const ParsedIterator = struct {
self.alloc.free(self.resolved);
}
self.resolved = self.alloc.alloc([]u8, 0) catch @panic("Alloc 0 can't fail");
if (self.subtokens) |ts| {
self.alloc.free(ts);
self.subtokens = null;
}
while (self.subtokensDel()) {}
self.subtokens = null;
}
 
/// Alias for restart to free stored memory