Make WordPress Core

Opened 12 days ago

Last modified 5 days ago

#64404 accepted defect (bug)

Uncaught DivisionByZeroError: Modulo by zero

Reported by: kaushiksomaiya's profile kaushiksomaiya Owned by: westonruter's profile westonruter
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Cron API Keywords: has-patch
Focuses: Cc:

Description

Hello team,

It seems there's a possibility of plugins or custom code inadvertently inserting boolean value in custom schedule intervals. When that happens - Cron breaks badly even after removal of the custom code/plugin declaring it. This is because there's an attempt to reschedule the event before executing it.. and probably if ( 0 === $interval ) { doesn't evaluate true for the bool value.

To Replicate:

// 1. Add a broken schedule add_filter( 'cron_schedules', function( $schedules ) { // INTENTIONALLY BROKEN $schedules['kscronbreaker_cron_schedule'] = array( 'interval' => false, // ❌ Wrong: must be integer > 0 'display' => 'Broken Cron Schedule', ); return $schedules; } ); // 2. Try to schedule the event add_action( 'init', function() { if ( ! wp_next_scheduled( 'kscronbreaker_broken_cron_hook' ) ) { wp_schedule_event( time() + 10, 'kscronbreaker_cron_schedule', 'kscronbreaker_broken_cron_hook' ); } } ); // 3. Cron handler (never runs) add_action( 'kscronbreaker_broken_cron_hook', function() { error_log( 'KS Cron Breaker executed — THIS SHOULD NEVER APPEAR' ); } ); 

Then check the server's fatal error logs

Uncaught DivisionByZeroError: Modulo by zero in /wp-5351423/wp-includes/cron.php:436

Change History (5)

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


12 days ago
#1

  • Keywords has-patch added

#2 @westonruter
12 days ago

  • Milestone changed from Awaiting Review to 7.0

This seems like something which would be worthwhile to fix, given that there is already is error checking for an invalid interval. Here's a quick proposal to fix: https://github.com/WordPress/wordpress-develop/pull/10619

#3 @westonruter
12 days ago

  • Owner set to westonruter
  • Status changed from new to accepted

#4 @soyebsalar01
8 days ago

Tested PR 10619 after rebuilding from src.

After using the custom cron schedule with an invalid interval (false) no longer causes a DevisionByZeroError. So the cron execute the event once and then fails to reschedule gracefully with invalid_schedule.

Patch works as expected.

@johnbillion commented on PR #10619:


5 days ago
#5

There's a failing test:

1) Tests_Cron::test_invalid_recurrence_for_event_returns_error
sprintf(): Argument number must be greater than zero

/var/www/src/wp-includes/cron.php:428
/var/www/tests/phpunit/tests/cron.php:856
phpvfscomposer:///var/www/vendor/phpunit/phpunit/phpunit:106
/var/www/vendor/bin/phpunit:118

Note: See TracTickets for help on using tickets.