Make WordPress Core

Opened 12 months ago

Last modified 2 months ago

#61468 new defect (bug)

DivisionByZero issue for comments on PHP8.0+

Reported by: hideandgeek404's profile hideandgeek404 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version:
Component: Comments Keywords: has-patch dev-feedback
Focuses: Cc:

Description

Quick summary

I have found an issue with PHP 8.0+ where if the comments_per_page option is set to 0, which really shouldn't be possible, PHP 8.0+ fires a DivisionByZero exception. This means that any blog post with the comments section and form showing then hits a fatal error.

Steps to reproduce

  1. Set the site's PHP version to 8.0+
  2. Visit Settings > Discussion and set the Break comments into pages with x option to 0.
  3. Save changes
  4. Visit a blog post with comments active
  5. See the blank post and fatal error in the logs

A clear and concise description of what you expected to happen.

The blog post should load and show any existing comments above the comment form.

What actually happened

When comments are activated on blog posts, loading the blog post in the front end causes a blank screen and a fatal error in the logs.

PHP Fatal error:  Uncaught DivisionByZeroError: Division by zero in /wordpress/core/6.5.4/wp-includes/comment-template.php:1520

In wp-includes/comment-template.php on line 1520 we find this code:

$comment_args['offset'] = ( (int) ceil( $top_level_count / $per_page ) - 1 ) * $per_page;

If the comments_per_page option is always going to be used for this math, it should not be possible to set it to zero in Settings > Discussion.

Change History (10)

This ticket was mentioned in PR #6862 on WordPress/wordpress-develop by @Presskopp.


12 months ago
#1

  • Keywords has-patch added

In my opinion it makes to sense to be able to set the minimum comments per page to zero, while having the pagination option enabled.

Trac ticket: #61468

#3 @narenin
12 months ago

  • Keywords dev-feedback added

Hi @hideandgeek404

In my opinion we can handle the case where $per_page is 0 by adding a check to ensure that it doesn't perform any division by zero or other invalid operations. I have added patch that can handle this scenario.

https://github.com/WordPress/wordpress-develop/pull/6863

Last edited 12 months ago by narenin (previous) (diff)

#4 @Presskopp
12 months ago

btw: I switched to PHP7 and the issue remained..

#5 @hellofromTonya
11 months ago

  • Version trunk deleted

Removing trunk as the Version that introduced this issue. Why? I'm not identifying any commits that happened during the current 6.6 or 6.7-alpha cycles. Once (or if) the commit is found, the Version should be updated.

#6 @burhi
2 months ago

Hello,

is there any progress on this issue?

Is there a solution?

https://wordpress.org/support/topic/php-8-x-missing-sidebar-comments-template-and-footer/

#8 @burhi
2 months ago

Thanks @Presskopp The codes are not compatible, I couldn't find it. The current (6.7.2) options-discussion.php file contains the following codes;

	__( 'Hold a comment in the queue if it contains %s or more links. (A common characteristic of comment spam is a large number of hyperlinks.)' ),
	'<input name="comment_max_links" type="number" step="1" min="0" id="comment_max_links" value="' . esc_attr( get_option( 'comment_max_links' ) ) . '" class="small-text" />'

#9 @burhi
2 months ago

@Presskopp Thanks a lot, I tried it thanks to your tip. I changed the Discussion Settings. "Top level comments per page" was set to 0, I changed it to 10

Problem solved.

#10 @Presskopp
2 months ago

I think the patch provided by @narenin is exactly what we need to solve this issue. In case of being 0 the division will be skipped and the value of $comment_args['offset'] will directly be set to 0.

Last edited 2 months ago by Presskopp (previous) (diff)
Note: See TracTickets for help on using tickets.