Skip to content

Commit 7605557

Browse files
committed
tinycbor bug fixes
- Fix cbor_buf_cmp to do memcmp and return complemented result - Fix container_size for the container - cbor_encoder_close_container(): look at isMap flag to determine container_size for comparison - iterate_string_chunks(): fixing NULL compare at the end of string and moving it out of the iterate_string_chunks(). This is to avoid buffer specific parser calls in the function - Re-enabling all parser tests - cbor_value_get_next_byte() is removed in mynewt version of tinycbor, so, we track offsets of the buffer which can be used for comparison in the parser tests instead of calculating the offset
1 parent 4c53ee1 commit 7605557

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ tag: distcheck
203203

204204
cflags := $(CPPFLAGS) -I$(SRCDIR)include
205205
cflags += -DTINYCBOR_VERSION_SUFFIX=\"$(DIRTYSRC)\"
206-
cflags += -ggdb -std=c99 $(CFLAGS)
206+
cflags += -ggdb -std=c99 -O0 $(CFLAGS)
207207
%.o: %.c
208208
@test -d $(@D) || $(MKDIR) $(@D)
209209
$(CC) $(cflags) $($(basename $(notdir $@))_CCFLAGS) -c -o $@ $<

src/cbor_buf_reader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ cbor_buf_reader_cmp(struct cbor_decoder_reader *d, char *dst, int src_offset,
6060
size_t len)
6161
{
6262
struct cbor_buf_reader *cb = (struct cbor_buf_reader *) d;
63-
return memcmp(dst, cb->buffer + src_offset, len);
63+
64+
return !memcmp(dst, cb->buffer + src_offset, len);
6465
}
6566

6667
static uintptr_t

src/cborencoder.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *writer, int fl
203203
encoder->writer = writer;
204204
encoder->added = 0;
205205
encoder->flags = flags;
206+
encoder->container_size = 0;
206207
}
207208

