From 8fd02f28a34e744f9ad30ac2a640c0aec50aff02 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 21 Aug 2025 21:48:21 +0900 Subject: [PATCH] bigint.c: fix uninitialized embedded array in bint_new When creating a bigint with embedded storage, the array wasn't being initialized when x->p was NULL but x->sz > 0. This could leave garbage memory in the embedded array, which VS 2022 might interpret differently than VS 2019, causing test failures. This fix ensures the embedded array is always properly initialized with zeros when x->p is NULL, preventing potential undefined behavior. Co-authored-by: Claude --- mrbgems/mruby-bigint/core/bigint.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-bigint/core/bigint.c b/mrbgems/mruby-bigint/core/bigint.c index 050eaf6cc..9dee7e8e3 100644 --- a/mrbgems/mruby-bigint/core/bigint.c +++ b/mrbgems/mruby-bigint/core/bigint.c @@ -784,7 +784,7 @@ limb_addmul_1(mp_limb *rp, const mp_limb *s1p, size_t n, mp_limb limb) return (mp_limb)acc; -#elif defined(_MSC_VER) && defined(MRB_64BIT) +#elif defined(_MSC_VER) && defined(MRB_64BIT) && !defined(MRB_NO_MPZ64BIT) /* 64-bit limbs on MSVC with 6x unrolling: use _umul128 */ unsigned long long carry = 0; size_t i; @@ -2826,7 +2826,13 @@ bint_new(mpz_ctx_t *ctx, mpz_t *x) if (x->sz <= RBIGINT_EMBED_SIZE_MAX) { RBIGINT_SET_EMBED_SIZE(b, x->sz); RBIGINT_SET_EMBED_SIGN(b, x->sn); - if (x->p) memcpy(RBIGINT_EMBED_ARY(b), x->p, x->sz*sizeof(mp_limb)); + if (x->p) { + memcpy(RBIGINT_EMBED_ARY(b), x->p, x->sz*sizeof(mp_limb)); + } + else { + /* Initialize embedded array to zero when x->p is NULL */ + memset(RBIGINT_EMBED_ARY(b), 0, x->sz*sizeof(mp_limb)); + } mpz_clear(ctx, x); } else {