ruby-changes:32834
From: akr <ko1@a...>
Date: Wed, 12 Feb 2014 03:25:51 +0900 (JST)
Subject: [ruby-changes:32834] akr:r44909 (trunk): * ext/openssl/ossl_bn.c (ossl_bn_initialize): Use rb_integer_pack.
akr 2014-02-11 23:06:51 +0900 (Tue, 11 Feb 2014) New Revision: 44909 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44909 Log: * ext/openssl/ossl_bn.c (ossl_bn_initialize): Use rb_integer_pack. Fix SEGV by OpenSSL::BN.new(1 << (2**34)). Modified files: trunk/ChangeLog trunk/ext/openssl/ossl_bn.c Index: ChangeLog =================================================================== --- ChangeLog (revision 44908) +++ ChangeLog (revision 44909) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Feb 11 22:59:10 2014 Tanaka Akira <akr@f...> + + * ext/openssl/ossl_bn.c (ossl_bn_initialize): Use rb_integer_pack. + Fix SEGV by OpenSSL::BN.new(1 << (2**34)). + Tue Feb 11 17:00:38 2014 Zachary Scott <e@z...> * ext/tk/README.tcltklib: [DOC] Fix typo by @xta [Fixes GH-532] Index: ext/openssl/ossl_bn.c =================================================================== --- ext/openssl/ossl_bn.c (revision 44908) +++ ext/openssl/ossl_bn.c (revision 44909) @@ -140,26 +140,24 @@ ossl_bn_initialize(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_bn.c#L140 return self; } else if (RB_TYPE_P(str, T_BIGNUM)) { - int i, j, len = RBIGNUM_LENINT(str); - BDIGIT *ds = RBIGNUM_DIGITS(str); + size_t len = rb_absint_size(str, NULL); + unsigned char *bin; VALUE buf; - unsigned char *bin = (unsigned char*)ALLOCV_N(BDIGIT, buf, len); + int sign; - for (i = 0; len > i; i++) { - BDIGIT v = ds[i]; - for (j = SIZEOF_BDIGITS - 1; 0 <= j; j--) { - bin[(len-1-i)*SIZEOF_BDIGITS+j] = v&0xff; - v >>= 8; - } - } + if (INT_MAX < len) { + rb_raise(eBNError, "bignum too long"); + } + bin = (unsigned char*)ALLOCV_N(unsigned char, buf, len); + sign = rb_integer_pack(str, bin, len, 1, 0, INTEGER_PACK_BIG_ENDIAN); GetBN(self, bn); - if (!BN_bin2bn(bin, (int)SIZEOF_BDIGITS*len, bn)) { + if (!BN_bin2bn(bin, (int)len, bn)) { ALLOCV_END(buf); ossl_raise(eBNError, NULL); } ALLOCV_END(buf); - if (!RBIGNUM_SIGN(str)) BN_set_negative(bn, 1); + if (sign < 0) BN_set_negative(bn, 1); return self; } if (RTEST(rb_obj_is_kind_of(str, cBN))) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/