Skip to content

Commit

Permalink
Fix the item size of value encoded only in tag to be 0 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
onkwon authored Aug 24, 2022
1 parent 15e4d71 commit f4b813c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion include/cbor/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>

#if !defined(CBOR_RECURSION_DEPTH)
#if !defined(CBOR_RECURSION_MAX_LEVEL)
#define CBOR_RECURSION_MAX_LEVEL 4
#endif

Expand Down
7 changes: 4 additions & 3 deletions src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ static cbor_error_t decode_negative_integer(cbor_item_t const *item,
}

uint64_t val = 0;
cbor_copy_be((uint8_t *)&val, buf, item->size);
size_t len = item->size? item->size : 1;
cbor_copy_be((uint8_t *)&val, buf, len);

val = ~val;

Expand All @@ -86,7 +87,7 @@ static cbor_error_t decode_negative_integer(cbor_item_t const *item,
buf[i] = 0xff;
}

cbor_copy_be(buf, (uint8_t *)&val, item->size);
cbor_copy_be(buf, (uint8_t *)&val, len);

return CBOR_SUCCESS;
}
Expand Down Expand Up @@ -153,7 +154,7 @@ cbor_error_t cbor_decode(cbor_reader_t const *reader, cbor_item_t const *item,
if (is_break(item)) {
return CBOR_BREAK;
}
if (item->size > bufsize) {
if (item->size > bufsize || bufsize == 0 || buf == NULL) {
return CBOR_OVERRUN;
}

Expand Down
4 changes: 2 additions & 2 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static cbor_error_t do_integer(struct parser_context *ctx)

cbor_item_t *item = &ctx->items[ctx->itemidx];
item->type = CBOR_ITEM_INTEGER;
item->size = (size_t)(ctx->following_bytes + !ctx->following_bytes);
item->size = (size_t)ctx->following_bytes;
item->offset = ctx->reader->msgidx;

ctx->reader->msgidx += (size_t)(ctx->following_bytes + 1);
Expand Down Expand Up @@ -198,7 +198,7 @@ static cbor_error_t do_float_and_other(struct parser_context *ctx)
cbor_error_t err = CBOR_SUCCESS;

item->type = CBOR_ITEM_FLOAT;
item->size = (size_t)ctx->following_bytes + !ctx->following_bytes;
item->size = (size_t)ctx->following_bytes;
item->offset = ctx->reader->msgidx;

if (ctx->following_bytes == (uint8_t)CBOR_INDEFINITE_VALUE) {
Expand Down
62 changes: 32 additions & 30 deletions tests/src/decoder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST(Decoder, ShouldDecodeUnsignedInteger_WhenEncodedGiven) {
cbor_reader_init(&reader, &m[i], sizeof(m[i]));
LONGS_EQUAL(CBOR_SUCCESS, cbor_parse(&reader, &item, 1, &n));
LONGS_EQUAL(1, n);
LONGS_EQUAL(1, item.size);
LONGS_EQUAL(0, item.size);
LONGS_EQUAL(0, item.offset);
uint8_t v;
LONGS_EQUAL(CBOR_SUCCESS,
Expand Down Expand Up @@ -241,7 +241,7 @@ TEST(Decoder, ShouldDecodeTextString_WhenWrappedInArray) {
LONGS_EQUAL(4, items[0].size); // array
LONGS_EQUAL(1, items[1].size); // text
LONGS_EQUAL(1, items[2].size); // text
LONGS_EQUAL(1, items[3].size); // unsigned
LONGS_EQUAL(0, items[3].size); // unsigned
LONGS_EQUAL(4, items[4].size); // text

LONGS_EQUAL(CBOR_SUCCESS, cbor_decode(&reader, &items[0], buf, sizeof(buf)));
Expand Down Expand Up @@ -289,7 +289,7 @@ TEST(Decoder, ShouldDecodeArray_WhenSingleLevelWithDiffentTypeValueGiven) {
LONGS_EQUAL(4, n);

mock().expectOneCall("f_array").withParameter("size", 3);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_string").withParameter("size", 3);

for (size_t i = 0; i < n; i++) {
Expand All @@ -305,19 +305,19 @@ TEST(Decoder, ShouldDecodeArray_WhenMultiLevelArrayGiven) {
LONGS_EQUAL(8, n);

mock().expectOneCall("f_array").withParameter("size", 3);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
}
}
TEST(Decoder, ShouldDecodeArray_WhenOneByteLengthGiven) {
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
uint8_t m[] = { 0x98,0x19,0x01,0x02,0x03,0x04,0x05,0x06, 0x07,0x08,0x09,
uint8_t m[] = { 0x98,0x19,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,
0x16,0x17,0x18,0x18,0x18,0x19 };
size_t n;
Expand All @@ -327,7 +327,8 @@ TEST(Decoder, ShouldDecodeArray_WhenOneByteLengthGiven) {
LONGS_EQUAL(26, n);

mock().expectOneCall("f_array").withParameter("size", 25);
mock().expectNCalls(25, "f_integer").withParameter("size", 1);
mock().expectNCalls(23, "f_integer").withParameter("size", 0);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand All @@ -343,7 +344,7 @@ TEST(Decoder, ShouldDecodeMap_WhenSingleLevelArrayGiven) {
LONGS_EQUAL(5, n);

mock().expectOneCall("f_map").withParameter("size", 2);
mock().expectNCalls(4, "f_integer").withParameter("size", 1);
mock().expectNCalls(4, "f_integer").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand Down Expand Up @@ -385,10 +386,10 @@ TEST(Decoder, ShouldDecodeMap_WhenArrayValueGiven) {

mock().expectOneCall("f_map").withParameter("size", 2);
mock().expectOneCall("f_string").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_string").withParameter("size", 1);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand Down Expand Up @@ -505,11 +506,11 @@ TEST(Decoder, ShouldDecodeArray_WhenMultiLevelIndefiniteArrarGiven) {
LONGS_EQUAL(10, n);

mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_float").withParameter("size", 0xff);
mock().expectOneCall("f_float").withParameter("size", 0xff);

Expand All @@ -527,11 +528,11 @@ TEST(Decoder, ShouldDecodeArray_WhenMultiLevelIndefiniteArrarGiven2) {
LONGS_EQUAL(9, n);

mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_float").withParameter("size", 0xff);

for (size_t i = 0; i < n; i++) {
Expand All @@ -548,11 +549,11 @@ TEST(Decoder, ShouldDecodeArray_WhenMultiLevelIndefiniteArrarGiven3) {
LONGS_EQUAL(9, n);

mock().expectOneCall("f_array").withParameter("size", 3);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_float").withParameter("size", 0xff);

for (size_t i = 0; i < n; i++) {
Expand All @@ -569,12 +570,12 @@ TEST(Decoder, ShouldDecodeArray_WhenMultiLevelIndefiniteArrarGiven4) {
LONGS_EQUAL(9, n);

mock().expectOneCall("f_array").withParameter("size", 3);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_float").withParameter("size", 0xff);
mock().expectOneCall("f_array").withParameter("size", 2);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand All @@ -592,7 +593,8 @@ TEST(Decoder, ShouldDecodeArray_WhenInfiniteLengthGiven) {
LONGS_EQUAL(27, n);

mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectNCalls(25, "f_integer").withParameter("size", 1);
mock().expectNCalls(23, "f_integer").withParameter("size", 0);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectOneCall("f_float").withParameter("size", 0xff);

for (size_t i = 0; i < n; i++) {
Expand All @@ -610,10 +612,10 @@ TEST(Decoder, ShouldDecodeMap_WhenMultiInfiniteLengthGiven) {

mock().expectOneCall("f_map").withParameter("size", (size_t)-1);
mock().expectOneCall("f_string").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 1);
mock().expectOneCall("f_integer").withParameter("size", 0);
mock().expectOneCall("f_string").withParameter("size", 1);
mock().expectOneCall("f_array").withParameter("size", (size_t)-1);
mock().expectNCalls(2, "f_integer").withParameter("size", 1);
mock().expectNCalls(2, "f_integer").withParameter("size", 0);
mock().expectOneCall("f_float").withParameter("size", 0xff);
mock().expectOneCall("f_float").withParameter("size", 0xff);

Expand Down Expand Up @@ -742,7 +744,7 @@ TEST(Decoder, ShouldDecodeFalse) {
LONGS_EQUAL(CBOR_SUCCESS, cbor_parse(&reader, items, sizeof(items)/sizeof(items[0]), &n));
LONGS_EQUAL(1, n);

mock().expectOneCall("f_simple").withParameter("size", 1);
mock().expectOneCall("f_simple").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand All @@ -760,7 +762,7 @@ TEST(Decoder, ShouldDecodeTrue) {
LONGS_EQUAL(CBOR_SUCCESS, cbor_parse(&reader, items, sizeof(items)/sizeof(items[0]), &n));
LONGS_EQUAL(1, n);

mock().expectOneCall("f_simple").withParameter("size", 1);
mock().expectOneCall("f_simple").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand All @@ -778,7 +780,7 @@ TEST(Decoder, ShouldDecodeNull) {
LONGS_EQUAL(CBOR_SUCCESS, cbor_parse(&reader, items, sizeof(items)/sizeof(items[0]), &n));
LONGS_EQUAL(1, n);

mock().expectOneCall("f_simple").withParameter("size", 1);
mock().expectOneCall("f_simple").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand All @@ -796,7 +798,7 @@ TEST(Decoder, ShouldDecodeSimpleValue) {
LONGS_EQUAL(CBOR_SUCCESS, cbor_parse(&reader, items, sizeof(items)/sizeof(items[0]), &n));
LONGS_EQUAL(1, n);

mock().expectOneCall("f_simple").withParameter("size", 1);
mock().expectOneCall("f_simple").withParameter("size", 0);

for (size_t i = 0; i < n; i++) {
check_item_type[items[i].type](items[i].size);
Expand Down

0 comments on commit f4b813c

Please sign in to comment.