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) ====================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22dcd-78d8-7a74-91db-b2fe15c853c5 Filed: 2026-11-30 18:52:50 UTC Severity: 22/100 Blamed commit: b866d2e2d79416f8497e4dffa7e800298d018f6c ## 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-MM` gives 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 ```sql 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).