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

decoding empty string results in 0 #5

Closed
daurnimator opened this issue Feb 10, 2019 · 6 comments
Closed

decoding empty string results in 0 #5

daurnimator opened this issue Feb 10, 2019 · 6 comments

Comments

@daurnimator
Copy link

$ lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require "org.conman.cbor".decode("")
0	2	UINT
@spc476
Copy link
Owner

spc476 commented Feb 10, 2019

I found the bug, but I'm not sure how I want to handle it. The intended code was supposed to throw an error, but thinking about it now, I'm not sure if I want to do that as it is a valid condition. I currently throw errors on bad input, which I'm also thinking was a bad idea (throwing on encoding is fine as that's a programming error, but upon decoding? That's not the programmer's fault but the sender's---at least, that's my thinking).

@spc476
Copy link
Owner

spc476 commented Feb 11, 2019

It now throws an error on no input, which was the original intent.

@spc476 spc476 closed this as completed Feb 11, 2019
@daurnimator
Copy link
Author

Is there something I can encode to get "nothing"?

@spc476
Copy link
Owner

spc476 commented Feb 11, 2019

That's an interesting question---by "nothing" to you mean a 0-byte blob of data? Or some CBOR value that represents "nothing"? I never thought about the former (what would that even mean?) and the later can be accomplished by calling cbor.SIMPLE.null(), which returns a CBOR null value or by calling cbor.SIMPLE.undefined(), which returns a CBOR undefined value. I'm not sure what would be better.

@daurnimator
Copy link
Author

by "nothing" to you mean a 0-byte blob of data

That's what I originally attempted. But I now see that that is probably incorrect

Or some CBOR value that represents "nothing"? I never thought about the former (what would that even mean?) and the later can be accomplished by calling cbor.SIMPLE.null(), which returns a CBOR null value or by calling cbor.SIMPLE.undefined(), which returns a CBOR undefined value. I'm not sure what would be better.

I guess I was looking for cbor.encode(nil)

@spc476
Copy link
Owner

spc476 commented Feb 11, 2019

But that works. cbor.encode() and cbor.encode(nil) both return a string of one character, "\246" which is a CBOR null value.

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

No branches or pull requests

2 participants