Skip to content

Commit 45e9619

Browse files
Mike Palltmr-g
authored andcommitted
Fix corner case of os.time().
Thanks to Temir Galeev. LuaJIT#1470 (cherry picked from commit 72d2061) This patch fixes invalid os.time(t) behavior, when it handles -1 time value as a fail of the libc mktime() regardless of the errno value. Temir Galeev: * added the description and the test for the patch Part of tarantool/tarantool#12480
1 parent 24f5227 commit 45e9619

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/lib_os.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ LJLIB_CF(os_time)
242242
ts.tm_mon = getfield(L, "month", -1) - 1;
243243
ts.tm_year = getfield(L, "year", -1) - 1900;
244244
ts.tm_isdst = getboolfield(L, "isdst");
245+
errno = 0;
245246
t = mktime(&ts);
246247
}
247-
if (t == (time_t)(-1))
248+
if (t == (time_t)(-1) && errno != 0)
248249
lua_pushnil(L);
249250
else
250251
lua_pushnumber(L, (lua_Number)t);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local tap = require('tap')
2+
3+
-- The test file demonstrates os.time() fail to return -1 time
4+
-- value.
5+
-- See also: https://github.com/LuaJIT/LuaJIT/issues/1470.
6+
local test = tap.test('lj-1470-os-time-epoch-minus-1s')
7+
8+
test:plan(1)
9+
10+
local minus_1s_time = os.date("*t", -1)
11+
test:is(os.time(minus_1s_time), -1, 'correct os.time()')
12+
13+
test:done(true)

0 commit comments

Comments
 (0)