Skip to content

fix: signed overflow parsing malformed date and time input - #1862

Open
Keerthana-64 wants to merge 1 commit into
psycopg:masterfrom
Keerthana-64:parse-datetime-overflow
Open

Keerthana-64 wants to merge 1 commit into
psycopg:masterfrom
Keerthana-64:parse-datetime-overflow

Conversation

@Keerthana-64

Copy link
Copy Markdown

typecast_parse_date() / typecast_parse_time() from master, built standalone with clang and run on two malformed values:

D 4294969296-01-01      n=3 y=2000 m=1 d=1
T 10:00:00+4294967297   n=5 tz=3600
(with -fsanitize=signed-integer-overflow -fsanitize-trap: Illegal instruction at the first one)

Each field is accumulated in an int with no bound, and the offset 3600 * tzhh + 60 * tzmm + tzss is computed in int too. So a malformed value wraps into a valid one and is returned silently: DATE('4294969296-01-01') gives date(2000, 1, 1) and the timestamp/time offset becomes +01:00, instead of raising. As with the recent bytea and int64 fixes, getting such a value into the client needs a malicious server. The interval parser already raises in this case (test_interval_overflow: "I want an error, not a random number").

The parsers now return a parse error once a field reaches 10 digits (no valid field gets close), so callers raise DataError. The offset is computed in PY_LONG_LONG and rejected if it doesn't fit an int. Valid input is unaffected: the same harness gives identical results before and after for the values in test_dates.py plus extremes (294276-12-31, +15:59:59, 24:00:00), and the trap build no longer fires. I couldn't run make check here (no libpq/PostgreSQL on this machine), so the new test_parse_overflow still needs a CI run.

typecast_parse_date() and typecast_parse_time() accumulated each field
in an int without overflow check, and the time zone offset was computed
in int too. A field with too many digits overflowed and could silently
wrap to a valid value: '4294969296-01-01' was parsed as 2000-01-01.

Reject such fields instead, as the interval parser already does.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant