From f4e0077f1dbd1a5498cde410944ba30d28f6f652 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 8 Nov 2024 18:39:11 +0900 Subject: [PATCH] Check bignum multiplication digits overflow --- bignum.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bignum.c b/bignum.c index e04843f4785b9e..399ef9625a18d8 100644 --- a/bignum.c +++ b/bignum.c @@ -5914,6 +5914,8 @@ bigsq(VALUE x) BDIGIT *xds, *zds; xn = BIGNUM_LEN(x); + if (ADD_OVERFLOW_LONG_P(2, xn)) + rb_raise(rb_eArgError, "square overflow"); zn = 2 * xn; z = bignew(zn, 1); @@ -5942,6 +5944,8 @@ bigmul0(VALUE x, VALUE y) xn = BIGNUM_LEN(x); yn = BIGNUM_LEN(y); + if (ADD_OVERFLOW_LONG_P(xn, yn)) + rb_raise(rb_eArgError, "multiplication overflow"); zn = xn + yn; z = bignew(zn, BIGNUM_SIGN(x)==BIGNUM_SIGN(y));