diff --git a/include/sonic/internal/arch/simd_skip.h b/include/sonic/internal/arch/simd_skip.h index 16f90765..54807831 100644 --- a/include/sonic/internal/arch/simd_skip.h +++ b/include/sonic/internal/arch/simd_skip.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -29,7 +30,7 @@ #include INCLUDE_ARCH_FILE(skip.h) -#include +#include "sonic/dom/parser.h" namespace sonic_json { namespace internal { @@ -346,7 +347,16 @@ class SkipScanner2 { } sonic_force_inline uint8_t peek() { + if (sonic_unlikely(pos_ >= len_)) { + setError(SonicError::kParseErrorEof); + return 0; + } auto peek = scanner_.SkipSpaceSafe(data_, pos_, len_); + // SkipSpaceSafe returns the last space if no token remains. + if (sonic_unlikely(pos_ == len_ && IsSpace(peek))) { + setError(SonicError::kParseErrorEof); + return 0; + } pos_ -= 1; return peek; } diff --git a/include/sonic/internal/itoa.h b/include/sonic/internal/itoa.h index c11f0d66..aef17ea3 100644 --- a/include/sonic/internal/itoa.h +++ b/include/sonic/internal/itoa.h @@ -56,7 +56,6 @@ sonic_force_inline char *Utoa_1_8(char *out, uint32_t val) { lz = val < 10; Copy2Digs(out, &kDigits[val * 2 + lz]); return out + 2 - lz; - ; } else if (val < 10000) { // 3 ~ 4 digits hi = val / 100; lo = val % 100; diff --git a/tests/jsonpath_test.cpp b/tests/jsonpath_test.cpp index 2a977d63..6cc4448a 100644 --- a/tests/jsonpath_test.cpp +++ b/tests/jsonpath_test.cpp @@ -554,6 +554,22 @@ TEST(JsonPath, JsonInfiniteLoop3) { EXPECT_EQ(std::get<0>(got), ""); } +TEST(JsonPath, IncompleteArrayIndex) { + auto got = GetByJsonPathOnDemand("[8", "$[0]"); + EXPECT_EQ(std::get<1>(got), kParseErrorEof); + EXPECT_EQ(std::get<0>(got), "8"); + + TestFail("[8 ", "$[0]"); + TestFail("[8,", "$[0]"); + TestFail("[8, ", "$[0]"); + TestFail("[8,9", "$[0]"); + TestFail("[[8", "$[0][0]"); + + got = GetByJsonPathOnDemand("[8]", "$[0]"); + EXPECT_EQ(std::get<1>(got), kErrorNone); + EXPECT_EQ(std::get<0>(got), "8"); +} + TEST(JsonPath, JsonTuple) { auto json = R"({a:1, b:2c})"; auto got = GetByJsonPathOnDemand(json, "$.b");