208209
#if FLOAT_SUPPORT
@@ -429,7 +430,7 @@ static CborError create_container(CborEncoder *encoder, CborEncoder *container,
429430
container->writer = encoder->writer;
430431
++encoder->added;
431432
container->added = 0;
432-
encoder->container_size = length;
433+
container->container_size = length;
433434

434435
cbor_static_assert(((MapType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == CborIteratorFlag_ContainerIsMap);
435436
cbor_static_assert(((ArrayType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == 0);
@@ -502,13 +503,21 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
502503
*/
503504
CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
504505
{
506+
size_t container_size;
507+
505508
encoder->writer = containerEncoder->writer;
506509

507-
if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
510+
if (containerEncoder->flags & CborIteratorFlag_UnknownLength) {
508511
return append_byte_to_buffer(encoder, BreakByte);
509-
else
510-
return containerEncoder->container_size == containerEncoder->added ? CborNoError :
511-
containerEncoder->container_size < containerEncoder->added ? CborErrorTooManyItems : CborErrorTooFewItems;
512+
} else {
513+
container_size = containerEncoder->container_size;
514+
if (containerEncoder->flags & CborIteratorFlag_ContainerIsMap) {
515+
container_size = containerEncoder->container_size * 2;
516+
}
517+
518+
return container_size == containerEncoder->added ? CborNoError :
519+
container_size < containerEncoder->added ? CborErrorTooManyItems : CborErrorTooFewItems;
520+
}
512521
}
513522

514523
/**

src/cborparser.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,16 +1172,6 @@ static CborError iterate_string_chunks(const CborValue *value, char *buffer, siz
11721172
}
11731173
}
11741174

1175-
/* is there enough room for the ending NUL byte? */
1176-
if (*result && *buflen > total) {
1177-
/* we are just trying to write a NULL byte here,, but this is hard
1178-
* because this is called by function pointer with an abstract
1179-
* reader. Since this is the output buffer, we can assume that if
1180-
* we have a valid buffer its ok to write a NULL here */
1181-
if(buffer) {
1182-
*(buffer + total) = '\0';
1183-
}
1184-
}
11851175
*buflen = total;
11861176

11871177
if (next) {
@@ -1264,8 +1254,19 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
12641254
bool copied_all;
12651255
CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
12661256
buffer ? (IterateFunction) value->parser->d->cpy : iterate_noop);
1267-
return err ? err :
1268-
copied_all ? CborNoError : CborErrorOutOfMemory;
1257+
if (err) {
1258+
return err;
1259+
}
1260+
1261+
if (!copied_all) {
1262+
return CborErrorOutOfMemory;
1263+
}
1264+
1265+
if (buffer) {
1266+
*((uint8_t *)buffer + *buflen) = '\0';
1267+
}
1268+
1269+
return CborNoError;
12691270
}
12701271

12711272
/**
@@ -1290,6 +1291,7 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
12901291
{
12911292
CborValue copy = *value;
12921293
CborError err = cbor_value_skip_tag(&copy);
1294+
12931295
if (err)
12941296
return err;
12951297
if (!cbor_value_is_text_string(&copy)) {
@@ -1298,8 +1300,17 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
12981300
}
12991301

13001302
size_t len = strlen(string);
1301-
return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len,
1302-
result, NULL, value->parser->d->cmp);
1303+
err = iterate_string_chunks(&copy, CONST_CAST(char *, string), &len,
1304+
result, NULL, value->parser->d->cmp);
1305+
if (err) {
1306+
return err;
1307+
}
1308+
1309+
if (*result && string[len] != '\0') {
1310+
*result = false;
1311+
}
1312+
1313+
return CborNoError;
13031314
}
13041315

13051316
/**
@@ -1394,7 +1405,7 @@ CborError cbor_value_map_find_value(const CborValue *map, const char *string, Cb
13941405
&equals, element, map->parser->d->cmp);
13951406
if (err)
13961407
goto error;
1397-
if (equals)
1408+
if (equals && string[dummyLen] == '\0')
13981409
return preparse_value(element);
13991410
} else {
14001411
/* skip this key */

tests/parser/tst_parser.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void compareOne_real(const QByteArray &data, const QString &expected, int line,
243243
QCOMPARE(int(err2), int(err));
244244

245245
// check that we consumed everything
246-
//QCOMPARE((void*)cbor_value_get_next_byte(&first), (void*)data.constEnd());
246+
QCOMPARE(cbor_value_at_end(&first), true);
247247

248248
compareFailed = false;
249249
}
@@ -1543,6 +1543,7 @@ void tst_Parser::validation()
15431543
QFETCH(QByteArray, data);
15441544
QFETCH(int, flags);
15451545
QFETCH(CborError, expectedError);
1546+
QFETCH(int, offset);
15461547

15471548
QString decoded;
15481549
CborParser parser;
@@ -1557,6 +1558,7 @@ void tst_Parser::validation()
15571558
CborError err3 = cbor_value_validate(&first, CborValidateBasic);
15581559
err = parseOne(&first, &decoded);
15591560
QCOMPARE(int(err), int(expectedError));
1561+
QCOMPARE(first.offset, offset);
15601562

15611563
if (!QByteArray(QTest::currentDataTag()).contains("utf8")) {
15621564
QCOMPARE(int(err2), int(expectedError));
@@ -1978,7 +1980,7 @@ void tst_Parser::endPointer_data()
19781980
void tst_Parser::endPointer()
19791981
{
19801982
QFETCH(QByteArray, data);
1981-
//QFETCH(int, offset);
1983+
QFETCH(int, offset);
19821984

19831985
QString decoded;
19841986
CborParser parser;
@@ -1991,7 +1993,7 @@ void tst_Parser::endPointer()
19911993

19921994
err = parseOne(&first, &decoded);
19931995
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
1994-
//QCOMPARE(int(first.ptr - reinterpret_cast<const quint8 *>(data.constBegin())), offset);
1996+
QCOMPARE(first.offset, offset);
19951997
}
19961998

19971999
void tst_Parser::recursionLimit_data()

0 commit comments

Comments
 (0)