From 8e1888793d97fa73c728d53fdd362f197aee22d6 Mon Sep 17 00:00:00 2001 From: tobil4sk Date: Tue, 24 Sep 2024 15:19:35 +0100 Subject: [PATCH] Fix incorrect handling of scalar documents with trailing content (#92) * Remove use of document_reference type It is not suitable for use here since the behaviour deviates from the regular document type when it comes to trailing content. * Add tests for scalar value with trailing content * Move invalid test case to invalid sub folder --- jsonexamples/invalid/bool_trailing.json | 1 + spec/compile_spec.lua | 18 ++++++++++++++++++ src/luasimdjson.cpp | 8 ++++---- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 jsonexamples/invalid/bool_trailing.json diff --git a/jsonexamples/invalid/bool_trailing.json b/jsonexamples/invalid/bool_trailing.json new file mode 100644 index 0000000..7dcf6e0 --- /dev/null +++ b/jsonexamples/invalid/bool_trailing.json @@ -0,0 +1 @@ +true trailing diff --git a/spec/compile_spec.lua b/spec/compile_spec.lua index 13a5bbd..49053e5 100644 --- a/spec/compile_spec.lua +++ b/spec/compile_spec.lua @@ -105,3 +105,21 @@ if tonumber(major) >= 5 and tonumber(minor) >= 3 then end) end) end + +local invalid_files = { + "bool_trailing.json" +} + +describe("Make sure invalid files are not accepted", function() + for _, file in ipairs(invalid_files) do + it("should fail to parse: " .. file, function() + local fileContents = loadFile("jsonexamples/invalid/" .. file) + local cjsonValue, cjsonError = pcall(function() cjson.decode(fileContents) end) + local simdjsonValue, simdjsonError = pcall(function() simdjson.parse(fileContents) end) + assert.is.False(cjsonValue) + assert.is.False(simdjsonValue) + assert(cjsonError) + assert(simdjsonError) + end) + end +end) diff --git a/src/luasimdjson.cpp b/src/luasimdjson.cpp index 0780d7c..b657652 100644 --- a/src/luasimdjson.cpp +++ b/src/luasimdjson.cpp @@ -43,8 +43,8 @@ ondemand::parser ondemand_parser; simdjson::padded_string jsonbuffer; template -void convert_ondemand_element_to_table(lua_State *L, T element) { - static_assert(std::is_base_of::value || std::is_base_of::value, "type parameter must be document_reference or value"); +void convert_ondemand_element_to_table(lua_State *L, T& element) { + static_assert(std::is_base_of::value || std::is_base_of::value, "type parameter must be document or value"); switch (element.type()) { @@ -168,7 +168,7 @@ static int parse(lua_State *L) try { // makes a padded_string_view for a bit of quickness! doc = ondemand_parser.iterate(get_padded_string_view(json_str, json_str_len, jsonbuffer)); - convert_ondemand_element_to_table(L, ondemand::document_reference(doc)); + convert_ondemand_element_to_table(L, doc); } catch (simdjson::simdjson_error &error) { luaL_error(L, error.what()); } @@ -186,7 +186,7 @@ static int parse_file(lua_State *L) try { json_string = padded_string::load(json_file); doc = ondemand_parser.iterate(json_string); - convert_ondemand_element_to_table(L, ondemand::document_reference(doc)); + convert_ondemand_element_to_table(L, doc); } catch (simdjson::simdjson_error &error) { luaL_error(L, error.what()); }