Skip to content

Commit

Permalink
fix: stack overflow in bug report (opentibiabr#1704)
Browse files Browse the repository at this point in the history
fix the FS.mkdir_p(path) function that was causing stackoverflow
exception

I made a change so that the code works as follows: The path is divided
into parts, then the function goes through the components, creating the
directory if it does not exist. The process repeats until all components
of the path have been successfully completed, avoiding stackoverflow
exception. At the end, the function returns true to indicate that the
operation was successful.
  • Loading branch information
lamonato29 authored Oct 19, 2023
1 parent 69c763d commit f219099
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions data/libs/functions/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,27 @@ function FS.mkdir_p(path)
if path == "" then
return true
end
if FS.exists(path) then
return true

local components = {}
for component in path:gmatch("[^/\\]+") do
table.insert(components, component)
end
FS.mkdir_p(path:match("(.*[/\\])"))
return FS.mkdir(path)

local currentPath = ""
for i, component in ipairs(components) do
currentPath = currentPath .. component

if not FS.exists(currentPath) then
local success, err = FS.mkdir(currentPath)
if not success then
return false, err
end
end

if i < #components then
currentPath = currentPath .. "/"
end
end

return true
end

0 comments on commit f219099

Please sign in to comment.