Fix strftime/1 and strflocaltime/1 to allow an empty result (#3670)
These functions failed with "unknown system failure" whenever strftime()
returned 0 for a non-empty format, but 0 also means the format expands to
an empty string, as %p does in a locale without AM/PM.
Prepend a character to the format and trim it from the result, so that 0
can only mean the result is too long. Now 0 | strflocaltime("%p") gives
"" in fr_FR.UTF-8, and a result that is still too long fails with
"result too long" error instead. The size limit is unchanged.
This commit is contained in:
1 parent
80fc7a169d
commit
6b53f80311
3 files changed
+37
-10
No files matched your search
@@ -1945,6 +1945,22 @@ try ["OK", strflocaltime({})] catch ["KO", .]
|
||||
0
|
||||
["KO","strflocaltime/1 requires a string format"]
|
||||
|
||||
[strftime("", " %Y ")]
|
||||
0
|
||||
["", " 1970 "]
|
||||
|
||||
try strftime("%c" * 10) catch .
|
||||
0
|
||||
"strftime/1: result too long"
|
||||
|
||||
[strflocaltime("", " %Y ")]
|
||||
[2015,2,5,23,51,47,4,63]
|
||||
["", " 2015 "]
|
||||
|
||||
try strflocaltime("%c" * 10) catch .
|
||||
[2015,2,5,23,51,47,4,63]
|
||||
"strflocaltime/1: result too long"
|
||||
|
||||
[strptime("%Y-%m-%dT%H:%M:%SZ")|(.,mktime)]
|
||||
"2015-03-05T23:51:47Z"
|
||||
[[2015,2,5,23,51,47,4,63],1425599507]
|
||||
|
||||
@@ -779,6 +779,15 @@ EOF
|
||||
fi
|
||||
fi
|
||||
|
||||
# strflocaltime/1 allows a format that expands to an empty string
|
||||
l=fr_FR.UTF-8
|
||||
if ! $msys && ! $mingw && [ -z "$(LC_ALL=$l date +%p 2>/dev/null)" ]; then
|
||||
if ! out=$(LC_ALL=$l $VALGRIND $Q $JQ -n '0 | strflocaltime("%p")' 2>&1) || [ "$out" != '""' ]; then
|
||||
echo "strflocaltime(\"%p\") in $l should output an empty string but got: $out"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Test syntax error messages of multiline filter (we can't test in jq.test)
|
||||
if $VALGRIND $Q $JQ -n '[
|
||||
try if .
|
||||
|
||||
Reference in new issue
Block a user