authorgravatar for 67321817+aaron-ang@users.noreply.github.comAaron Ang <67321817+aaron-ang@users.noreply.github.com> 2025-12-24 16:39:56-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-27 20:38:33+01:00
log9db3e23e8081e7598e35a2da53a2b29c064d8404
tree98483662eebe5803949e6068f14ddee9f9cee02f
parente55e6b5528bb2f01de242fcf32b172e244e98e74

std.math.big: require sufficient capacity for aliased params


2 files changed, 133 insertions(+), 57 deletions(-)

lib/std/math/big/int.zig+124-53
......@@ -3013,7 +3013,9 @@ pub const Managed = struct {
30133013 ///
30143014 /// Returns an error if memory could not be allocated.
30153015 pub fn addScalar(r: *Managed, a: *const Managed, scalar: anytype) Allocator.Error!void {
3016 try r.ensureAddScalarCapacity(a.toConst(), scalar);
3016 const needed = @max(a.len(), calcLimbLen(scalar)) + 1;
3017 const aliased = limbsAliasDistinct(r, a);
3018 try r.ensureAliasAwareCapacity(needed, aliased);
30173019 var m = r.toMutable();
30183020 m.addScalar(a.toConst(), scalar);
30193021 r.setMetadata(m.positive, m.len);
......@@ -3025,7 +3027,9 @@ pub const Managed = struct {
30253027 ///
30263028 /// Returns an error if memory could not be allocated.
30273029 pub fn add(r: *Managed, a: *const Managed, b: *const Managed) Allocator.Error!void {
3028 try r.ensureAddCapacity(a.toConst(), b.toConst());
3030 const needed = @max(a.len(), b.len()) + 1;
3031 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3032 try r.ensureAliasAwareCapacity(needed, aliased);
30293033 var m = r.toMutable();
30303034 m.add(a.toConst(), b.toConst());
30313035 r.setMetadata(m.positive, m.len);
......@@ -3043,7 +3047,9 @@ pub const Managed = struct {
30433047 signedness: Signedness,
30443048 bit_count: usize,
30453049 ) Allocator.Error!bool {
3046 try r.ensureTwosCompCapacity(bit_count);
3050 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3051 const needed = calcTwosCompLimbCount(bit_count);
3052 try r.ensureAliasAwareCapacity(needed, aliased);
30473053 var m = r.toMutable();
30483054 const wrapped = m.addWrap(a.toConst(), b.toConst(), signedness, bit_count);
30493055 r.setMetadata(m.positive, m.len);
......@@ -3056,7 +3062,9 @@ pub const Managed = struct {
30563062 ///
30573063 /// Returns an error if memory could not be allocated.
30583064 pub fn addSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!void {
3059 try r.ensureTwosCompCapacity(bit_count);
3065 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3066 const needed = calcTwosCompLimbCount(bit_count);
3067 try r.ensureAliasAwareCapacity(needed, aliased);
30603068 var m = r.toMutable();
30613069 m.addSat(a.toConst(), b.toConst(), signedness, bit_count);
30623070 r.setMetadata(m.positive, m.len);
......@@ -3068,7 +3076,9 @@ pub const Managed = struct {
30683076 ///
30693077 /// Returns an error if memory could not be allocated.
30703078 pub fn sub(r: *Managed, a: *const Managed, b: *const Managed) !void {
3071 try r.ensureCapacity(@max(a.len(), b.len()) + 1);
3079 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3080 const needed = @max(a.len(), b.len()) + 1;
3081 try r.ensureAliasAwareCapacity(needed, aliased);
30723082 var m = r.toMutable();
30733083 m.sub(a.toConst(), b.toConst());
30743084 r.setMetadata(m.positive, m.len);
......@@ -3086,7 +3096,9 @@ pub const Managed = struct {
30863096 signedness: Signedness,
30873097 bit_count: usize,
30883098 ) Allocator.Error!bool {
3089 try r.ensureTwosCompCapacity(bit_count);
3099 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3100 const needed = calcTwosCompLimbCount(bit_count);
3101 try r.ensureAliasAwareCapacity(needed, aliased);
30903102 var m = r.toMutable();
30913103 const wrapped = m.subWrap(a.toConst(), b.toConst(), signedness, bit_count);
30923104 r.setMetadata(m.positive, m.len);
......@@ -3105,7 +3117,9 @@ pub const Managed = struct {
31053117 signedness: Signedness,
31063118 bit_count: usize,
31073119 ) Allocator.Error!void {
3108 try r.ensureTwosCompCapacity(bit_count);
3120 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3121 const needed = calcTwosCompLimbCount(bit_count);
3122 try r.ensureAliasAwareCapacity(needed, aliased);
31093123 var m = r.toMutable();
31103124 m.subSat(a.toConst(), b.toConst(), signedness, bit_count);
31113125 r.setMetadata(m.positive, m.len);
......@@ -3124,7 +3138,9 @@ pub const Managed = struct {
31243138 alias_count += 1;
31253139 if (rma.limbs.ptr == b.limbs.ptr)
31263140 alias_count += 1;
3127 try rma.ensureMulCapacity(a.toConst(), b.toConst());
3141 const needed = a.len() + b.len() + 1;
3142 const capacity_alias = limbsAliasDistinct(rma, a) or limbsAliasDistinct(rma, b);
3143 try rma.ensureAliasAwareCapacity(needed, capacity_alias);
31283144 var m = rma.toMutable();
31293145 if (alias_count == 0) {
31303146 m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator);
......@@ -3156,8 +3172,9 @@ pub const Managed = struct {
31563172 alias_count += 1;
31573173 if (rma.limbs.ptr == b.limbs.ptr)
31583174 alias_count += 1;
3159
3160 try rma.ensureTwosCompCapacity(bit_count);
3175 const needed = calcTwosCompLimbCount(bit_count);
3176 const capacity_alias = limbsAliasDistinct(rma, a) or limbsAliasDistinct(rma, b);
3177 try rma.ensureAliasAwareCapacity(needed, capacity_alias);
31613178 var m = rma.toMutable();
31623179 if (alias_count == 0) {
31633180 m.mulWrapNoAlias(a.toConst(), b.toConst(), signedness, bit_count, rma.allocator);
......@@ -3174,16 +3191,40 @@ pub const Managed = struct {
31743191 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
31753192 }
31763193
3177 pub fn ensureAddScalarCapacity(r: *Managed, a: Const, scalar: anytype) !void {
3178 try r.ensureCapacity(@max(a.limbs.len, calcLimbLen(scalar)) + 1);
3194 /// True if two distinct `Managed` parameters share the same limbs buffer.
3195 ///
3196 /// We specifically exclude the case where `@intFromPtr(a) == @intFromPtr(b)` (same object).
3197 /// When both pointers refer to the same `Managed` instance, `ensureCapacity` can reallocate
3198 /// the buffer (if needed) without creating dangling pointers for that object.
3199 fn limbsAliasDistinct(a: *const Managed, b: *const Managed) bool {
3200 return @intFromPtr(a) != @intFromPtr(b) and a.limbs.ptr == b.limbs.ptr;
3201 }
3202
3203 /// When `aliased` is false (including when both pointers refer to the same object),
3204 /// `ensureCapacity` may reallocate; callers who rely on distinct `Managed` instances
3205 /// aliasing must ensure capacity before aliasing.
3206 /// See https://github.com/ziglang/zig/issues/6167
3207 fn ensureAliasAwareCapacity(r: *Managed, needed: usize, aliased: bool) !void {
3208 if (aliased) {
3209 assert(needed <= r.limbs.len);
3210 } else {
3211 try r.ensureCapacity(needed);
3212 }
31793213 }
31803214
3181 pub fn ensureAddCapacity(r: *Managed, a: Const, b: Const) !void {
3182 try r.ensureCapacity(@max(a.limbs.len, b.limbs.len) + 1);
3215 /// Use this function before doing `addScalar` if some of your parameters alias each other
3216 pub fn ensureAddScalarCapacity(r: *Managed, a: *const Managed, scalar: anytype) !void {
3217 try r.ensureCapacity(@max(a.len(), calcLimbLen(scalar)) + 1);
31833218 }
31843219
3185 pub fn ensureMulCapacity(rma: *Managed, a: Const, b: Const) !void {
3186 try rma.ensureCapacity(a.limbs.len + b.limbs.len + 1);
3220 /// Use this function before doing `add` if some of your parameters alias each other
3221 pub fn ensureAddCapacity(r: *Managed, a: *const Managed, b: *const Managed) !void {
3222 try r.ensureCapacity(@max(a.len(), b.len()) + 1);
3223 }
3224
3225 /// Use this function before doing `mul` if some of your parameters alias each other
3226 pub fn ensureMulCapacity(rma: *Managed, a: *const Managed, b: *const Managed) !void {
3227 try rma.ensureCapacity(a.len() + b.len() + 1);
31873228 }
31883229
31893230 /// q = a / b (rem r)
......@@ -3192,8 +3233,10 @@ pub const Managed = struct {
31923233 ///
31933234 /// Returns an error if memory could not be allocated.
31943235 pub fn divFloor(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
3195 try q.ensureCapacity(a.len());
3196 try r.ensureCapacity(b.len());
3236 const q_alias = limbsAliasDistinct(q, a) or limbsAliasDistinct(q, b);
3237 const r_alias = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3238 try q.ensureAliasAwareCapacity(a.len(), q_alias);
3239 try r.ensureAliasAwareCapacity(b.len(), r_alias);
31973240 var mq = q.toMutable();
31983241 var mr = r.toMutable();
31993242 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.len(), b.len()));
......@@ -3209,8 +3252,10 @@ pub const Managed = struct {
32093252 ///
32103253 /// Returns an error if memory could not be allocated.
32113254 pub fn divTrunc(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
3212 try q.ensureCapacity(a.len());
3213 try r.ensureCapacity(b.len());
3255 const q_alias = limbsAliasDistinct(q, a) or limbsAliasDistinct(q, b);
3256 const r_alias = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3257 try q.ensureAliasAwareCapacity(a.len(), q_alias);
3258 try r.ensureAliasAwareCapacity(b.len(), r_alias);
32143259 var mq = q.toMutable();
32153260 var mr = r.toMutable();
32163261 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.len(), b.len()));
......@@ -3223,7 +3268,9 @@ pub const Managed = struct {
32233268 /// r = a << shift, in other words, r = a * 2^shift
32243269 /// r and a may alias.
32253270 pub fn shiftLeft(r: *Managed, a: *const Managed, shift: usize) !void {
3226 try r.ensureCapacity(a.len() + (shift / limb_bits) + 1);
3271 const aliased = limbsAliasDistinct(r, a);
3272 const needed = a.len() + (shift / limb_bits) + 1;
3273 try r.ensureAliasAwareCapacity(needed, aliased);
32273274 var m = r.toMutable();
32283275 m.shiftLeft(a.toConst(), shift);
32293276 r.setMetadata(m.positive, m.len);
......@@ -3232,7 +3279,9 @@ pub const Managed = struct {
32323279 /// r = a <<| shift with 2s-complement saturating semantics.
32333280 /// r and a may alias.
32343281 pub fn shiftLeftSat(r: *Managed, a: *const Managed, shift: usize, signedness: Signedness, bit_count: usize) !void {
3235 try r.ensureTwosCompCapacity(bit_count);
3282 const aliased = limbsAliasDistinct(r, a);
3283 const needed = calcTwosCompLimbCount(bit_count);
3284 try r.ensureAliasAwareCapacity(needed, aliased);
32363285 var m = r.toMutable();
32373286 m.shiftLeftSat(a.toConst(), shift, signedness, bit_count);
32383287 r.setMetadata(m.positive, m.len);
......@@ -3254,7 +3303,9 @@ pub const Managed = struct {
32543303 return;
32553304 }
32563305
3257 try r.ensureCapacity(a.len() - (shift / limb_bits));
3306 const aliased = limbsAliasDistinct(r, a);
3307 const needed = a.len() - (shift / limb_bits);
3308 try r.ensureAliasAwareCapacity(needed, aliased);
32583309 var m = r.toMutable();
32593310 m.shiftRight(a.toConst(), shift);
32603311 r.setMetadata(m.positive, m.len);
......@@ -3263,7 +3314,9 @@ pub const Managed = struct {
32633314 /// r = ~a under 2s-complement wrapping semantics.
32643315 /// r and a may alias.
32653316 pub fn bitNotWrap(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
3266 try r.ensureTwosCompCapacity(bit_count);
3317 const aliased = limbsAliasDistinct(r, a);
3318 const needed = calcTwosCompLimbCount(bit_count);
3319 try r.ensureAliasAwareCapacity(needed, aliased);
32673320 var m = r.toMutable();
32683321 m.bitNotWrap(a.toConst(), signedness, bit_count);
32693322 r.setMetadata(m.positive, m.len);
......@@ -3273,7 +3326,9 @@ pub const Managed = struct {
32733326 ///
32743327 /// a and b are zero-extended to the longer of a or b.
32753328 pub fn bitOr(r: *Managed, a: *const Managed, b: *const Managed) !void {
3276 try r.ensureCapacity(@max(a.len(), b.len()));
3329 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3330 const needed = @max(a.len(), b.len());
3331 try r.ensureAliasAwareCapacity(needed, aliased);
32773332 var m = r.toMutable();
32783333 m.bitOr(a.toConst(), b.toConst());
32793334 r.setMetadata(m.positive, m.len);
......@@ -3285,7 +3340,8 @@ pub const Managed = struct {
32853340 if (b.isPositive()) b.len() else if (a.isPositive()) a.len() else a.len() + 1
32863341 else if (a.isPositive()) a.len() else if (b.isPositive()) b.len() else b.len() + 1;
32873342
3288 try r.ensureCapacity(cap);
3343 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3344 try r.ensureAliasAwareCapacity(cap, aliased);
32893345 var m = r.toMutable();
32903346 m.bitAnd(a.toConst(), b.toConst());
32913347 r.setMetadata(m.positive, m.len);
......@@ -3294,7 +3350,8 @@ pub const Managed = struct {
32943350 /// r = a ^ b
32953351 pub fn bitXor(r: *Managed, a: *const Managed, b: *const Managed) !void {
32963352 const cap = @max(a.len(), b.len()) + @intFromBool(a.isPositive() != b.isPositive());
3297 try r.ensureCapacity(cap);
3353 const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
3354 try r.ensureAliasAwareCapacity(cap, aliased);
32983355
32993356 var m = r.toMutable();
33003357 m.bitXor(a.toConst(), b.toConst());
......@@ -3306,7 +3363,9 @@ pub const Managed = struct {
33063363 ///
33073364 /// rma's allocator is used for temporary storage to boost multiplication performance.
33083365 pub fn gcd(rma: *Managed, x: *const Managed, y: *const Managed) !void {
3309 try rma.ensureCapacity(@min(x.len(), y.len()));
3366 const aliased = limbsAliasDistinct(rma, x) or limbsAliasDistinct(rma, y);
3367 const needed = @min(x.len(), y.len());
3368 try rma.ensureAliasAwareCapacity(needed, aliased);
33103369 var m = rma.toMutable();
33113370 var limbs_buffer = std.array_list.Managed(Limb).init(rma.allocator);
33123371 defer limbs_buffer.deinit();
......@@ -3317,18 +3376,20 @@ pub const Managed = struct {
33173376 /// r = a * a
33183377 pub fn sqr(rma: *Managed, a: *const Managed) !void {
33193378 const needed_limbs = 2 * a.len() + 1;
3320
3321 if (rma.limbs.ptr == a.limbs.ptr) {
3322 var m = try Managed.initCapacity(rma.allocator, needed_limbs);
3323 errdefer m.deinit();
3324 var m_mut = m.toMutable();
3325 m_mut.sqrNoAlias(a.toConst(), rma.allocator);
3326 m.setMetadata(m_mut.positive, m_mut.len);
3327
3328 rma.deinit();
3329 rma.swap(&m);
3379 const capacity_alias = limbsAliasDistinct(rma, a);
3380 const same_buffer = rma.limbs.ptr == a.limbs.ptr;
3381 try rma.ensureAliasAwareCapacity(needed_limbs, capacity_alias);
3382
3383 if (same_buffer) {
3384 const a_len = a.len();
3385 const tmp = try rma.allocator.alloc(Limb, a_len);
3386 defer rma.allocator.free(tmp);
3387 @memcpy(tmp[0..a_len], a.limbs[0..a_len]);
3388 const a_const = Const{ .limbs = tmp[0..a_len], .positive = a.isPositive() };
3389 var rma_mut = rma.toMutable();
3390 rma_mut.sqrNoAlias(a_const, rma.allocator);
3391 rma.setMetadata(rma_mut.positive, rma_mut.len);
33303392 } else {
3331 try rma.ensureCapacity(needed_limbs);
33323393 var rma_mut = rma.toMutable();
33333394 rma_mut.sqrNoAlias(a.toConst(), rma.allocator);
33343395 rma.setMetadata(rma_mut.positive, rma_mut.len);
......@@ -3337,21 +3398,23 @@ pub const Managed = struct {
33373398
33383399 pub fn pow(rma: *Managed, a: *const Managed, b: u32) !void {
33393400 const needed_limbs = calcPowLimbsBufferLen(a.bitCountAbs(), b);
3401 const capacity_alias = limbsAliasDistinct(rma, a);
3402 const same_buffer = rma.limbs.ptr == a.limbs.ptr;
33403403
3404 try rma.ensureAliasAwareCapacity(needed_limbs, capacity_alias);
33413405 const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);
33423406 defer rma.allocator.free(limbs_buffer);
33433407
3344 if (rma.limbs.ptr == a.limbs.ptr) {
3345 var m = try Managed.initCapacity(rma.allocator, needed_limbs);
3346 errdefer m.deinit();
3347 var m_mut = m.toMutable();
3348 m_mut.pow(a.toConst(), b, limbs_buffer);
3349 m.setMetadata(m_mut.positive, m_mut.len);
3350
3351 rma.deinit();
3352 rma.swap(&m);
3408 if (same_buffer) {
3409 const a_len = a.len();
3410 const tmp = try rma.allocator.alloc(Limb, a_len);
3411 defer rma.allocator.free(tmp);
3412 @memcpy(tmp[0..a_len], a.limbs[0..a_len]);
3413 const a_const = Const{ .limbs = tmp[0..a_len], .positive = a.isPositive() };
3414 var rma_mut = rma.toMutable();
3415 rma_mut.pow(a_const, b, limbs_buffer);
3416 rma.setMetadata(rma_mut.positive, rma_mut.len);
33533417 } else {
3354 try rma.ensureCapacity(needed_limbs);
33553418 var rma_mut = rma.toMutable();
33563419 rma_mut.pow(a.toConst(), b, limbs_buffer);
33573420 rma.setMetadata(rma_mut.positive, rma_mut.len);
......@@ -3361,6 +3424,7 @@ pub const Managed = struct {
33613424 /// r = ⌊√a⌋
33623425 pub fn sqrt(rma: *Managed, a: *const Managed) !void {
33633426 const bit_count = a.bitCountAbs();
3427 const aliased = limbsAliasDistinct(rma, a);
33643428
33653429 if (bit_count == 0) {
33663430 try rma.set(0);
......@@ -3376,7 +3440,8 @@ pub const Managed = struct {
33763440 const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);
33773441 defer rma.allocator.free(limbs_buffer);
33783442
3379 try rma.ensureCapacity((a.len() - 1) / 2 + 1);
3443 const needed = (a.len() - 1) / 2 + 1;
3444 try rma.ensureAliasAwareCapacity(needed, aliased);
33803445 var m = rma.toMutable();
33813446 m.sqrt(a.toConst(), limbs_buffer);
33823447 rma.setMetadata(m.positive, m.len);
......@@ -3384,7 +3449,9 @@ pub const Managed = struct {
33843449
33853450 /// r = truncate(Int(signedness, bit_count), a)
33863451 pub fn truncate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
3387 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
3452 const aliased = limbsAliasDistinct(r, a);
3453 const needed = calcTwosCompLimbCount(bit_count);
3454 try r.ensureAliasAwareCapacity(needed, aliased);
33883455 var m = r.toMutable();
33893456 m.truncate(a.toConst(), signedness, bit_count);
33903457 r.setMetadata(m.positive, m.len);
......@@ -3392,7 +3459,9 @@ pub const Managed = struct {
33923459
33933460 /// r = saturate(Int(signedness, bit_count), a)
33943461 pub fn saturate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
3395 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
3462 const aliased = limbsAliasDistinct(r, a);
3463 const needed = calcTwosCompLimbCount(bit_count);
3464 try r.ensureAliasAwareCapacity(needed, aliased);
33963465 var m = r.toMutable();
33973466 m.saturate(a.toConst(), signedness, bit_count);
33983467 r.setMetadata(m.positive, m.len);
......@@ -3401,7 +3470,9 @@ pub const Managed = struct {
34013470 /// r = @popCount(a) with 2s-complement semantics.
34023471 /// r and a may be aliases.
34033472 pub fn popCount(r: *Managed, a: *const Managed, bit_count: usize) !void {
3404 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
3473 const aliased = limbsAliasDistinct(r, a);
3474 const needed = calcTwosCompLimbCount(bit_count);
3475 try r.ensureAliasAwareCapacity(needed, aliased);
34053476 var m = r.toMutable();
34063477 m.popCount(a.toConst(), bit_count);
34073478 r.setMetadata(m.positive, m.len);
lib/std/math/big/int_test.zig+9-4
......@@ -583,7 +583,9 @@ test "bitcount + sizeInBaseUpperBound" {
583583 try testing.expect(a.sizeInBaseUpperBound(2) >= 32);
584584 try testing.expect(a.sizeInBaseUpperBound(10) >= 10);
585585
586 try a.shiftLeft(&a, 5000);
586 const shift = 5000;
587 try a.ensureCapacity(a.len() + (shift / @bitSizeOf(Limb)) + 1);
588 try a.shiftLeft(&a, shift);
587589 try testing.expectEqual(5032, a.bitCountAbs());
588590 try testing.expect(a.sizeInBaseUpperBound(2) >= 5032);
589591 a.setSign(false);
......@@ -2380,7 +2382,9 @@ test "truncate negative multi to single" {
23802382test "truncate multi unsigned many" {
23812383 var a = try Managed.initSet(testing.allocator, 1);
23822384 defer a.deinit();
2383 try a.shiftLeft(&a, 1023);
2385 const shift = 1023;
2386 try a.ensureCapacity(a.len() + (shift / @bitSizeOf(Limb)) + 1);
2387 try a.shiftLeft(&a, shift);
23842388
23852389 var b = try Managed.init(testing.allocator);
23862390 defer b.deinit();
......@@ -3263,7 +3267,7 @@ test "regression test for 1 limb overflow with alias" {
32633267 var b = try Managed.initSet(testing.allocator, 12200160415121876738);
32643268 defer b.deinit();
32653269
3266 try a.ensureAddCapacity(a.toConst(), b.toConst());
3270 try a.ensureAddCapacity(&a, &b);
32673271 try a.add(&a, &b);
32683272
32693273 try testing.expectEqual(.eq, a.toConst().orderAgainstScalar(19740274219868223167));
......@@ -3277,7 +3281,7 @@ test "regression test for realloc with alias" {
32773281 var b = try Managed.initSet(testing.allocator, 9079598147510263717870894449029933369491131786514446266146);
32783282 defer b.deinit();
32793283
3280 try a.ensureAddCapacity(a.toConst(), b.toConst());
3284 try a.ensureAddCapacity(&a, &b);
32813285 try a.add(&a, &b);
32823286
32833287 try testing.expectEqual(.eq, a.toConst().orderAgainstScalar(14691098406862188148944207245954912110548093601382197697835));
......@@ -3692,6 +3696,7 @@ test "mul multi-multi alias r with a and b" {
36923696 var a = try Managed.initSet(testing.allocator, 2 * maxInt(Limb));
36933697 defer a.deinit();
36943698
3699 try a.ensureMulCapacity(&a, &a);
36953700 try a.mul(&a, &a);
36963701
36973702 var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb));