From 12051b02f1f455b85d5a519dd1747a67d4bb68d0 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 3 Jun 2020 15:55:18 -0600 Subject: [PATCH] fix memory errors --- lib/std/fs/path.zig | 2 +- lib/std/math/big/int.zig | 6 +++++- test/tests.zig | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index 7504570d2d..34326f2872 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -1110,7 +1110,7 @@ pub fn relativePosix(allocator: *Allocator, from: []const u8, to: []const u8) ![ } if (to_rest.len == 0) { // shave off the trailing slash - return result[0 .. result_index - 1]; + return allocator.shrink(result, result_index - 1); } mem.copy(u8, result[result_index..], to_rest); diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 8ee1474275..9379f881db 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -619,6 +619,9 @@ pub const Mutable = struct { var r = try Managed.init(limbs_buffer.allocator); defer r.deinit(); + var tmp_x = try Managed.init(limbs_buffer.allocator); + defer tmp_x.deinit(); + while (y.len() > 1) { assert(x.isPositive() and y.isPositive()); assert(x.len() >= y.len()); @@ -670,7 +673,8 @@ pub const Mutable = struct { try t_big.add(r.toConst(), t_big.toConst()); // u = Cx + Dy, r as u - try x.mul(x.toConst(), Cp); + try tmp_x.copy(x.toConst()); + try x.mul(tmp_x.toConst(), Cp); try r.mul(y.toConst(), Dp); try r.add(x.toConst(), r.toConst()); diff --git a/test/tests.zig b/test/tests.zig index 577f48b03a..0cf5ec28dd 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -648,8 +648,9 @@ pub const StackTracesContext = struct { const stdout = child.stdout.?.inStream().readAllAlloc(b.allocator, max_stdout_size) catch unreachable; defer b.allocator.free(stdout); - var stderr = child.stderr.?.inStream().readAllAlloc(b.allocator, max_stdout_size) catch unreachable; - defer b.allocator.free(stderr); + const stderrFull = child.stderr.?.inStream().readAllAlloc(b.allocator, max_stdout_size) catch unreachable; + defer b.allocator.free(stderrFull); + var stderr = stderrFull; const term = child.wait() catch |err| { debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });