From f219099d65746d3b0047d10692d06000524363d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Lu=C3=ADs=20Lucarelo=20Lamonato?= Date: Thu, 19 Oct 2023 01:45:55 -0300 Subject: [PATCH] fix: stack overflow in bug report (#1704) 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. --- data/libs/functions/fs.lua | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/data/libs/functions/fs.lua b/data/libs/functions/fs.lua index 46e81a7352f..f1a98d66f06 100644 --- a/data/libs/functions/fs.lua +++ b/data/libs/functions/fs.lua @@ -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