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

This commit is contained in:
GethDW
2022-10-11 18:12:03 +01:00
committed by GitHub
parent a72b584c76
commit 01b9fa2c25
2 changed files with 4 additions and 1 deletions

View File

@@ -2378,6 +2378,7 @@ pub const Managed = struct {
/// This is identical to an `init`, followed by a `set`.
pub fn initSet(allocator: Allocator, value: anytype) !Managed {
var s = try Managed.init(allocator);
errdefer s.deinit();
try s.set(value);
return s;
}

View File

@@ -30,8 +30,10 @@ pub const Rational = struct {
/// Create a new Rational. A small amount of memory will be allocated on initialization.
/// This will be 2 * Int.default_capacity.
pub fn init(a: Allocator) !Rational {
var p = try Int.init(a);
errdefer p.deinit();
return Rational{
.p = try Int.init(a),
.p = p,
.q = try Int.initSet(a, 1),
};
}