spc476 / CBOR

The most comprehensive CBOR module in the Lua universe.
GNU Lesser General Public License v3.0
22 stars 3 forks source link

decoding empty string results in 0 #5

Closed daurnimator closed 5 years ago

daurnimator commented 5 years ago
$ lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require "org.conman.cbor".decode("")
0   2   UINT
spc476 commented 5 years ago

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 commented 5 years ago

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

daurnimator commented 5 years ago

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

spc476 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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