Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 56688dd

Browse filesBrowse files
committed
Fix not throwing due to overflow while writing NodaTime's period (#5894)
Fixes #5893 (cherry picked from commit e6c166b)
1 parent b977012 commit 56688dd
Copy full SHA for 56688dd

File tree

Expand file treeCollapse file tree

4 files changed

+74
-20
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+74
-20
lines changed

‎src/Npgsql.NodaTime/Internal/PeriodConverter.cs

Copy file name to clipboardExpand all lines: src/Npgsql.NodaTime/Internal/PeriodConverter.cs
+18-8Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ protected override void WriteCore(PgWriter writer, Period value)
3535
{
3636
// We have to normalize the value as otherwise we might get a value with 0 everything except for ticks, which we ignore
3737
value = value.Normalize();
38-
// Note that the end result must be long
39-
// see #3438
40-
var microsecondsInDay =
41-
(((value.Hours * NodaConstants.MinutesPerHour + value.Minutes) * NodaConstants.SecondsPerMinute + value.Seconds) * NodaConstants.MillisecondsPerSecond + value.Milliseconds) * 1000 +
42-
value.Nanoseconds / 1000; // Take the microseconds, discard the nanosecond remainder
4338

44-
writer.WriteInt64(microsecondsInDay);
45-
writer.WriteInt32(value.Weeks * 7 + value.Days); // days
46-
writer.WriteInt32(value.Years * 12 + value.Months); // months
39+
try
40+
{
41+
checked
42+
{
43+
// Note that the end result must be long
44+
// see #3438
45+
var microsecondsInDay =
46+
(((value.Hours * NodaConstants.MinutesPerHour + value.Minutes) * NodaConstants.SecondsPerMinute + value.Seconds) * NodaConstants.MillisecondsPerSecond + value.Milliseconds) * 1000 +
47+
value.Nanoseconds / 1000; // Take the microseconds, discard the nanosecond remainder
48+
writer.WriteInt64(microsecondsInDay);
49+
writer.WriteInt32(value.Weeks * 7 + value.Days); // days
50+
writer.WriteInt32(value.Years * 12 + value.Months); // months
51+
}
52+
}
53+
catch (OverflowException ex)
54+
{
55+
throw new ArgumentException(NpgsqlNodaTimeStrings.CannotWritePeriodDueToOverflow, ex);
56+
}
4757
}
4858
}

‎src/Npgsql.NodaTime/Properties/NpgsqlNodaTimeStrings.Designer.cs

Copy file name to clipboardExpand all lines: src/Npgsql.NodaTime/Properties/NpgsqlNodaTimeStrings.Designer.cs
+41-12Lines changed: 41 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Npgsql.NodaTime/Properties/NpgsqlNodaTimeStrings.resx

Copy file name to clipboardExpand all lines: src/Npgsql.NodaTime/Properties/NpgsqlNodaTimeStrings.resx
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@
2424
<data name="CannotReadIntervalWithMonthsAsDuration" xml:space="preserve">
2525
<value>Cannot read PostgreSQL interval with non-zero months to NodaTime Duration. Try reading as a NodaTime Period instead.</value>
2626
</data>
27+
<data name="CannotWritePeriodDueToOverflow" xml:space="preserve">
28+
<value>Cannot write NodaTime's Period because it's out of range for the PG interval type.</value>
29+
</data>
2730
</root>

‎test/Npgsql.PluginTests/NodaTimeTests.cs

Copy file name to clipboardExpand all lines: test/Npgsql.PluginTests/NodaTimeTests.cs
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,18 @@ public async Task Normalize_period_on_write()
799799
Assert.That(dbValue, Is.EqualTo(expectedAfterRoundtrip));
800800
}
801801

802+
[Test]
803+
public async Task Period_write_throw_on_overflow()
804+
{
805+
var periodBuilder = new PeriodBuilder
806+
{
807+
Years = int.MaxValue
808+
};
809+
var ex = await AssertTypeUnsupportedWrite<Period, ArgumentException>(periodBuilder.Build(), "interval");
810+
Assert.That(ex.Message, Is.EqualTo(NpgsqlNodaTimeStrings.CannotWritePeriodDueToOverflow));
811+
Assert.That(ex.InnerException, Is.TypeOf<OverflowException>());
812+
}
813+
802814
#endregion Interval
803815

804816
#region Support

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.