Skip to content

Commit

Permalink
Fixed potential stack overflow when calculating changes to DtStart, D…
Browse files Browse the repository at this point in the history
…tEnd, etc.
  • Loading branch information
Carbonitex committed Nov 8, 2019
1 parent 4b86b46 commit 4543db4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 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

0 comments on commit 4543db4

Please sign in to comment.