Skip to content

Commit 8e18887

Browse files
authored
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
1 parent eab3aab commit 8e18887

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true trailing

spec/compile_spec.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,21 @@ if tonumber(major) >= 5 and tonumber(minor) >= 3 then
105105
end)
106106
end)
107107
end
108+
109+
local invalid_files = {
110+
"bool_trailing.json"
111+
}
112+
113+
describe("Make sure invalid files are not accepted", function()
114+
for _, file in ipairs(invalid_files) do
115+
it("should fail to parse: " .. file, function()
116+
local fileContents = loadFile("jsonexamples/invalid/" .. file)
117+
local cjsonValue, cjsonError = pcall(function() cjson.decode(fileContents) end)
118+
local simdjsonValue, simdjsonError = pcall(function() simdjson.parse(fileContents) end)
119+
assert.is.False(cjsonValue)
120+
assert.is.False(simdjsonValue)
121+
assert(cjsonError)
122+
assert(simdjsonError)
123+
end)
124+
end
125+
end)

src/luasimdjson.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ ondemand::parser ondemand_parser;
4343
simdjson::padded_string jsonbuffer;
4444

4545
template<typename T>
46-
void convert_ondemand_element_to_table(lua_State *L, T element) {
47-
static_assert(std::is_base_of<ondemand::document_reference, T>::value || std::is_base_of<ondemand::value, T>::value, "type parameter must be document_reference or value");
46+
void convert_ondemand_element_to_table(lua_State *L, T& element) {
47+
static_assert(std::is_base_of<ondemand::document, T>::value || std::is_base_of<ondemand::value, T>::value, "type parameter must be document or value");
4848

4949
switch (element.type()) {
5050

@@ -168,7 +168,7 @@ static int parse(lua_State *L)
168168
try {
169169
// makes a padded_string_view for a bit of quickness!
170170
doc = ondemand_parser.iterate(get_padded_string_view(json_str, json_str_len, jsonbuffer));
171-
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
171+
convert_ondemand_element_to_table(L, doc);
172172
} catch (simdjson::simdjson_error &error) {
173173
luaL_error(L, error.what());
174174
}
@@ -186,7 +186,7 @@ static int parse_file(lua_State *L)
186186
try {
187187
json_string = padded_string::load(json_file);
188188
doc = ondemand_parser.iterate(json_string);
189-
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
189+
convert_ondemand_element_to_table(L, doc);
190190
} catch (simdjson::simdjson_error &error) {
191191
luaL_error(L, error.what());
192192
}

0 commit comments

Comments
 (0)