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

Fixes UTF8 output length by accounting for variable header size #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alex-miller-0
Copy link

cb0r was failing to decode data whose key was >23 bytes. This is because 23 is the largest value which does not include a length prefix, per the cbor spec.

Here is an example of the issue:

#include "cb0r.h"
#include "string.h"
#include "stdio.h"
#define NOT_TOO_LONG_KEY "HelloIHaveAVeryLongName"
#define TOO_LONG_KEY "HelloIHaveAVeryLongName!"
int main() {
  // { "HelloIHaveAVeryLongName": 0 }
  uint8_t notTooLong[26] = {
    0xa1, 0x77, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x49, 
    0x48, 0x61, 0x76, 0x65, 0x41, 0x56, 0x65, 0x72, 
    0x79, 0x4c, 0x6f, 0x6e, 0x67, 0x4e, 0x61, 0x6d,
    0x65, 0x00, 
  };
  // { "HelloIHaveAVeryLongName!": 0 }
  uint8_t tooLong[28] = {
    0xa1, 0x78, 0x18, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 
    0x49, 0x48, 0x61, 0x76, 0x65, 0x41, 0x56, 0x65, 
    0x72, 0x79, 0x4c, 0x6f, 0x6e, 0x67, 0x4e, 0x61, 
    0x6d, 0x65, 0x21, 0x00
  };
  cb0r_s result1 = {0};
  cb0r_s data1 = {0};
  if (!cb0r_read(notTooLong, sizeof(notTooLong), &data1))
    return -1;
  bool found = cb0r_find(&data1, CB0R_UTF8, 23, (uint8_t*)NOT_TOO_LONG_KEY, &result1);
  printf("Found not too long key: %d\n\r", found);
  cb0r_s result2 = {0};
  cb0r_s data2 = {0};
  if (!cb0r_read(tooLong, sizeof(tooLong), &data2))
    return -1;
  // FUNNY ENOUGH THIS PASSES IF I USE 25 INSTEAD OF 24
  bool found2 = cb0r_find(&data2, CB0R_UTF8, 24, (uint8_t*)TOO_LONG_KEY, &result2);
  printf("Found too long key: %d", found2);
  return 0;
}

Note the prefix of the 23-character key is 0x77, while that of the 24-character key is 0x78, 0x18, where 0x18 indicates the length of 24.

Failing to account for this extra byte led to result->length being invalid, which led to a decoding failure. Simply capturing this header size results in proper decoding.

Note that we only needed to fix this for UTF8 types and while we have smoke-tested a large range of types and sizes, there are some types we do not use so similar issues may be present in other areas of the code.

@letmaik
Copy link

letmaik commented Jun 21, 2021

Confirmed that this also fixes length of byte arrays, unsurpringly.

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

Successfully merging this pull request may close these issues.

2 participants