Commit 01b9fa2c25

GethDW <gethwilliams@googlemail.com>
2022-10-11 19:12:03
std: fix memory leak on OutOfMemory error in math.big.int and math.big.rationa
1 parent a72b584
Changed files (2)
lib
std
lib/std/math/big/int.zig
@@ -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;
     }
lib/std/math/big/rational.zig
@@ -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),
         };
     }