Visitar URL original
Throw when an interval is out of range of TimeSpan by johnkaram02 · Pull Request #6727 · npgsql/npgsql · GitHub
Skip to content

Throw when an interval is out of range of TimeSpan - #6727

Open
johnkaram02 wants to merge 1 commit into
npgsql:mainfrom
johnkaram02:interval-timespan-overflow
Open

johnkaram02 wants to merge 1 commit into
npgsql:mainfrom
johnkaram02:interval-timespan-overflow

Conversation

@johnkaram02

Copy link
Copy Markdown

Fixes #

Reading an interval as TimeSpan computed the ticks with unchecked arithmetic, so an interval outside the range of TimeSpan, such as interval '300000000 hours', silently wrapped around and returned a wrong TimeSpan with the opposite sign.

This change computes the ticks with checked arithmetic and throws an InvalidCastException when the interval doesn't fit in a TimeSpan, the same exception type the converter already throws for intervals with months. The message suggests reading the value as NpgsqlInterval or NodaTime Period.

The new Interval_out_of_TimeSpan_range_cannot_read_as_TimeSpan test covers a large positive hour count, a large negative hour count and a large day count. It fails without the fix and passes with it. I ran Npgsql.Tests.Types (828 passed, 3 skipped) against PostgreSQL 17.11 on Linux with .NET SDK 10.0.403.

Reading an interval as TimeSpan computed the ticks with unchecked
arithmetic. An interval larger than TimeSpan.MaxValue, such as
'300000000 hours', silently wrapped around and returned a wrong
TimeSpan with the opposite sign.

Compute the ticks with checked arithmetic and throw an
InvalidCastException when the interval is outside the range of
TimeSpan, as reading an interval with months already does.

This branch has not been deployed

No deployments
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