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

2^-14 fails to serialize on little-endian systems #11

Closed
dgrigsby opened this issue Dec 15, 2015 · 4 comments
Closed

2^-14 fails to serialize on little-endian systems #11

dgrigsby opened this issue Dec 15, 2015 · 4 comments
Milestone

Comments

@dgrigsby
Copy link

From here: http://tools.ietf.org/html/rfc7049#appendix-A
There is this example:
0.00006103515625 -> 0xf90400
It turns out the left hand side is 2^-14.
Using libcbor 0.3.1 on XCode simulator (little-endian), cbor_serialize_alloc fails after cbor_build_float2 for the provided number.
Actually it appears to get into an endless loop until aborted by an illegal malloc request.
Here's a function that demonstrates the issue:

  • (void) testTwoToMinus14
    {
    cbor_item_t* pItem = cbor_build_float2(0.00006103515625);
    unsigned char* buffer;
    size_t buffer_size, length = cbor_serialize_alloc(pItem, &buffer, &buffer_size);
    XCTAssert(length);
    }

Attached is an image showing my test program and the results.
cbor2

@dgrigsby
Copy link
Author

The problem appears to be in cbor_encode_half.
"exp" is -14 (which is correct), represented in a byte as 0xf2.
Then this is interpreted as passing the if (exp > 15) test at line 147.
A zero is returned, which causes cbor_serialize_alloc to uselessly resize the buffer and try again.
The screenshot below shows the sordid details.
cbor3

@dgrigsby
Copy link
Author

This may mean you can't encode any half-float between 0 and 0.5.

dgrigsby pushed a commit to dgrigsby/libcbor that referenced this issue Dec 17, 2015
@PJK PJK modified the milestone: v0.5 Nov 7, 2016
@PJK
Copy link
Owner

PJK commented Jan 9, 2017

Hi @dgrigsby,

I'm not sure this problem has to do with endianess -- I've added all the examples to the test suite (075cdbf) and they seem to pass on x86 machines.

Could you please describe the complete setup/XCode config?

Thank you!

@PJK
Copy link
Owner

PJK commented Jan 12, 2017

You were right, there was a problem in how the encoding works. Until there is a native way to represent half floats in C, it will remain a compromise, but I've updated the encoding so that it always produces a reasonable value.

#49 together with #50 should resolve your issue.

@PJK PJK closed this as completed Jan 12, 2017
PJK added a commit that referenced this issue Sep 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants