Add test witnessing unescaping bug

This commit is contained in:
2025-05-19 17:15:56 -04:00
parent 9a5b94e25d
commit 4174f1c609

View File

@@ -3,6 +3,7 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <limits>
#include <string> #include <string>
#include <doctest.h> #include <doctest.h>
@@ -208,10 +209,10 @@ TEST_CASE("parser3") {
TEST_CASE("streaming") { testStreaming(json); } TEST_CASE("streaming") { testStreaming(json); }
void doTestUnescapingUtf8(std::string const &escaped, void doTestUnescapingUtf8(std::string const &escaped,
std::string const &expected, bool streaming) { std::string const &expected, int stride) {
CAPTURE(escaped); CAPTURE(escaped);
CAPTURE(expected); CAPTURE(expected);
CAPTURE(streaming); CAPTURE(stride);
auto c = noopCallbacks(); auto c = noopCallbacks();
std::string result; std::string result;
c.on_string_data = +[](void *p, const char *buf, int len) { c.on_string_data = +[](void *p, const char *buf, int len) {
@@ -220,13 +221,11 @@ void doTestUnescapingUtf8(std::string const &escaped,
}; };
parser3::Parser3 parser(&c, &result); parser3::Parser3 parser(&c, &result);
auto copy = escaped; auto copy = escaped;
if (streaming) { for (int i = 0; i < copy.size(); i += stride) {
for (int i = 0; i < copy.size(); ++i) {
CAPTURE(i); CAPTURE(i);
CHECK(parser.parse(copy.data() + i, 1) == parser3::S_AGAIN); CHECK(
} parser.parse(copy.data() + i, std::min<int>(stride, copy.size() - i)) ==
} else { parser3::S_AGAIN);
CHECK(parser.parse(copy.data(), copy.size()) == parser3::S_AGAIN);
} }
CHECK(parser.parse(nullptr, 0) == parser3::S_OK); CHECK(parser.parse(nullptr, 0) == parser3::S_OK);
CHECK(result.size() == expected.size()); CHECK(result.size() == expected.size());
@@ -235,8 +234,11 @@ void doTestUnescapingUtf8(std::string const &escaped,
void testUnescapingUtf8(std::string const &escaped, void testUnescapingUtf8(std::string const &escaped,
std::string const &expected) { std::string const &expected) {
doTestUnescapingUtf8(escaped, expected, false); for (int stride = 0; stride < 10; ++stride) {
doTestUnescapingUtf8(escaped, expected, true); doTestUnescapingUtf8(escaped, expected,
stride == 0 ? std::numeric_limits<int>::max()
: stride);
}
} }
TEST_CASE("unescaping utf-8") { TEST_CASE("unescaping utf-8") {