Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#53773 closed defect (bug) (fixed)

/wp-admin/includes/ajax-actions.php file line 3006 is creating divide by zero error

Reported by: 2linctools's profile 2linctools Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.8.1 Priority: normal
Severity: normal Version: 5.8
Component: Media Keywords: good-first-bug has-patch commit fixed-major
Focuses: Cc:

Description (last modified by SergeyBiryukov)

In the /wp-admin/includes/ajax-actions.php file line 3006 it keeps popping up an error in our php error log that says it is trying to divide by zero. It produces an error over and over again.

PHP Warning: Division by zero in /wp-admin/includes/ajax-actions.php on line 3006

This is line 3006:

$max_pages = ceil( $total_posts / (int) $attachments_query->query['posts_per_page'] );

Here is the whole section:

/**
	 * Filters the arguments passed to WP_Query during an Ajax
	 * call for querying attachments.
	 *
	 * @since 3.7.0
	 *
	 * @see WP_Query::parse_query()
	 *
	 * @param array $query An array of query variables.
	 */
	$query             = apply_filters( 'ajax_query_attachments_args', $query );
	$attachments_query = new WP_Query( $query );

	$posts       = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
	$posts       = array_filter( $posts );
	$total_posts = $attachments_query->found_posts;

	if ( $total_posts < 1 ) {
		// Out-of-bounds, run the query again without LIMIT for total count.
		unset( $query['paged'] );

		$count_query = new WP_Query();
		$count_query->query( $query );
		$total_posts = $count_query->found_posts;
	}

	$max_pages = ceil( $total_posts / (int) $attachments_query->query['posts_per_page'] );

	header( 'X-WP-Total: ' . (int) $total_posts );
	header( 'X-WP-TotalPages: ' . (int) $max_pages );

	wp_send_json_success( $posts );
}

Our php error log looks like this: (truncated a lot)

[23-Jul-2021 18:46:05 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 18:46:14 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 18:46:32 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 18:46:34 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 18:58:34 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 18:58:42 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 19:35:22 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 19:35:58 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006
[23-Jul-2021 19:36:01 UTC] PHP Warning:  Division by zero in  /wp-admin/includes/ajax-actions.php on line 3006

Attachments (1)

53773.diff (673 bytes) - added by kapilpaul 3 years ago.
Created patch.

Download all attachments as: .zip

Change History (8)

#1 @SergeyBiryukov
3 years ago

  • Component changed from General to Media
  • Description modified (diff)
  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 5.8.1

Hi there, welcome to WordPress Trac! Thanks for the report.

Introduced in [51145].

It looks like a plugin or theme changes the posts_per_page value to 0 using the ajax_query_attachments_args filter. By default, it's set to 40 in wp-includes/js/media-models.js.

We should be able to check the value and set $max_pages to 0 too in that case.

Last edited 3 years ago by SergeyBiryukov (previous) (diff)

@kapilpaul
3 years ago

Created patch.

This ticket was mentioned in PR #1518 on WordPress/wordpress-develop by kapilpaul.


3 years ago
#2

  • Keywords has-patch added; needs-patch removed

This PR will fix the /wp-admin/includes/ajax-actions.php file line 3006 is creating divide by zero error.

Trac Ticket: https://core.trac.wordpress.org/ticket/53773

#3 @audrasjb
3 years ago

I tested the proposed patch with changing the posts_per_page value using ajax_query_attachments_args filter and it looks like it solves the issue.
Marking for commit.

#4 @audrasjb
3 years ago

  • Keywords commit added

#5 @SergeyBiryukov
3 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 51485:

Media: Check the posts_per_page value in wp_ajax_query_attachments() before using it as a divisor.

This avoids a "Division by zero" PHP warning if a plugin changes the posts_per_page value to zero.

Follow-up to [51145].

Props 2linctools, kapilpaul, audrasjb.
Fixes #53773.

#6 @SergeyBiryukov
3 years ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for backporting to the 5.8 branch.

#7 @desrosj
3 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 51507:

Media: Check the posts_per_page value in wp_ajax_query_attachments() before using it as a divisor.

This avoids a "Division by zero" PHP warning if a plugin changes the posts_per_page value to zero.

Follow-up to [51145].

Props 2linctools, kapilpaul, audrasjb, SergeyBiryukov.
Merges [51485] to the 5.8 branch.
Fixes #53773.

Note: See TracTickets for help on using tickets.