authorgravatar for gethwilliams@googlemail.comGethDW <gethwilliams@googlemail.com> 2022-10-11 18:12:03+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-10-11 20:12:03+03:00
log01b9fa2c2574bd02937ce40525e94026c3acd551
tree9dc57db34c1101496eb8353ffbdb00e9db115507
parenta72b584c765e0b714e40b55b43971d4b0d2ad8dd
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std: fix memory leak on OutOfMemory error in math.big.int and math.big.rationa


2 files changed, 4 insertions(+), 1 deletions(-)

lib/std/math/big/int.zig+1
...@@ -2378,6 +2378,7 @@ pub const Managed = struct {...@@ -2378,6 +2378,7 @@ pub const Managed = struct {
2378 /// This is identical to an `init`, followed by a `set`.2378 /// This is identical to an `init`, followed by a `set`.
2379 pub fn initSet(allocator: Allocator, value: anytype) !Managed {2379 pub fn initSet(allocator: Allocator, value: anytype) !Managed {
2380 var s = try Managed.init(allocator);2380 var s = try Managed.init(allocator);
2381 errdefer s.deinit();
2381 try s.set(value);2382 try s.set(value);
2382 return s;2383 return s;
2383 }2384 }
lib/std/math/big/rational.zig+3-1
...@@ -30,8 +30,10 @@ pub const Rational = struct {...@@ -30,8 +30,10 @@ pub const Rational = struct {
30 /// Create a new Rational. A small amount of memory will be allocated on initialization.30 /// Create a new Rational. A small amount of memory will be allocated on initialization.
31 /// This will be 2 * Int.default_capacity.31 /// This will be 2 * Int.default_capacity.
32 pub fn init(a: Allocator) !Rational {32 pub fn init(a: Allocator) !Rational {
33 var p = try Int.init(a);
34 errdefer p.deinit();
33 return Rational{35 return Rational{
34 .p = try Int.init(a),36 .p = p,
35 .q = try Int.initSet(a, 1),37 .q = try Int.initSet(a, 1),
36 };38 };
37 }39 }