Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is DecodingError for byte \0x80 expected? #5

Closed
grzuy opened this issue Jan 18, 2019 · 8 comments
Closed

Is DecodingError for byte \0x80 expected? #5

grzuy opened this issue Jan 18, 2019 · 8 comments
Labels

Comments

@grzuy
Copy link
Contributor

grzuy commented Jan 18, 2019

$ irb
irb(main):002:0> require "libcbor/all"
.../.gem/ruby/2.6.0/gems/libcbor-0.1.0/lib/libcbor.rb:50: warning: constant ::Fixnum is deprecated
=> true
irb(main):004:0> CBOR.decode(CBOR.encode("\x7F".b))
=> "\x7F"
irb(main):005:0> CBOR.decode(CBOR.encode("\x80".b))
Traceback (most recent call last):
        7: from ../ruby-2.6.0/bin/irb:23:in `<main>'
        6: from ../ruby-2.6.0/bin/irb:23:in `load'
        5: from ../ruby-2.6.0/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        4: from (irb):5
        3: from ../.gem/ruby/2.6.0/gems/libcbor-0.1.0/lib/libcbor.rb:65:in `decode'
        2: from ../.gem/ruby/2.6.0/gems/libcbor-0.1.0/lib/libcbor.rb:65:in `tap'
        1: from ../.gem/ruby/2.6.0/gems/libcbor-0.1.0/lib/libcbor.rb:65:in `block in decode'
CBOR::DecodingError (CBOR::DecodingError)
@cabo
Copy link

cabo commented Jan 18, 2019

I can't build libcbor right now. What does CBOR.encode("\x80".b) give you?

@grzuy
Copy link
Contributor Author

grzuy commented Jan 19, 2019

> cbor = CBOR.encode("\x80".b)
=> "a\x80"
> cbor.encoding
=> #<Encoding:ASCII-8BIT>

@cabo
Copy link

cabo commented Jan 19, 2019

Oh. That means the encoder does not recognize "\x80".b as a byte string and instead encodes it as a text string. "\x80" is not valid UTF-8, so this might be why the decoding then fails.

@cabo
Copy link

cabo commented Jan 19, 2019

Apparently, libcbor-ruby does not use the encoding to distinguish byte from text strings, but requires the user to use the separate CBOR::ByteString class for byte strings. I can't test this right now, but if you replace "\x80".b by CBOR::ByteString.new("\x80".b), this should work.

@grzuy
Copy link
Contributor Author

grzuy commented Jan 19, 2019

Indeed, using CBOR::ByteString.new("\x80".b) works.
Thank you!

Would be great if .encode could be able to distinguish byte from test string...

@cabo
Copy link

cabo commented Jan 20, 2019

You could modify CBOR::StringHelper::__libcbor_to_cbor (lib/helpers.rb) to check for encoding and do what CBOR::BytestringHelper::__libcbor_to_cbor does if the encoding is BINARY. (If legacy encodings are still important for text strings for you, you could even convert to UTF-8 for those encodings.)

@grzuy
Copy link
Contributor Author

grzuy commented Jan 20, 2019

Thank you

@PJK
Copy link
Owner

PJK commented Jan 27, 2019

Yup, @cabo is spot on. Filed #6 to improve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants