Skip to content

Commit 4a13b3e

Browse files
parser: add a test that the string copy functions properly terminate
Or don't terminate, as the case may be. Relates to #194. Signed-off-by: Thiago Macieira <[email protected]>
1 parent 5d62d78 commit 4a13b3e

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

tests/parser/tst_parser.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -990,22 +990,44 @@ static void chunkedStringTest(const QByteArray &data, const QString &concatenate
990990
err = cbor_value_calculate_string_length(&copy, &n);
991991
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
992992

993-
QByteArray buffer(n, Qt::Uninitialized);
993+
size_t nn = n;
994+
QByteArray buffer(n + 1, Qt::Uninitialized);
995+
QByteArray buffer2(n + 1, Qt::Uninitialized);
996+
buffer[int(n)] = 0xff;
997+
buffer2[int(n)] = 0xff;
994998
QString formatted;
995999
if (cbor_value_is_byte_string(&copy)) {
996-
err = cbor_value_copy_byte_string(&copy, (uint8_t *)buffer.data(), &n, nullptr);
1000+
err = cbor_value_copy_byte_string(&copy, (uint8_t *)buffer.data(), &nn, nullptr);
9971001
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
998-
QCOMPARE(int(n), buffer.size());
1002+
QCOMPARE(nn, n);
9991003

1000-
formatted = QString::fromLatin1("h'" + buffer.toHex() + '\'');
1004+
formatted = QString::fromLatin1("h'" + QByteArray::fromRawData(buffer.data(), n).toHex() + '\'');
1005+
1006+
// repeat by allowing the null termination
1007+
nn = n + 1;
1008+
err = cbor_value_copy_byte_string(&copy, (uint8_t *)buffer2.data(), &nn, nullptr);
10011009
} else {
10021010
err = cbor_value_copy_text_string(&copy, buffer.data(), &n, nullptr);
10031011
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
1004-
QCOMPARE(int(n), buffer.size());
1012+
QCOMPARE(nn, n);
10051013

10061014
formatted = '"' + QString::fromUtf8(buffer.data(), n) + '"';
1015+
1016+
// repeat by allowing the null termination
1017+
nn = n + 1;
1018+
err = cbor_value_copy_text_string(&copy, buffer2.data(), &nn, nullptr);
10071019
}
1020+
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
10081021
QCOMPARE(formatted, concatenated);
1022+
1023+
// verify terminators
1024+
QCOMPARE(buffer.at(n), char(0xff));
1025+
QCOMPARE(buffer2.at(n), '\0');
1026+
QCOMPARE(nn, n);
1027+
1028+
buffer.truncate(n);
1029+
buffer2.truncate(n);
1030+
QCOMPARE(buffer2, buffer);
10091031
}
10101032

10111033
// confirm that the extra string we appended is still here

0 commit comments

Comments
 (0)