'mktime' can return -1

This commit is contained in:
Roberto I
2026-09-15 09:49:44 -03:00
parent 35a87f8c5b
commit 4c32b2dfd2
2 changed files with 24 additions and 8 deletions

View File

@@ -345,11 +345,22 @@ static int os_date (lua_State *L) {
}
static int os_time_aux (lua_State *L, time_t t, int err) {
if (err || t != (time_t)(l_timet)t)
return luaL_error(L,
"time result cannot be represented in this installation");
l_pushtime(L, t);
return 1;
}
static int os_time (lua_State *L) {
time_t t;
if (lua_isnoneornil(L, 1)) /* called without args? */
t = time(NULL); /* get current time */
if (lua_isnoneornil(L, 1)) { /* called without args? */
time_t t = time(NULL); /* get current time; error if it is -1 */
return os_time_aux(L, t, (t == (time_t)(-1)));
}
else {
time_t t;
struct tm ts;
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1); /* make sure table is at the top */
@@ -360,14 +371,12 @@ static int os_time (lua_State *L) {
ts.tm_min = getfield(L, "min", 0, 0);
ts.tm_sec = getfield(L, "sec", 0, 0);
ts.tm_isdst = getboolfield(L, "isdst");
ts.tm_wday = -1; /* if call succeeds, it will set this value */
t = mktime(&ts);
setallfields(L, &ts); /* update fields with normalized values */
/* error if tm_wday was not set */
return os_time_aux(L, t, ts.tm_wday == -1);
}
if (t != (time_t)(l_timet)t || t == (time_t)(-1))
return luaL_error(L,
"time result cannot be represented in this installation");
l_pushtime(L, t);
return 1;
}

View File

@@ -854,6 +854,13 @@ local function checkDateTable (t)
_G.D = nil
end
do -- testing mktime returning -1
local t = os.date("*t", -1)
assert(os.time(t) == -1)
end
checkDateTable(os.time())
if not _port then
-- assume that time_t can represent these values