Skip to content

Commit

Permalink
Prevent infinite loops in get_comment_ancestors (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwiebe committed May 10, 2024
1 parent f6b4783 commit 8bf37f7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,18 +876,20 @@ function get_comment_ancestors( $comment ) {

$ancestors = array();

$id = $comment->comment_parent;
$id = (int) $comment->comment_parent;
$ancestors[] = $id;

// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( $ancestor = \get_comment( $id ) ) {
while ( $id > 0 ) {
$ancestor = \get_comment( $id );
$parent_id = (int) $ancestor->comment_parent;

// Loop detection: If the ancestor has been seen before, break.
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( empty( $ancestor->comment_parent ) || ( $ancestor->comment_parent == $comment->comment_ID ) || in_array( $ancestor->comment_parent, $ancestors, true ) ) {
if ( empty( $parent_id ) || ( $parent_id === (int) $comment->comment_ID ) || in_array( $parent_id, $ancestors, true ) ) {
break;
}

$id = $comment->comment_parent;
$id = $parent_id;
$ancestors[] = $id;
}

Expand Down

0 comments on commit 8bf37f7

Please sign in to comment.