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

Add safe version of os.date #2

Merged
merged 1 commit into from
Jul 19, 2023
Merged
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
28 changes: 25 additions & 3 deletions controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,29 @@ local function get_clear(pos)
end
end

local function safe_date()
return(os.date("*t",os.time()))
-- Wraps os.date to only replace valid formats,
-- ignoring invalid ones that would cause a hard crash.
local TIME_MAX = 32535244800 -- 01/01/3001
local function safe_date(str, time)
if type(time) ~= "number" then
time = os.time()
elseif time < 0 or time >= TIME_MAX then
return nil
end
if type(str) ~= "string" then
return os.date("%c", time)
end
if str == "*t" then
return os.date("*t", time)
end
str = string.gsub(str, "%%[aAbBcdHImMpSwxXyY]", function(s)
return os.date(s, time)
end)
return str
end

local function datetable()
return os.date("*t")
end

-- string.rep(str, n) with a high value for n can be used to DoS
Expand Down Expand Up @@ -557,7 +578,8 @@ local function create_environment(pos, mem, event, itbl, send_warning)
clock = os.clock,
difftime = os.difftime,
time = os.time,
datetable = safe_date,
date = safe_date,
datetable = datetable,
},
}
env._G = env
Expand Down