intel/tinycbor

Why assuming CBOR stream contains the only top-level entity?

phirsov opened this issue · 1 comments

https://tools.ietf.org/html/rfc7049 states, that:

Data Stream: A sequence of zero or more data items, not further assembled into a larger containing data item.

But tinycbor library handles data stream with one top-level entity only. The following code have been added to the parser test:

namespace QTest {
template<> char *toString<CborType>(const CborType &type)
{
    const char *repr = "other";

    switch (type)
    {
        case CborIntegerType:
            repr = "int";
            break;

        case CborInvalidType:
            repr = "invalid";
            break;

        // not considering other types
    }
    
    return qstrdup(repr);
}
}

// skip ...

void tst_Parser::twotoplevel() // have been already added to tst_Parser class definition
{
    CborParser parser;
    CborValue value;

    const char *data = "\x01\x02";

    QCOMPARE(cbor_parser_init(reinterpret_cast<const quint8 *>(data), strlen(data), 0, &parser, &value), CborNoError);
    
    int parsed;

    QCOMPARE(cbor_value_get_type(&value), CborIntegerType);
    QCOMPARE(cbor_value_get_int(&value, &parsed), CborNoError);
    QCOMPARE(parsed, 1);
    QCOMPARE(cbor_value_advance(&value), CborNoError);

    QCOMPARE(cbor_value_get_type(&value), CborIntegerType); // <---- failed here
    QCOMPARE(cbor_value_get_int(&value, &parsed), CborNoError);
    QCOMPARE(parsed, 2);
}

produces the following failure:

FAIL! : tst_Parser::twotoplevel() Compared values are not the same
Actual (cbor_value_get_type(&value)): invalid
Expected (CborIntegerType) : int

It was intentional like that, allowing one to determine with the same recursive algorithm the full parsing of one data item and recover any further payload bytes that are in the socket/file/whatever.

I don't want to change the behaviour. But we can easily add a flag to cbor_parser_init to read infinite items.