aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl/ossl_bn.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/openssl/ossl_bn.c')
-rw-r--r--ext/openssl/ossl_bn.c30
1 files changed, 8 insertions, 22 deletions
diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c
index 750d8ad87f..c3a371a008 100644
--- a/ext/openssl/ossl_bn.c
+++ b/ext/openssl/ossl_bn.c
@@ -167,41 +167,27 @@ ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
switch (base) {
case 0:
len = BN_bn2mpi(bn, NULL);
- if (!(buf = OPENSSL_malloc(len))) {
- ossl_raise(eBNError, "Cannot allocate mem for BN");
- }
- if (BN_bn2mpi(bn, buf) != len) {
- OPENSSL_free(buf);
+ str = rb_str_new(0, len);
+ if (BN_bn2mpi(bn, RSTRING(str)->ptr) != len)
ossl_raise(eBNError, NULL);
- }
break;
case 2:
len = BN_num_bytes(bn);
- if (!(buf = OPENSSL_malloc(len))) {
- ossl_raise(eBNError, "Cannot allocate mem for BN");
- }
- if (BN_bn2bin(bn, buf) != len) {
- OPENSSL_free(buf);
+ str = rb_str_new(0, len);
+ if (BN_bn2bin(bn, RSTRING(str)->ptr) != len)
ossl_raise(eBNError, NULL);
- }
break;
case 10:
- if (!(buf = BN_bn2dec(bn))) {
- ossl_raise(eBNError, NULL);
- }
- len = strlen(buf);
+ if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
+ str = ossl_buf2str(buf, strlen(buf));
break;
case 16:
- if (!(buf = BN_bn2hex(bn))) {
- ossl_raise(eBNError, NULL);
- }
- len = strlen(buf);
+ if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
+ str = ossl_buf2str(buf, strlen(buf));
break;
default:
ossl_raise(rb_eArgError, "illegal radix %d", base);
}
- str = rb_str_new(buf, len);
- OPENSSL_free(buf);
return str;
}