aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl/ossl.c
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-12-11 18:43:54 +0900
committerKazuki Yamaguchi <k@rhe.jp>2017-05-22 10:02:03 +0900
commit1596d19f0f3042e2022aec5aeae5afbb7df01386 (patch)
tree65821269a0f75523145e1fbd7d9b7a8dd37d744a /ext/openssl/ossl.c
parent3dd75865b7bf7bd5b89eb760c2e131a27bdd1d43 (diff)
downloadruby-openssl-1596d19f0f3042e2022aec5aeae5afbb7df01386.tar.gz
Add ossl_str_new(), an exception-safe rb_str_new()
Add a new function ossl_str_new() as an exception-safe wrapper of rb_str_new(). This is useful for the openssl library because we can't always raise NoMemoryError immediately due to the independent memory management of OpenSSL.
Diffstat (limited to 'ext/openssl/ossl.c')
-rw-r--r--ext/openssl/ossl.c32
1 files changed, 25 insertions, 7 deletions
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c
index 3ddc3f56..db487ac9 100644
--- a/ext/openssl/ossl.c
+++ b/ext/openssl/ossl.c
@@ -92,22 +92,40 @@ OSSL_IMPL_SK2ARY(x509crl, X509_CRL)
OSSL_IMPL_SK2ARY(x509name, X509_NAME)
static VALUE
-ossl_str_new(int size)
+ossl_str_new_i(VALUE size)
{
- return rb_str_new(0, size);
+ return rb_str_new(NULL, (long)size);
+}
+
+VALUE
+ossl_str_new(const char *ptr, long len, int *pstate)
+{
+ VALUE str;
+ int state;
+
+ str = rb_protect(ossl_str_new_i, len, &state);
+ if (pstate)
+ *pstate = state;
+ if (state) {
+ if (!pstate)
+ rb_set_errinfo(Qnil);
+ return Qnil;
+ }
+ if (ptr)
+ memcpy(RSTRING_PTR(str), ptr, len);
+ return str;
}
VALUE
ossl_buf2str(char *buf, int len)
{
VALUE str;
- int status = 0;
+ int state;
- str = rb_protect((VALUE (*)(VALUE))ossl_str_new, len, &status);
- if(!NIL_P(str)) memcpy(RSTRING_PTR(str), buf, len);
+ str = ossl_str_new(buf, len, &state);
OPENSSL_free(buf);
- if(status) rb_jump_tag(status);
-
+ if (state)
+ rb_jump_tag(state);
return str;
}