gmtime/localtime/strftime abort (UBSan / float-cast-overflow) on out-of-range or non-finite input
jq 1.8.2, built with UBSan (clang, -fsanitize=undefined). f_gmtime (src/builtin.c:1754) and f_localtime (src/builtin.c:1789) convert the caller's JSON number to a time_t with time_t secs = fsecs;, with no check that fsecs is finite or within time_t's range. For +/-inf or a value like 1e20 this narrowing cast is undefined behaviour and the process aborts. It's also reachable through strftime, which calls gmtime internally, and a jq-level try/catch does not help because it's a hard process abort, not a jq error value.
jq -n '[try ([2020,0,"x"] | strftime("%Y")) catch ., try (1e400 | strftime("%Y")) catch ., try ([2020,0,1,0,0,0,0,0] | strftime(("%Y" * 200))) catch .]' jq -n '[try (1e20 | localtime) catch ., try (1e20 | strflocaltime("%Y")) catch .]'
src/builtin.c:1754:17: runtime error: inf is outside the range of representable values of type 'long' (f_gmtime <- f_strftime) src/builtin.c:1789:17: runtime error: 1e+20 is outside the range of representable values of type 'long' (f_localtime)
Fix: check isfinite(fsecs) and that it's within time_t's range before the cast, returning a jv_invalid error the same way the code just above already does for mktime's sentinel results. Note current master changed this to time_t secs = floor(fsecs);, which changes rounding but not the range check, so the same abort still reproduces there.
Found by replaying automatically generated tests under ASan/UBSan.
Source: jqlang/jq