Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,23 @@ CBOR_INLINE_API CborError cbor_value_get_uint64(const CborValue *value, uint64_t

CBOR_INLINE_API CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
{
uint64_t v;
assert(cbor_value_is_integer(value));
*result = (int64_t) _cbor_value_extract_int64_helper(value);
v = _cbor_value_extract_int64_helper(value);
if (value->flags & CborIteratorFlag_NegativeInteger)
*result = -*result - 1;
v = -v - 1;
*result = (int64_t) v;
return CborNoError;
}

CBOR_INLINE_API CborError cbor_value_get_int(const CborValue *value, int *result)
{
unsigned v;
assert(cbor_value_is_integer(value));
*result = (int) _cbor_value_extract_int64_helper(value);
v = (unsigned) _cbor_value_extract_int64_helper(value);
if (value->flags & CborIteratorFlag_NegativeInteger)
*result = -*result - 1;
v = -v - 1;
*result = (int) v;
return CborNoError;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/parser/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void addIntegers()
<< quint64(std::numeric_limits<qint64>::max())
<< std::numeric_limits<qint64>::max() << false << true;
QTest::newRow("UINT64_MAX") << raw("\x1b" "\xff\xff\xff\xff" "\xff\xff\xff\xff")
<< std::numeric_limits<quint64>::max() << qint64(-123456) << false << false;
<< std::numeric_limits<quint64>::max() << qint64(-1) << false << false;

// negative integers
QTest::newRow("-1") << raw("\x20") << Q_UINT64_C(0) << Q_INT64_C(-1) << true << true;
Expand All @@ -81,11 +81,11 @@ void addIntegers()
<< quint64(std::numeric_limits<qint64>::max())
<< std::numeric_limits<qint64>::min()
<< true << true;
QTest::newRow("INT64_MIN-1") << raw("\x3b\x80\0\0\0""\0\0\0\0") << Q_UINT64_C(9223372036854775808) << qint64(-123456) << true << false;
QTest::newRow("INT64_MIN-1") << raw("\x3b\x80\0\0\0""\0\0\0\0") << Q_UINT64_C(9223372036854775808) << qint64(9223372036854775807) << true << false;
QTest::newRow("-UINT64_MAX") << raw("\x3b" "\xff\xff\xff\xff" "\xff\xff\xff\xfe")
<< (std::numeric_limits<quint64>::max() - 1) << qint64(-123456) << true << false;
<< (std::numeric_limits<quint64>::max() - 1) << qint64(1) << true << false;
QTest::newRow("-UINT64_MAX+1") << raw("\x3b" "\xff\xff\xff\xff" "\xff\xff\xff\xff")
<< std::numeric_limits<quint64>::max() << qint64(-123456) << true << false;
<< std::numeric_limits<quint64>::max() << qint64(0) << true << false;
}

[[maybe_unused]] void addFloatingPoint()
Expand Down
15 changes: 10 additions & 5 deletions tests/parser/tst_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,21 @@ void tst_Parser::integers()
QVERIFY(cbor_value_is_unsigned_integer(&w.first));
}

// even if this isn't in int64_t's range, these should just silently
// truncate with no UB
int64_t value;
if (inInt64Range) {
cbor_value_get_int64(&w.first, &value);
QCOMPARE(qint64(value), expectedValue);
}
err = cbor_value_get_int64(&w.first, &value);
QCOMPARE(err, CborNoError);
QCOMPARE(qint64(value), expectedValue);
int ivalue;
err = cbor_value_get_int(&w.first, &ivalue);
QCOMPARE(err, CborNoError);
QCOMPARE(qint64(value), expectedValue);

// but these check
err = cbor_value_get_int64_checked(&w.first, &value);
QCOMPARE(err, inInt64Range ? CborNoError : CborErrorDataTooLarge);

int ivalue;
bool inIntRange = inInt64Range && (expectedValue == int(expectedValue));
err = cbor_value_get_int_checked(&w.first, &ivalue);
QCOMPARE(err, inIntRange ? CborNoError : CborErrorDataTooLarge);
Expand Down
Loading