diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-04-28 21:58:36 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-04-28 21:58:36 +0000 |
commit | 6fb5140dd6fa398d6f09804e3c035254958763c0 (patch) | |
tree | 57b221b6db780b65b18c817418671941141f72dc /ext/openssl/ossl_bn.c | |
parent | 312a5a8cb47d903f43129bd226a11c96c7e21011 (diff) | |
download | ruby-6fb5140dd6fa398d6f09804e3c035254958763c0.tar.gz |
ossl_bn.c: reduce alloca/malloc
* ext/openssl/ossl_bn.c (ossl_bn_initialize): no need of alloca for
small fixed size array.
* ext/openssl/ossl_bn.c (ossl_bn_initialize): check overflow first,
and use alloca for small size input.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40518 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/openssl/ossl_bn.c')
-rw-r--r-- | ext/openssl/ossl_bn.c | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c index 7d5c7d6347..a01272946e 100644 --- a/ext/openssl/ossl_bn.c +++ b/ext/openssl/ossl_bn.c @@ -123,7 +123,7 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self) if (RB_TYPE_P(str, T_FIXNUM)) { long i; - unsigned char *bin = (unsigned char*)ALLOCA_N(long, 1); + unsigned char bin[sizeof(long)]; long n = FIX2LONG(str); unsigned long un = labs(n); @@ -133,31 +133,32 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self) } GetBN(self, bn); - if (!BN_bin2bn(bin, sizeof(long), bn)) { + if (!BN_bin2bn(bin, sizeof(bin), bn)) { ossl_raise(eBNError, NULL); } if (n < 0) BN_set_negative(bn, 1); return self; } else if (RB_TYPE_P(str, T_BIGNUM)) { - long i, j; + int i, j, len = RBIGNUM_LENINT(str); BDIGIT *ds = RBIGNUM_DIGITS(str); - unsigned char *bin = (unsigned char*)ALLOC_N(BDIGIT, RBIGNUM_LEN(str)); + VALUE buf; + unsigned char *bin = (unsigned char*)ALLOCV_N(BDIGIT, buf, len); - for (i = 0; RBIGNUM_LEN(str) > i; i++) { + for (i = 0; len > i; i++) { BDIGIT v = ds[i]; for (j = sizeof(BDIGIT) - 1; 0 <= j; j--) { - bin[(RBIGNUM_LEN(str)-1-i)*sizeof(BDIGIT)+j] = v&0xff; + bin[(len-1-i)*sizeof(BDIGIT)+j] = v&0xff; v >>= 8; } } GetBN(self, bn); - if (!BN_bin2bn(bin, (int)sizeof(BDIGIT)*RBIGNUM_LENINT(str), bn)) { - xfree(bin); + if (!BN_bin2bn(bin, (int)sizeof(BDIGIT)*len, bn)) { + ALLOCV_END(buf); ossl_raise(eBNError, NULL); } - xfree(bin); + ALLOCV_END(buf); if (!RBIGNUM_SIGN(str)) BN_set_negative(bn, 1); return self; } |