From 6736cdc44fbf10dd9650d4647154df7604eb7237 Mon Sep 17 00:00:00 2001 From: Benjamin Ullian Date: Sun, 19 May 2024 21:53:34 -0400 Subject: [PATCH 1/2] Add test for json-inside-json and fix it --- LogMonitor/LogMonitorTests/UtilityTests.cpp | 6 +++ LogMonitor/src/LogMonitor/Utility.cpp | 52 +++++++-------------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/LogMonitor/LogMonitorTests/UtilityTests.cpp b/LogMonitor/LogMonitorTests/UtilityTests.cpp index beb9efee..62e17e97 100644 --- a/LogMonitor/LogMonitorTests/UtilityTests.cpp +++ b/LogMonitor/LogMonitorTests/UtilityTests.cpp @@ -82,6 +82,12 @@ namespace UtilityTests expect = L"C:\\\\Drive\\\\XX"; Utility::SanitizeJson(str); Assert::IsTrue(str == expect, L"should escape \\"); + + str = L"{\"foo\":\"bar\",\"json_in_json\":\"{\\\"key_inner\\\":\\\"value_inner\\\"}\"}"; + expect = L"{\\\"foo\\\":\\\"bar\\\",\\\"json_in_json\\\":\\\"{\\\\\\\"key_inner\\\\\\\":\\\\\\\"value_inner\\\\\\\"}\\\"}"; + Utility::SanitizeJson(str); + Assert::IsTrue(str == expect, L"should properly escape json inside json"); + } }; } diff --git a/LogMonitor/src/LogMonitor/Utility.cpp b/LogMonitor/src/LogMonitor/Utility.cpp index f5b08e34..e1471cab 100644 --- a/LogMonitor/src/LogMonitor/Utility.cpp +++ b/LogMonitor/src/LogMonitor/Utility.cpp @@ -265,43 +265,25 @@ bool Utility::isJsonNumber(_In_ std::wstring& str) /// void Utility::SanitizeJson(_Inout_ std::wstring& str) { - size_t i = 0; - while (i < str.size()) { + size_t num_esc = 0; + for (size_t i = 0; i < str.size(); i++) { auto sub = str.substr(i, 1); - if (sub == L"\"") { - if ((i > 0 && str.substr(i - 1, 1) != L"\\") - || i == 0) - { - str.replace(i, 1, L"\\\""); - i++; - } + if (sub == L"\"" || sub == L"\\" || sub == L"\n" || sub == L"\r") { + num_esc++; } - else if (sub == L"\\") { - if ((i < str.size() - 1 && str.substr(i + 1, 1) != L"\\") - || i == str.size() - 1) - { - str.replace(i, 1, L"\\\\"); - i++; - } - else { - i += 2; - } - } - else if (sub == L"\n") { - if ((i > 0 && str.substr(i - 1, 1) != L"\\") - || i == 0) - { - str.replace(i, 1, L"\\n"); - i++; - } - } - else if (sub == L"\r") { - if ((i > 0 && str.substr(i - 1, 1) != L"\\") - || i == 0) - { - str.replace(i, 1, L"\\r"); - i++; - } + } + + str.reserve(str.size() + num_esc); + for (size_t i = 0; i < str.size(); i++) { + auto sub = str.substr(i, 1); + if (sub == L"\n") { + str.replace(i, 1, L"\\n"); + } else if (sub == L"\r") { + str.replace(i, 1, L"\\r"); + } else if (sub == L"\"" || sub == L"\\") { + str.insert(i, L"\\"); + } else { + continue; } i++; } From ba18d4aaaebd61ed1bee5b3b1f50778e33b68c27 Mon Sep 17 00:00:00 2001 From: Benjamin Ullian Date: Wed, 22 May 2024 11:38:43 -0400 Subject: [PATCH 2/2] do not increment parser offset after finishing parsing the number --- LogMonitor/src/LogMonitor/JsonFileParser.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/LogMonitor/src/LogMonitor/JsonFileParser.cpp b/LogMonitor/src/LogMonitor/JsonFileParser.cpp index 54f4cde0..79c59a17 100644 --- a/LogMonitor/src/LogMonitor/JsonFileParser.cpp +++ b/LogMonitor/src/LogMonitor/JsonFileParser.cpp @@ -250,7 +250,6 @@ JsonFileParser::ParseNumber() // // End of string. // - offset++; AdvanceBufferPointer(offset); m_doubleValue = negativeValue ? -parsedValue : parsedValue;