Skip to content

Commit

Permalink
[fix] initialize @unused_bits = 0 for BitString
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jan 9, 2025
1 parent 86daed3 commit e175e69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
25 changes: 15 additions & 10 deletions src/main/java/org/jruby/ext/openssl/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -1574,16 +1574,21 @@ static void initializeImpl(final ThreadContext context,

// NOTE: Primitive only
final String baseName = self.getMetaClass().getRealClass().getBaseName();
if ( "ObjectId".equals( baseName ) ) {
final String name;
try {
name = oid2Sym( runtime, getObjectID(runtime, value.toString()), true );
}
catch (IllegalArgumentException e) {
// e.g. in case of nil "string not an OID"
throw runtime.newTypeError(e.getMessage());
}
if ( name != null ) value = runtime.newString(name);
switch (baseName) {
case "ObjectId":
final String name;
try {
name = oid2Sym( runtime, getObjectID(runtime, value.toString()), true );
}
catch (IllegalArgumentException e) {
// e.g. in case of nil "string not an OID"
throw runtime.newTypeError(e.getMessage());
}
if ( name != null ) value = runtime.newString(name);
break;
case "BitString":
self.setInstanceVariable("@unused_bits", runtime.newFixnum(0));
break;
}

self.setInstanceVariable("@tag", tag);
Expand Down
11 changes: 4 additions & 7 deletions src/test/ruby/test_asn1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1162,14 +1162,11 @@ def test_decode
#assert_equal calulated_sig, sig_val.value
end

# This is from the upstream MRI tests, might be superseded by `test_bit_string_infinite_length`?
# This is from the upstream MRI tests
def test_bitstring
# TODO: Import Issue
# fails <nil> expected but was <0>
#encode_decode_test B(%w{ 03 01 00 }), OpenSSL::ASN1::BitString.new(B(%w{}))
# TODO: Import Issue
# fails with <nil> expected but was <0>
#encode_decode_test B(%w{ 03 02 00 01 }), OpenSSL::ASN1::BitString.new(B(%w{ 01 }))
encode_decode_test B(%w{ 03 01 00 }), OpenSSL::ASN1::BitString.new(B(%w{}))
encode_decode_test B(%w{ 03 02 00 01 }), OpenSSL::ASN1::BitString.new(B(%w{ 01 }))

obj = OpenSSL::ASN1::BitString.new(B(%w{ F0 }))
obj.unused_bits = 4
encode_decode_test B(%w{ 03 02 04 F0 }), obj
Expand Down

0 comments on commit e175e69

Please sign in to comment.