Skip to content

Commit

Permalink
Merge pull request #473 from SmarterTools/extrapolatetimes-stackoverflow
Browse files Browse the repository at this point in the history
Fixed potential stack overflow when calculating changes to DtStart, DtEnd, etc.
  • Loading branch information
rianjs committed Apr 10, 2021
2 parents 3f05403 + 4543db4 commit c299986
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
23 changes: 15 additions & 8 deletions net-core/Ical.Net/CalendarComponents/CalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override IDateTime DtStart
set
{
base.DtStart = value;
ExtrapolateTimes();
ExtrapolateTimes(2);
}
}

Expand All @@ -66,7 +66,7 @@ public virtual IDateTime DtEnd
if (!Equals(DtEnd, value))
{
Properties.Set("DTEND", value);
ExtrapolateTimes();
ExtrapolateTimes(0);
}
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public virtual TimeSpan Duration
if (!Equals(Duration, value))
{
Properties.Set("DURATION", value);
ExtrapolateTimes();
ExtrapolateTimes(1);
}
}
}
Expand Down Expand Up @@ -258,20 +258,27 @@ protected override void OnDeserialized(StreamingContext context)
{
base.OnDeserialized(context);

ExtrapolateTimes();
ExtrapolateTimes(-1);
}

private void ExtrapolateTimes()
private void ExtrapolateTimes(int source)
{
if (DtEnd == null && DtStart != null && Duration != default(TimeSpan))
/*
* Source values, a fix introduced to prevent stack overflow exceptions from occuring.
* -1 = Anybody, stack overflow could maybe still occur in this case?
* 0 = End
* 1 = Duration
* 2 = DtStart
*/
if (DtEnd == null && DtStart != null && Duration != default(TimeSpan) && source != 0)
{
DtEnd = DtStart.Add(Duration);
}
else if (Duration == default(TimeSpan) && DtStart != null && DtEnd != null)
else if (Duration == default(TimeSpan) && DtStart != null && DtEnd != null && source != 1)
{
Duration = DtEnd.Subtract(DtStart);
}
else if (DtStart == null && Duration != default(TimeSpan) && DtEnd != null)
else if (DtStart == null && Duration != default(TimeSpan) && DtEnd != null && source != 2)
{
DtStart = DtEnd.Subtract(Duration);
}
Expand Down
20 changes: 13 additions & 7 deletions net-core/Ical.Net/CalendarComponents/Todo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override IDateTime DtStart
set
{
base.DtStart = value;
ExtrapolateTimes();
ExtrapolateTimes(2);
}
}

Expand All @@ -47,7 +47,7 @@ public virtual IDateTime Due
set
{
Properties.Set("DUE", value);
ExtrapolateTimes();
ExtrapolateTimes(0);
}
}

Expand All @@ -70,7 +70,7 @@ public virtual TimeSpan Duration
set
{
Properties.Set("DURATION", value);
ExtrapolateTimes();
ExtrapolateTimes(1);
}
}

Expand Down Expand Up @@ -186,17 +186,23 @@ protected override void OnDeserializing(StreamingContext context)
base.OnDeserializing(context);
}

private void ExtrapolateTimes()
private void ExtrapolateTimes(int source)
{
if (Due == null && DtStart != null && Duration != default(TimeSpan))
/*
* Source values, a fix introduced to prevent StackOverflow exceptions from occuring.
* 0 = Due
* 1 = Duration
* 2 = DtStart
*/
if (Due == null && DtStart != null && Duration != default(TimeSpan) && source != 0)
{
Due = DtStart.Add(Duration);
}
else if (Duration == default(TimeSpan) && DtStart != null && Due != null)
else if (Duration == default(TimeSpan) && DtStart != null && Due != null && source != 1)
{
Duration = Due.Subtract(DtStart);
}
else if (DtStart == null && Duration != default(TimeSpan) && Due != null)
else if (DtStart == null && Duration != default(TimeSpan) && Due != null && source != 2)
{
DtStart = Due.Subtract(Duration);
}
Expand Down
23 changes: 15 additions & 8 deletions v2/ical.NET/Components/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override IDateTime DtStart
set
{
base.DtStart = value;
ExtrapolateTimes();
ExtrapolateTimes(2);
}
}

Expand All @@ -68,7 +68,7 @@ public virtual IDateTime DtEnd
if (!Equals(DtEnd, value))
{
Properties.Set("DTEND", value);
ExtrapolateTimes();
ExtrapolateTimes(0);
}
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public virtual TimeSpan Duration
if (!Equals(Duration, value))
{
Properties.Set("DURATION", value);
ExtrapolateTimes();
ExtrapolateTimes(1);
}
}
}
Expand Down Expand Up @@ -263,20 +263,27 @@ protected override void OnDeserialized(StreamingContext context)
{
base.OnDeserialized(context);

ExtrapolateTimes();
ExtrapolateTimes(-1);
}

private void ExtrapolateTimes()
private void ExtrapolateTimes(int source)
{
if (DtEnd == null && DtStart != null && Duration != default(TimeSpan))
/*
* Source values, a fix introduced to prevent stack overflow exceptions from occuring.
* -1 = Anybody, stack overflow could maybe still occur in this case?
* 0 = End
* 1 = Duration
* 2 = DtStart
*/
if (DtEnd == null && DtStart != null && Duration != default(TimeSpan) && source != 0)
{
DtEnd = DtStart.Add(Duration);
}
else if (Duration == default(TimeSpan) && DtStart != null && DtEnd != null)
else if (Duration == default(TimeSpan) && DtStart != null && DtEnd != null && source != 1)
{
Duration = DtEnd.Subtract(DtStart);
}
else if (DtStart == null && Duration != default(TimeSpan) && DtEnd != null)
else if (DtStart == null && Duration != default(TimeSpan) && DtEnd != null && source != 2)
{
DtStart = DtEnd.Subtract(Duration);
}
Expand Down
20 changes: 13 additions & 7 deletions v2/ical.NET/Components/Todo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override IDateTime DtStart
set
{
base.DtStart = value;
ExtrapolateTimes();
ExtrapolateTimes(2);
}
}

Expand All @@ -49,7 +49,7 @@ public virtual IDateTime Due
set
{
Properties.Set("DUE", value);
ExtrapolateTimes();
ExtrapolateTimes(0);
}
}

Expand All @@ -72,7 +72,7 @@ public virtual TimeSpan Duration
set
{
Properties.Set("DURATION", value);
ExtrapolateTimes();
ExtrapolateTimes(1);
}
}

Expand Down Expand Up @@ -198,17 +198,23 @@ protected override void OnDeserializing(StreamingContext context)
base.OnDeserializing(context);
}

private void ExtrapolateTimes()
private void ExtrapolateTimes(int source)
{
if (Due == null && DtStart != null && Duration != default(TimeSpan))
/*
* Source values, a fix introduced to prevent StackOverflow exceptions from occuring.
* 0 = Due
* 1 = Duration
* 2 = DtStart
*/
if (Due == null && DtStart != null && Duration != default(TimeSpan) && source != 0)
{
Due = DtStart.Add(Duration);
}
else if (Duration == default(TimeSpan) && DtStart != null && Due != null)
else if (Duration == default(TimeSpan) && DtStart != null && Due != null && source != 1)
{
Duration = Due.Subtract(DtStart);
}
else if (DtStart == null && Duration != default(TimeSpan) && Due != null)
else if (DtStart == null && Duration != default(TimeSpan) && Due != null && source != 2)
{
DtStart = Due.Subtract(Duration);
}
Expand Down

0 comments on commit c299986

Please sign in to comment.