From 7afd811fdd82bf663ed49bc82417132113c7e9f9 Mon Sep 17 00:00:00 2001 From: Semyon Date: Mon, 28 Oct 2024 17:04:03 +0300 Subject: [PATCH] fix abort on invalid null value parsing in BinaryJson (#10991) --- ydb/library/binary_json/ut/entry_ut.cpp | 13 +++++++++++++ ydb/library/binary_json/write.cpp | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ydb/library/binary_json/ut/entry_ut.cpp b/ydb/library/binary_json/ut/entry_ut.cpp index d9099b0f9fdc..097707aec22a 100644 --- a/ydb/library/binary_json/ut/entry_ut.cpp +++ b/ydb/library/binary_json/ut/entry_ut.cpp @@ -17,6 +17,7 @@ class TBinaryJsonEntryTest : public TBinaryJsonTestBase { UNIT_TEST(TestGetContainer); UNIT_TEST(TestGetString); UNIT_TEST(TestGetNumber); + UNIT_TEST(TestInvalidInput); UNIT_TEST_SUITE_END(); void TestGetType() { @@ -93,6 +94,18 @@ class TBinaryJsonEntryTest : public TBinaryJsonTestBase { UNIT_ASSERT_VALUES_EQUAL(container.GetElement(0).GetNumber(), testCase.second); } } + + void TestInvalidInput() { + const TVector> testCases = { + {"nul", "N_ATOM_ERROR: Problem while parsing an atom starting with the letter 'n'"}, + }; + + for (const auto& testCase : testCases) { + const auto parsingResult = SerializeToBinaryJson(testCase.first); + UNIT_ASSERT(parsingResult.IsFail()); + UNIT_ASSERT_VALUES_EQUAL(parsingResult.GetErrorMessage(), testCase.second); + } + } }; UNIT_TEST_SUITE_REGISTRATION(TBinaryJsonEntryTest); diff --git a/ydb/library/binary_json/write.cpp b/ydb/library/binary_json/write.cpp index 7bafea612fac..11cbbd22c55d 100644 --- a/ydb/library/binary_json/write.cpp +++ b/ydb/library/binary_json/write.cpp @@ -600,7 +600,9 @@ template case simdjson::ondemand::json_type::null: { auto is_null = value.is_null(); RETURN_IF_NOT_SUCCESS(is_null.error()); - Y_ABORT_UNLESS(is_null.value_unsafe()); + if (Y_UNLIKELY(!is_null.value_unsafe())) { + return simdjson::error_code::N_ATOM_ERROR; + } callbacks.OnNull(); break; }