Skip to content

Commit ce4022d

Browse files
committed
Merge branch 'maint-2.0' into maint
* maint-2.0: x509name: fix handling of X509_NAME_{oneline,print_ex}() return value x509name: refactor OpenSSL::X509::Name#to_s test/test_x509name: change script encoding to ASCII-8BIT
2 parents 158201f + 307db49 commit ce4022d

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

ext/openssl/ossl_x509name.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,27 +250,27 @@ ossl_x509name_to_s_old(VALUE self)
250250
{
251251
X509_NAME *name;
252252
char *buf;
253-
VALUE str;
254253

255254
GetX509Name(self, name);
256255
buf = X509_NAME_oneline(name, NULL, 0);
257-
str = rb_str_new2(buf);
258-
OPENSSL_free(buf);
259-
260-
return str;
256+
if (!buf)
257+
ossl_raise(eX509NameError, "X509_NAME_oneline");
258+
return ossl_buf2str(buf, rb_long2int(strlen(buf)));
261259
}
262260

263261
static VALUE
264262
x509name_print(VALUE self, unsigned long iflag)
265263
{
266264
X509_NAME *name;
267265
BIO *out;
266+
int ret;
268267

269268
GetX509Name(self, name);
270269
out = BIO_new(BIO_s_mem());
271270
if (!out)
272271
ossl_raise(eX509NameError, NULL);
273-
if (!X509_NAME_print_ex(out, name, 0, iflag)) {
272+
ret = X509_NAME_print_ex(out, name, 0, iflag);
273+
if (ret < 0 || iflag == XN_FLAG_COMPAT && ret == 0) {
274274
BIO_free(out);
275275
ossl_raise(eX509NameError, "X509_NAME_print_ex");
276276
}

test/test_x509name.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ def test_to_s
371371
assert_equal "DC = org, DC = ruby-lang, " \
372372
"CN = \"\\E3\\83\\95\\E3\\83\\BC, \\E3\\83\\90\\E3\\83\\BC\"",
373373
name.to_s(OpenSSL::X509::Name::ONELINE)
374+
375+
empty = OpenSSL::X509::Name.new
376+
assert_equal "", empty.to_s
377+
assert_equal "", empty.to_s(OpenSSL::X509::Name::COMPAT)
378+
assert_equal "", empty.to_s(OpenSSL::X509::Name::RFC2253)
379+
assert_equal "", empty.to_s(OpenSSL::X509::Name::ONELINE)
374380
end
375381

376382
def test_to_utf8
@@ -386,6 +392,9 @@ def test_to_utf8
386392
expected = "CN=フー\\, バー,DC=ruby-lang,DC=org".force_encoding("UTF-8")
387393
assert_equal expected, str
388394
assert_equal Encoding.find("UTF-8"), str.encoding
395+
396+
empty = OpenSSL::X509::Name.new
397+
assert_equal "", empty.to_utf8
389398
end
390399

391400
def test_equals2

0 commit comments

Comments
 (0)