Skip to content

Commit

Permalink
Fixed stack overflow issues that could occur when certain values of T…
Browse files Browse the repository at this point in the history
…odo and Event are null.
  • Loading branch information
Carbonitex committed Nov 7, 2019
1 parent de891fe commit 4b86b46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
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 4b86b46

Please sign in to comment.