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

cbor_map_get and cbor_equal #96

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

cbor_map_get and cbor_equal #96

wants to merge 11 commits into from

Conversation

babelouest
Copy link
Contributor

Add two functions in the library:

  • cbor_equal: compare two cbor items content and return true if type, metadata and data are the same
  • cbor_map_get: return the value corresponding to the specified key in a CBOR map structure

Copy link
Owner

@PJK PJK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @babelouest, thanks for the CL! I have added a few smaller comments to the code; the high level comment is that equality on deeply nested structures is very tricky and I while I'm definitely open to doing this, we will need to give it a lot of consideration.

The main question to start with is: Do you think equality should be structural (exact same shape, e.g. integers must have the same width and value) or semantic (e.g. integers can have different width as long as the value is the same)? What are the tradeoffs? (I haven't implemented equality to avoid having to answer these :))

src/cbor/common.c Show resolved Hide resolved
if (cbor_string_length(item1) != cbor_string_length(item2))
return false;
else
return !memcmp(item1->data, item2->data, cbor_string_length(item1))?true:false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

src/cbor/common.c Show resolved Hide resolved
src/cbor/common.c Show resolved Hide resolved
src/cbor/common.c Show resolved Hide resolved
src/cbor/common.c Show resolved Hide resolved
test/equal_test.c Show resolved Hide resolved
test/type_5_test.c Show resolved Hide resolved
test/type_5_test.c Show resolved Hide resolved
src/cbor/common.c Show resolved Hide resolved
@babelouest
Copy link
Contributor Author

Hello @PJK ,

Thanks for your feedback, I know libcbor and the cbor format for only a few weeks now, so I'm not familiar with all the subtle use cases :-)

My real goal is to have cbor_map_get to facilitate parsing webauthn cbor attestations and assertions, for example:
https://github.com/babelouest/glewlwyd/blob/2.0b/src/scheme/webauthn.c#L1128
https://github.com/babelouest/glewlwyd/blob/2.0b/src/scheme/webauthn.c#L879

In my opinion, a cbor_equal should test strict equality, both data and metadata. Because cbor is a binary format, used by machines rather than humans, so, a machine is more intended to know which format it needs.
But we could create a cbor_equiv in addition to cbor_equal, that would test equivalent cbor data.
The problem would be to set the rules on what is equivalent and what isn't.
For example, is a bytestring of size 4 containning "hello" equivalent to a string containing "hello" ? Is a negative int equivalent to a unsigned int? etc.

But you're the maintainer so it's your call.

I will address your comments soon.

@babelouest
Copy link
Contributor Author

@PJK , I've made some adjustments, and now Travis CI fails, I don't understand why. I may have missed a cbor_decref somewhere but I don't know where...
valgrind says there's no memory leak.

Do you have an idea?

@babelouest
Copy link
Contributor Author

@PJK , any update?

@PJK
Copy link
Owner

PJK commented Jul 23, 2019

Thanks for changes and sorry about the delay, I'm somewhat overloaded these days.

Looking at the Travis failure, I see

+./clang-format.sh
/usr/bin/clang-format-8
+git status
+grep 'nothing to commit, working tree clean'

This means that the code is not formatted according to clang-format's liking -- you can just run the script yourself and commit the changes, that should fix it. Also filed #104 to make this part better.

@babelouest
Copy link
Contributor Author

If I run the clang-format by myself, I don't see any error:

nico@localhost:~/workspace/libcbor$ clang-format-8 --verbose -style=file -i **/*.c **/*.h **/*.cpp
Formatting demo/hello_cbor.c
Formatting examples/cjson2cbor.c
Formatting examples/create_items.c
Formatting examples/hello.c
Formatting examples/readfile.c
Formatting examples/sort.c
Formatting examples/streaming_parser.c
Formatting src/allocators.c
Formatting src/cbor.c
Formatting test/assertions.c
Formatting test/bad_inputs_test.c
Formatting test/callbacks_test.c
Formatting test/cbor_serialize_test.c
Formatting test/cbor_stream_decode_test.c
Formatting test/copy_test.c
Formatting test/equal_test.c
Formatting test/fuzz_test.c
Formatting test/memory_allocation_test.c
Formatting test/pretty_printer_test.c
Formatting test/stream_expectations.c
Formatting test/type_0_encoders_test.c
Formatting test/type_0_test.c
Formatting test/type_1_encoders_test.c
Formatting test/type_1_test.c
Formatting test/type_2_encoders_test.c
Formatting test/type_2_test.c
Formatting test/type_3_encoders_test.c
Formatting test/type_3_test.c
Formatting test/type_4_encoders_test.c
Formatting test/type_4_test.c
Formatting test/type_5_encoders_test.c
Formatting test/type_5_test.c
Formatting test/type_6_encoders_test.c
Formatting test/type_6_test.c
Formatting test/type_7_encoders_test.c
Formatting test/type_7_test.c
Formatting test/unicode_test.c
Formatting src/cbor.h
Formatting test/assertions.h
Formatting test/stream_expectations.h
Formatting test/cpp_linkage_test.cpp
nico@localhost:~/workspace/libcbor$ echo $?
0

Would there be some environment difference between travis-ci and my Ubuntu?

@PJK
Copy link
Owner

PJK commented Jul 25, 2019

Hmm, could be, that is unfortunate. I will try to take a look later this week

@babelouest
Copy link
Contributor Author

After further investigation, it seems the problem comes from the following command in .travis-qemu.sh:

git status | grep "nothing to commit, working tree clean"

I just provided a PR to make some small improvements to the CI script: #105
This should do

@babelouest
Copy link
Contributor Author

All checks have passed

Nailed it! :-)

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

Successfully merging this pull request may close these issues.

2 participants