WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 6 weeks ago

#40293 new defect (bug)

Comment template - warning

Reported by: victorfreitas Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8
Component: Comments Keywords: has-patch has-unit-tests has-screenshots
Focuses: Cc:

Description

Hi,

in the comments_template function, you are entering a divide-by-zero condition at line 1379 of the wp-includes/comment-template.php file.

The line described above is like this.

<?php
$comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
?>

I believe this will solve.

<?php
$offset = 0;

if ( $top_level_count && $per_page ) {
    $offset = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
}

$comment_args['offset'] = $offset;
?>

I'm using a default install with theme Twenty Seventeen.

Thank you!

Attachments (3)

comment-template.patch (592 bytes) - added by thamaraiselvam 8 months ago.
40293-how-to-reproduce-divide-by-zero.jpg (31.5 KB) - added by birgire 6 weeks ago.
40293.2.diff (2.3 KB) - added by birgire 6 weeks ago.

Download all attachments as: .zip

Change History (8)

#1 follow-up: @thamaraiselvam
8 months ago

  • Keywords has-patch added

Yes, It could lead to divide-by-zero condition in some scenario and @victorfreitas I have removed the variable

$offset

to reduce unnecessary memory usages because it's just one-time usage.

#2 follow-up: @johnbillion
8 months ago

  • Keywords needs-unit-tests reporter-feedback added

Thanks for the ticket @victorfreitas! Welcome to WordPress Trac.

Under what condition does this occur? Can you provide some steps to reproduce please?

Any change here will need unit tests.

#3 in reply to: ↑ 2 @victorfreitas
8 months ago

Hi @johnbillion,

Yes, on the Discussion Settings (options-discussion.php) page you need to put "Break comments into pages with" to 0 comments_per_page option.

Then just access a page or single.

Replying to johnbillion:

Thanks for the ticket @victorfreitas! Welcome to WordPress Trac.

Under what condition does this occur? Can you provide some steps to reproduce please?

Any change here will need unit tests.

#4 in reply to: ↑ 1 @victorfreitas
8 months ago

Thanks @thamaraiselvam you're right.
By default comment args offset is empty even.

Replying to thamaraiselvam:

Yes, It could lead to divide-by-zero condition in some scenario and @victorfreitas I have removed the variable

$offset

to reduce unnecessary memory usages because it's just one-time usage.

@birgire
6 weeks ago

#5 @birgire
6 weeks ago

  • Keywords has-unit-tests has-screenshots added; needs-unit-tests reporter-feedback removed

In 40293-how-to-reproduce-divide-by-zero.jpg we see the settings to reproduce the "Division by zero" PHP error in comments_template().

It's attempting to write a test to check if there's no "Division by zero" PHP error.

But there's no assertExceptionNotThrown() and there's some discussion on PHPUnit-Doc:Testing an exception is not thrown that it could be an anti-pattern.

There's a way to add a comments_template_query_args filter with an assertion to check if offset is zero, but I don't think that's the way here.

So in 40293.2.diff I try instead to display comments, with the problematic setup of:

  • default_comments_page as 'newest'
  • comments_per_page as '0'
  • page_comments as '1'

in a similar way as previous tests in Tests_Comment_CommentsTemplate.


Last edited 6 weeks ago by birgire (previous) (diff)
Note: See TracTickets for help on using tickets.