Skip to content
Myr

Fix strftime/1 and strflocaltime/1 to allow an empty result (#3670)

Landed as 6b53f80

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.

authored by Doha Nam · committed by the platform · parent 80fc7a1 · Browse files · 3 files+37 added−10 removed
This landing

State

Landed as 6b53f80

By oscabriel

Diff stats

3 files+37 added−10 removed

  • tests:+25 added−0 removed
  • other:+12 added−10 removed

3 files+37 added−10 removed

modified:src/builtin.c+12 added-10 removed
1847 unmodified lines
1848 struct tm tm;1849 if (!jv2tm(a, &tm, 0))1850 return ret_error(b, jv_string("strftime/1 requires parsed datetime inputs"));18511852 /* Prepend a character so that strftime() returning 0 means the result1853 * did not fit, even if the format expands to nothing; it is trimmed below. */1854 b = jv_string_concat(jv_string(" "), b);1855 const char *fmt = jv_string_value(b);1853 int fmt_not_empty = *fmt != '\0';1856 size_t max_size = strlen(fmt) + 100;1857 char *buf = jv_mem_alloc(max_size);1858#if defined(__APPLE__) || defined(__sun)1859 /* Apple Libc (as of version 1669.40.2) contains a bug which causes it to
25 unmodified lines
1885#if defined(__sun)1886 tzset();1887#endif1888 jv_free(b);1887 /* POSIX doesn't provide errno values for strftime() failures; weird */1888 if ((n == 0 && fmt_not_empty) || n > max_size) {1889 if (n == 0 || n > max_size) {1890 free(buf);1890 return jv_invalid_with_msg(jv_string("strftime/1: unknown system failure"));1891 return jv_invalid_with_msg(jv_string("strftime/1: result too long"));1892 }1892 jv ret = jv_string_sized(buf, n);1893 jv ret = jv_string_sized(buf + 1, n - 1);1894 free(buf);1895 return ret;1896}1897#else
19 unmodified lines
1917 return ret_error2(a, b, jv_string("strflocaltime/1 requires a string format"));1918 struct tm tm;1919 if (!jv2tm(a, &tm, 1))1920 return ret_error(b, jv_string("strflocaltime/1 requires parsed datetime inputs"));1921 /* Prepend a character so that strftime() returning 0 means the result1922 * did not fit, even if the format expands to nothing; it is trimmed below. */1923 b = jv_string_concat(jv_string(" "), b);1924 const char *fmt = jv_string_value(b);1921 int fmt_not_empty = *fmt != '\0';1925 size_t max_size = strlen(fmt) + 100;1926 char *buf = jv_mem_alloc(max_size);1927 size_t n = strftime(buf, max_size, fmt, &tm);1928 jv_free(b);1926 /* POSIX doesn't provide errno values for strftime() failures; weird */1927 if ((n == 0 && fmt_not_empty) || n > max_size) {1929 if (n == 0 || n > max_size) {1930 free(buf);1929 return jv_invalid_with_msg(jv_string("strflocaltime/1: unknown system failure"));1931 return jv_invalid_with_msg(jv_string("strflocaltime/1: result too long"));1932 }1931 jv ret = jv_string_sized(buf, n);1933 jv ret = jv_string_sized(buf + 1, n - 1);1934 free(buf);1935 return ret;1936}1937#else
239 unmodified lines
modified:tests/jq.test+16 added
1943 unmodified lines
1944try ["OK", strflocaltime({})] catch ["KO", .]194501946["KO","strflocaltime/1 requires a string format"]19471948[strftime("", " %Y ")]194901950["", " 1970 "]19511952try strftime("%c" * 10) catch .195301954"strftime/1: result too long"19551956[strflocaltime("", " %Y ")]1957[2015,2,5,23,51,47,4,63]1958["", " 2015 "]19591960try strflocaltime("%c" * 10) catch .1961[2015,2,5,23,51,47,4,63]1962"strflocaltime/1: result too long"19631964[strptime("%Y-%m-%dT%H:%M:%SZ")|(.,mktime)]1965"2015-03-05T23:51:47Z"1966[[2015,2,5,23,51,47,4,63],1425599507]1967
879 unmodified lines
modified:tests/shtest+9 added
777 unmodified lines
778 fi779 fi780fi781782# strflocaltime/1 allows a format that expands to an empty string783l=fr_FR.UTF-8784if ! $msys && ! $mingw && [ -z "$(LC_ALL=$l date +%p 2>/dev/null)" ]; then785 if ! out=$(LC_ALL=$l $VALGRIND $Q $JQ -n '0 | strflocaltime("%p")' 2>&1) || [ "$out" != '""' ]; then786 echo "strflocaltime(\"%p\") in $l should output an empty string but got: $out"787 exit 1788 fi789fi790791# Test syntax error messages of multiline filter (we can't test in jq.test)792if $VALGRIND $Q $JQ -n '[793 try if .794 then 1
222 unmodified lines