-
Notifications
You must be signed in to change notification settings - Fork 590
Fixing datetime arithmetic overflow/underflow. #5781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
v-isyamauchi-gh
wants to merge
16
commits into
main
Choose a base branch
from
personal/v-isyamauchi/181592
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b92edaa
Fixing datetime arithmetic overflow/underflow.
v-isyamauchi-gh 01dc4de
Addressing reviewer's comments.
v-isyamauchi-gh c2315f6
Addressing more comments.
v-isyamauchi-gh fe905dd
Addressing more comments 2.
v-isyamauchi-gh a776e52
Merge branch 'main' into personal/v-isyamauchi/181592
v-isyamauchi-gh ec1ab52
Addressing more comments 3
v-isyamauchi-gh fe822c5
Refactor DateTime arithmetic to try-catch for robustness
v-isyamauchi-gh fe0a471
Revert "Refactor DateTime arithmetic to try-catch for robustness"
v-isyamauchi-gh a954c15
Changing to a try-catch approach.
v-isyamauchi-gh ad09ea4
Merge branch 'main' into personal/v-isyamauchi/181592
v-isyamauchi-gh d6cb34f
Adding 2 additional parameters to the safe methods.
v-isyamauchi-gh d465110
Addressing reviewers comments 4
v-isyamauchi-gh 6b28e15
Reverting last change
v-isyamauchi-gh d4ae9f2
Merge branch 'main' into personal/v-isyamauchi/181592
v-isyamauchi-gh 104b146
Addressing comments 5.
v-isyamauchi-gh 0a2bcee
Merge branch 'main' into personal/v-isyamauchi/181592
v-isyamauchi-gh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletions
230
src/Microsoft.Health.Fhir.Core.UnitTests/Extensions/DateTimeSafeExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using Microsoft.Health.Fhir.Core.Extensions; | ||
| using Microsoft.Health.Fhir.Tests.Common; | ||
| using Microsoft.Health.Test.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Health.Fhir.Core.UnitTests.Extensions | ||
| { | ||
| [Trait(Traits.OwningTeam, OwningTeam.Fhir)] | ||
| [Trait(Traits.Category, Categories.Search)] | ||
| public class DateTimeSafeExtensionsTests | ||
| { | ||
| // SafeAddTicks - DateTime | ||
|
|
||
| [Fact] | ||
| public void GivenNormalDateTime_WhenSafeAddTicksCalled_ThenReturnsExpectedResult() | ||
| { | ||
| var dt = new DateTime(2024, 6, 15, 12, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddTicks(TimeSpan.TicksPerMillisecond); | ||
| Assert.Equal(dt.AddTicks(TimeSpan.TicksPerMillisecond), result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeNearMaxValue_WhenSafeAddTicksPositive_ThenClampsToMaxValue() | ||
| { | ||
| var dt = DateTime.MaxValue.AddTicks(-100); | ||
| DateTime result = dt.SafeAddTicks(TimeSpan.TicksPerMillisecond); | ||
| Assert.Equal(DateTime.MaxValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeMaxValue_WhenSafeAddTicksPositive_ThenClampsToMaxValue() | ||
| { | ||
| DateTime result = DateTime.MaxValue.SafeAddTicks(1); | ||
| Assert.Equal(DateTime.MaxValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeNearMinValue_WhenSafeAddTicksNegative_ThenClampsToMinValue() | ||
| { | ||
| var dt = DateTime.MinValue.AddTicks(100); | ||
| DateTime result = dt.SafeAddTicks(-TimeSpan.TicksPerMillisecond); | ||
| Assert.Equal(DateTime.MinValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeMinValue_WhenSafeAddTicksNegative_ThenClampsToMinValue() | ||
| { | ||
| DateTime result = DateTime.MinValue.SafeAddTicks(-1); | ||
| Assert.Equal(DateTime.MinValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTime_WhenSafeAddTicksZero_ThenReturnsOriginal() | ||
| { | ||
| var dt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddTicks(0); | ||
| Assert.Equal(dt, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenAnyDateTime_WhenSafeAddTicksLongMinValue_ThenClampsToMinValue() | ||
| { | ||
| var dt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddTicks(long.MinValue); | ||
| Assert.Equal(DateTime.MinValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenUtcDateTime_WhenSafeAddTicksClampsToMaxValue_ThenPreservesKind() | ||
| { | ||
| var dt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = DateTime.MaxValue.AddTicks(-1).SafeAddTicks(TimeSpan.TicksPerDay); | ||
| Assert.Equal(DateTimeKind.Unspecified, result.Kind); | ||
| Assert.Equal(DateTime.MaxValue.Ticks, result.Ticks); | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public void GivenLocalDateTime_WhenSafeAddTicksClampsToMinValue_ThenPreservesKind() | ||
| { | ||
| var dt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Local); | ||
| DateTime result = dt.AddTicks(1).SafeAddTicks(-TimeSpan.TicksPerDay); | ||
| Assert.Equal(DateTimeKind.Local, result.Kind); | ||
| Assert.Equal(DateTime.MinValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| // SafeAddTicks - DateTimeOffset | ||
|
|
||
| [Fact] | ||
| public void GivenNormalDateTimeOffset_WhenSafeAddTicksCalled_ThenReturnsExpectedResult() | ||
| { | ||
| var dto = new DateTimeOffset(2024, 6, 15, 12, 0, 0, TimeSpan.Zero); | ||
| DateTimeOffset result = dto.SafeAddTicks(TimeSpan.TicksPerMillisecond); | ||
| Assert.Equal(dto.AddTicks(TimeSpan.TicksPerMillisecond), result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffsetNearMaxValue_WhenSafeAddTicksPositive_ThenClampsToMaxValue() | ||
| { | ||
| var dto = DateTimeOffset.MaxValue.AddTicks(-100); | ||
| DateTimeOffset result = dto.SafeAddTicks(TimeSpan.TicksPerMillisecond); | ||
| Assert.Equal(DateTimeOffset.MaxValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffsetNearMinValue_WhenSafeAddTicksNegative_ThenClampsToMinValue() | ||
| { | ||
| var dto = DateTimeOffset.MinValue.AddTicks(100); | ||
| DateTimeOffset result = dto.SafeAddTicks(-TimeSpan.TicksPerMillisecond); | ||
| Assert.Equal(DateTimeOffset.MinValue, result); | ||
| } | ||
|
|
||
| // SafeAddDays - DateTime | ||
|
|
||
| [Fact] | ||
| public void GivenNormalDateTime_WhenSafeAddDaysCalled_ThenReturnsExpectedResult() | ||
| { | ||
| var dt = new DateTime(2024, 6, 15, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddDays(1); | ||
| Assert.Equal(dt.AddDays(1), result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeNearMaxValue_WhenSafeAddDaysPositive_ThenClampsToMaxValue() | ||
| { | ||
| var dt = DateTime.MaxValue.AddDays(-0.5); | ||
| DateTime result = dt.SafeAddDays(1); | ||
| Assert.Equal(DateTime.MaxValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeNearMinValue_WhenSafeAddDaysNegative_ThenClampsToMinValue() | ||
| { | ||
| var dt = DateTime.MinValue.AddDays(0.5); | ||
| DateTime result = dt.SafeAddDays(-1); | ||
| Assert.Equal(DateTime.MinValue, result); | ||
| } | ||
|
|
||
| // SafeAddDays - DateTimeOffset | ||
|
|
||
| [Fact] | ||
| public void GivenNormalDateTimeOffset_WhenSafeAddDaysCalled_ThenReturnsExpectedResult() | ||
| { | ||
| var dto = new DateTimeOffset(2024, 6, 15, 0, 0, 0, TimeSpan.Zero); | ||
| DateTimeOffset result = dto.SafeAddDays(1); | ||
| Assert.Equal(dto.AddDays(1), result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffsetNearMaxValue_WhenSafeAddDaysPositive_ThenClampsToMaxValue() | ||
| { | ||
| var dto = DateTimeOffset.MaxValue.AddDays(-0.5); | ||
| DateTimeOffset result = dto.SafeAddDays(1); | ||
| Assert.Equal(DateTimeOffset.MaxValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffsetNearMinValue_WhenSafeAddDaysNegative_ThenClampsToMinValue() | ||
| { | ||
| var dto = DateTimeOffset.MinValue.AddDays(0.5); | ||
| DateTimeOffset result = dto.SafeAddDays(-1); | ||
| Assert.Equal(DateTimeOffset.MinValue, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTime_WhenSafeAddDaysIntMaxValue_ThenClampsToMaxValue() | ||
| { | ||
| var dt = new DateTime(2024, 6, 15, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddDays(int.MaxValue); | ||
| Assert.Equal(DateTime.MaxValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTime_WhenSafeAddDaysIntMinValue_ThenClampsToMinValue() | ||
| { | ||
| var dt = new DateTime(2024, 6, 15, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddDays(int.MinValue); | ||
| Assert.Equal(DateTime.MinValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenUtcDateTime_WhenSafeAddDaysClampsToMaxValue_ThenPreservesKind() | ||
| { | ||
| var dt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
| DateTime result = dt.SafeAddDays(int.MaxValue); | ||
| Assert.Equal(DateTimeKind.Utc, result.Kind); | ||
| Assert.Equal(DateTime.MaxValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenLocalDateTime_WhenSafeAddDaysClampsToMinValue_ThenPreservesKind() | ||
| { | ||
| var dt = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Local); | ||
| DateTime result = dt.SafeAddDays(int.MinValue); | ||
| Assert.Equal(DateTimeKind.Local, result.Kind); | ||
| Assert.Equal(DateTime.MinValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffset_WhenSafeAddDaysIntMaxValue_ThenClampsToMaxValue() | ||
| { | ||
| var dto = new DateTimeOffset(2024, 6, 15, 0, 0, 0, TimeSpan.Zero); | ||
| DateTimeOffset result = dto.SafeAddDays(int.MaxValue); | ||
| Assert.Equal(DateTimeOffset.MaxValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffset_WhenSafeAddDaysIntMinValue_ThenClampsToMinValue() | ||
| { | ||
| var dto = new DateTimeOffset(2024, 6, 15, 0, 0, 0, TimeSpan.Zero); | ||
| DateTimeOffset result = dto.SafeAddDays(int.MinValue); | ||
| Assert.Equal(DateTimeOffset.MinValue.Ticks, result.Ticks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenDateTimeOffset_WhenSafeAddDaysClampsToMaxValue_ThenPreservesOffset() | ||
| { | ||
| var offset = TimeSpan.FromHours(5); | ||
| var dto = new DateTimeOffset(2024, 1, 1, 0, 0, 0, offset); | ||
| DateTimeOffset result = dto.SafeAddDays(int.MaxValue); | ||
| Assert.Equal(offset, result.Offset); | ||
| Assert.Equal(DateTimeOffset.MaxValue.Ticks, result.Ticks); | ||
| } | ||
| } | ||
| } | ||
|
v-isyamauchi-gh marked this conversation as resolved.
v-isyamauchi-gh marked this conversation as resolved.
|
||
101 changes: 101 additions & 0 deletions
101
src/Microsoft.Health.Fhir.Core/Extensions/DateTimeSafeExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Health.Fhir.Core.Extensions | ||
| { | ||
| /// <summary> | ||
| /// Extension methods for <see cref="DateTime"/> and <see cref="DateTimeOffset"/> that clamp | ||
| /// results to <see cref="DateTime.MinValue"/>/<see cref="DateTime.MaxValue"/> instead of | ||
| /// throwing on overflow. | ||
| /// </summary> | ||
| public static class DateTimeSafeExtensions | ||
| { | ||
| private const long MaxDaysBeforeTicksOverflow = long.MaxValue / TimeSpan.TicksPerDay; | ||
|
|
||
| /// <summary> | ||
| /// Adds the specified number of ticks to a <see cref="DateTime"/>, clamping the result | ||
| /// to <see cref="DateTime.MinValue"/> or <see cref="DateTime.MaxValue"/> on overflow. | ||
| /// </summary> | ||
| public static DateTime SafeAddTicks(this DateTime value, long ticks) | ||
| { | ||
| if (ticks == long.MinValue) | ||
| { | ||
| // Can't negate long.MinValue; adding it always underflows within DateTime's range. | ||
| return new DateTime(DateTime.MinValue.Ticks, value.Kind); | ||
| } | ||
|
|
||
| if (ticks > 0 && value.Ticks > DateTime.MaxValue.Ticks - ticks) | ||
| { | ||
| return new DateTime(DateTime.MaxValue.Ticks, value.Kind); | ||
| } | ||
|
|
||
| if (ticks < 0 && value.Ticks < DateTime.MinValue.Ticks - ticks) | ||
| { | ||
| return new DateTime(DateTime.MinValue.Ticks, value.Kind); | ||
| } | ||
|
|
||
| return value.AddTicks(ticks); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds the specified number of ticks to a <see cref="DateTimeOffset"/>, clamping the result | ||
| /// to <see cref="DateTimeOffset.MinValue"/> or <see cref="DateTimeOffset.MaxValue"/> on overflow. | ||
| /// </summary> | ||
|
Copilot marked this conversation as resolved.
|
||
| public static DateTimeOffset SafeAddTicks(this DateTimeOffset value, long ticks) | ||
| { | ||
| if (ticks == long.MinValue) | ||
| { | ||
| // Can't negate long.MinValue; adding it always underflows within DateTimeOffset's range. | ||
| return DateTimeOffset.MinValue; | ||
| } | ||
|
|
||
| if (ticks > 0 && value.Ticks > DateTimeOffset.MaxValue.Ticks - ticks) | ||
| { | ||
| return DateTimeOffset.MaxValue; | ||
| } | ||
|
|
||
| if (ticks < 0 && value.Ticks < DateTimeOffset.MinValue.Ticks - ticks) | ||
| { | ||
| return DateTimeOffset.MinValue; | ||
| } | ||
|
|
||
| return value.AddTicks(ticks); | ||
| } | ||
|
v-isyamauchi-gh marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Adds the specified number of days to a <see cref="DateTime"/>, clamping the result | ||
| /// to <see cref="DateTime.MinValue"/> or <see cref="DateTime.MaxValue"/> on overflow. | ||
| /// </summary> | ||
| public static DateTime SafeAddDays(this DateTime value, int days) | ||
| { | ||
| // Detect if days * TimeSpan.TicksPerDay would overflow long. | ||
| if (days > MaxDaysBeforeTicksOverflow || days < -MaxDaysBeforeTicksOverflow) | ||
| { | ||
| return days > 0 ? new DateTime(DateTime.MaxValue.Ticks, value.Kind) : new DateTime(DateTime.MinValue.Ticks, value.Kind); | ||
| } | ||
|
|
||
| long ticks = days * TimeSpan.TicksPerDay; | ||
| return value.SafeAddTicks(ticks); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds the specified number of days to a <see cref="DateTimeOffset"/>, clamping the result | ||
| /// to <see cref="DateTimeOffset.MinValue"/> or <see cref="DateTimeOffset.MaxValue"/> on overflow. | ||
| /// </summary> | ||
| public static DateTimeOffset SafeAddDays(this DateTimeOffset value, int days) | ||
| { | ||
| // Detect if days * TimeSpan.TicksPerDay would overflow long. | ||
| if (days > MaxDaysBeforeTicksOverflow || days < -MaxDaysBeforeTicksOverflow) | ||
| { | ||
| return days > 0 ? new DateTimeOffset(DateTimeOffset.MaxValue.Ticks, value.Offset) : new DateTimeOffset(DateTimeOffset.MinValue.Ticks, value.Offset); | ||
| } | ||
|
|
||
| long ticks = days * TimeSpan.TicksPerDay; | ||
| return value.SafeAddTicks(ticks); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.