Skip to content

Commit

Permalink
Don't use foreach if the loop body modifies the iteration variable
Browse files Browse the repository at this point in the history
This fixes a segfault in compiling vibe.d.
  • Loading branch information
schuetzm committed Aug 30, 2015
1 parent 898f608 commit 442baf7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/statement.d
Expand Up @@ -4918,7 +4918,7 @@ public:
*/
if (!(_body.blockExit(sc.func, false) & BEthrow) && ClassDeclaration.exception)
{
foreach (i; 0 .. catches.dim)
foreach_reverse (i; 0 .. catches.dim)
{
Catch c = (*catches)[i];
/* If catch exception type is derived from Exception
Expand All @@ -4927,7 +4927,6 @@ public:
{
// Remove c from the array of catches
catches.remove(i);
--i;
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/compilable/exception.d
@@ -0,0 +1,18 @@
class E2 : Exception { this() { super(null); } }
class E3 : Exception { this() { super(null); } }

void main()
{
try
{
}
catch (E3)
{
}
catch (E2)
{
}
catch (Exception)
{
}
}

0 comments on commit 442baf7

Please sign in to comment.