Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible C stack overflow with json.encode #1079

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ljson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ static soup::UniquePtr<soup::JsonNode> checkJson(lua_State* L, int i)
while (lua_next(L, -2))
{
lua_pushvalue(L, -2);
luaE_incCstack(L);
arr->children.emplace_back(checkJson(L, -2));
L->nCcalls--;
lua_pop(L, 2);
}
lua_pop(L, 1);
Expand Down
20 changes: 15 additions & 5 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -1577,13 +1577,23 @@ do
local function recursive_enc()
local t = {}
t.t = t
try
return require"json".encode(t)
catch e then
return e
return require"json".encode(t)
end
assert(select(2, pcall(recursive_enc)) == "C stack overflow")

local function overflow_enc()
local root = {}
do
local prev_t = root
for i = 1, 10000 do
local t = {}
prev_t:insert(t)
prev_t = t
end
end
return require"json".encode(root)
end
assert(recursive_enc() == "C stack overflow")
assert(select(2, pcall(overflow_enc)) == "C stack overflow")
end
do
local xml = require "pluto:xml"
Expand Down
Loading