Skip to content

Commit c06de8f

Browse files
Krishna Paifacebook-github-bot
authored andcommitted
fix (json_parse): Fix parseHex asan error
Summary: Fixes json_parse parseHex asan error. This happens when a string is generated with an incomplete escape sequence. Also inlined the call to parseHex. Differential Revision: D70595015
1 parent bfeb189 commit c06de8f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

velox/functions/prestosql/json/JsonStringUtil.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ size_t normalizedSizeForJsonCast(const char* input, size_t length) {
198198

199199
namespace {
200200

201-
int32_t parseHex(const std::string_view& hexString) {
201+
FOLLY_ALWAYS_INLINE int32_t parseHex(std::string_view hexString) {
202202
int32_t result = 0;
203203
for (auto c : hexString) {
204204
result = (result << 4) + digitToHex(c);
@@ -235,7 +235,7 @@ int32_t getEscapedChar(std::string_view view, size_t& pos) {
235235
pos++;
236236
return '/';
237237
case 'u': {
238-
if (pos + 5 > view.size()) {
238+
if (pos + 6 > view.size()) {
239239
VELOX_USER_FAIL("Invalid escape sequence at the end of string");
240240
}
241241

@@ -349,7 +349,7 @@ size_t normalizeForJsonParse(const char* input, size_t length, char* output) {
349349
continue;
350350
case 'u': {
351351
VELOX_USER_CHECK(
352-
start + 5 <= end, "Invalid escape sequence at the end of string");
352+
start + 6 <= end, "Invalid escape sequence at the end of string");
353353

354354
// Read 4 hex digits.
355355
auto codePoint = parseHex(std::string_view(start + 2, 4));
@@ -442,7 +442,7 @@ size_t normalizedSizeForJsonParse(const char* input, size_t length) {
442442
continue;
443443
case 'u': {
444444
VELOX_USER_CHECK(
445-
start + 5 <= end, "Invalid escape sequence at the end of string");
445+
start + 6 <= end, "Invalid escape sequence at the end of string");
446446
auto codePoint = parseHex(std::string_view(start + 2, 4));
447447
if (isHighSurrogate(codePoint) || isLowSurrogate(codePoint) ||
448448
isSpecialCode(codePoint)) {

velox/functions/prestosql/tests/JsonFunctionsTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ TEST_F(JsonFunctionsTest, jsonParse) {
481481
R"({"\/&\"\f\r\n": "a", "\/&page=26": "c"})",
482482
R"({"/&\"\f\r\n":"a","/&page=26":"c"})");
483483
}
484+
485+
// Test with incomplete unicode escape sequences.
486+
VELOX_ASSERT_USER_THROW(
487+
jsonParse("\"\\u1234\\u89\""),
488+
"Invalid escape sequence at the end of string");
484489
}
485490

486491
TEST_F(JsonFunctionsTest, canonicalization) {

0 commit comments

Comments
 (0)