Report
to_date/to_timestamp/jsonpath datetime() parse Y,YYY with sscanf("%d,%03d"), so the low group may carry a sign, have fewer than three digits, or leave a fourth digit for the next field, giving silently wrong years and months (1,-23 -> 0977, 1,2345-01 -> month 5)
- Severity
- 22
- Blamed commit
- b866d2e2d79416f8497e4dffa7e800298d018f6c
On 27 branches
REL7_1_STABLE, REL7_2_STABLE, REL7_3_STABLE, REL7_4_STABLE, REL8_0_STABLE, REL8_1_STABLE, REL8_2_STABLE, REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
The input side of the Y,YYY template pattern in from_char_parse()
(formatting.c) is implemented as
sscanf(s, "%d,%03d%n", &millennia, &years, &nch)
and accepts anything sscanf accepts for the second conversion. The "%03d" width is only a maximum of three characters (a sign counts as one of them), not a requirement of three digits, so:
- a sign is accepted in the low group and is added to the millennium part: "1,-23" is read as 1000 - 23 = 977, "1,+5" as 1005;
- fewer than three digits are accepted: "1,2" is read as 1002, not rejected or read as 1200;
- a longer digit run is truncated after three characters and the rest
is left for the next template field: "1,2345-01" with
Y,YYY-MMgives year 1234 and month 5.
Neither to_char() nor the other year patterns can produce or accept
such input, and the result is a plausible-looking but wrong date
rather than an error.
Reproduction
set timezone = 'UTC';
select to_date('1,-23', 'Y,YYY'); -- 0977-01-01
select to_date('1,+5', 'Y,YYY'); -- 1005-01-01
select to_date('1,2', 'Y,YYY'); -- 1002-01-01
select to_date('1,2345-01', 'Y,YYY-MM'); -- 1234-05-01
select jsonb_path_query('"1,-23"', '$.datetime("Y,YYY")');
-- "0977-01-01"
Expected: an "invalid value ... for "Y,YYY"" error for all of these (the low group must be exactly three digits), as for "1,x" or "2020", which are rejected.
Analysis
from_char_parse() case DCH_Y_YYY calls sscanf and only checks
matched < 2; nch is used to advance the input pointer. No check is
made that the characters consumed by "%03d" are three ASCII digits.
Since sscanf also skips leading whitespace for "%d", 1, 234 is
accepted as well.
The pattern has used sscanf since the first to_char family import;
a later change (read past end of string) only added the %n pointer
advance and did not tighten the format.
A possible fix is to validate that the text after the comma starts with three digits (isdigit on each of the next three characters) before calling strtol/sscanf, or to parse the millennium part with from_char_parse_int_len() and the low group with a fixed length of three.
Notes
Tested on ca492a9197a449bca3953f7e287c9aee6d295c6d (master), assert-enabled build. Not to be confused with the to_char(interval) Y,YYY output report 01a22625-cd6c-760d-aef7-13ab6e0b5ec9 (a different direction and code path).
Branches: REL_14_STABLE through master (the same sscanf format is present on all of them; reproduced on master only).