Skip to content

Commit

Permalink
Fix: Crash if far too many sections
Browse files Browse the repository at this point in the history
Closes #557
  • Loading branch information
leezer3 committed Nov 18, 2020
1 parent 074dcc7 commit 32b132d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions source/RouteManager2/CurrentRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,42 @@ public CurrentRoute(BaseRenderer renderer)
/// <summary>Updates all sections within the route</summary>
public void UpdateAllSections()
{
UpdateSection(Sections.LastOrDefault());
/*
* When there are an insane amount of sections, updating via a reference chain
* may trigger a StackOverflowException
*
* Instead, pull out the reference to the next section in an out variable
* and use a while loop
* https://github.com/leezer3/OpenBVE/issues/557
*/
Section nextSectionToUpdate;
UpdateSection(Sections.LastOrDefault(), out nextSectionToUpdate);
while (nextSectionToUpdate != null)
{
UpdateSection(nextSectionToUpdate, out nextSectionToUpdate);
}
}

/// <summary>Updates the specified signal section</summary>
/// <param name="SectionIndex"></param>
public void UpdateSection(int SectionIndex)
{
UpdateSection(Sections[SectionIndex]);
Section nextSectionToUpdate;
UpdateSection(Sections[SectionIndex], out nextSectionToUpdate);
while (nextSectionToUpdate != null)
{
UpdateSection(nextSectionToUpdate, out nextSectionToUpdate);
}
}

/// <summary>Updates the specified signal section</summary>
/// <param name="Section"></param>
public void UpdateSection(Section Section)
/// <param name="PreviousSection"></param>
public void UpdateSection(Section Section, out Section PreviousSection)
{
if (Section == null)
{
PreviousSection = null;
return;
}

Expand Down Expand Up @@ -355,7 +375,7 @@ public void UpdateSection(Section Section)
Section.CurrentAspect = newAspect;

// update previous section
UpdateSection(Section.PreviousSection);
PreviousSection = Section.PreviousSection;
}

/// <summary>Updates the currently displayed background</summary>
Expand Down

0 comments on commit 32b132d

Please sign in to comment.