Make WordPress Core

Opened 4 years ago

Closed 3 years ago

Last modified 5 weeks ago

#54186 closed defect (bug) (fixed)

Cast string to int in `wp_checkdate`

Reported by: swissspidy's profile swissspidy Owned by: swissspidy's profile swissspidy
Milestone: 6.1 Priority: normal
Severity: normal Version:
Component: Date/Time Keywords: has-patch
Focuses: Cc:

Description

I am currently getting some PHP warnings when running integration tests against core:

PHP Warning: checkdate() expects parameter 1 to be int, string given in /tmp/wordpress/wp-includes/functions.php on line 6903

As it turns out, wp_resolve_post_date() extracts year/month/day from a post date (which is a string) and passes it to wp_checkdate (and from there to checkdate()), which requires ints.

To avoid these warnings, the input arguments should be cast to integers.

Attachments (1)

54186.patch (517 bytes) - added by hilayt24 4 years ago.
Explicitly converted the strings to the integer

Download all attachments as: .zip

Change History (5)

@hilayt24
4 years ago

Explicitly converted the strings to the integer

#1 @hilayt24
4 years ago

  • Keywords has-patch added; needs-patch good-first-bug removed

#2 @swissspidy
3 years ago

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

In 54126:

Date/Time: Cast extracted strings to integers in wp_resolve_post_date().

wp_resolve_post_date() extracts year/month/day from a post date (which is a string) and passes it to wp_checkdate (and from there to checkdate()), which requires ints.

Casting the strings to integers avoids PHP notices due to incorrect argument types.

Props hilayt24.
Fixes #54186

#3 @swissspidy
3 years ago

  • Milestone changed from Awaiting Review to 6.1

#4 @johnjamesjacoby
5 weeks ago

In 61172:

Date/Time: Prevent a PHP exception when inserting posts with a partially malformed post_date.

This commit updates wp_resolve_post_date() to use a regular expression for parsing the date string into year, month, and day matches. This approach handles missing leading zeros more reliably than substr() while remaining performant (see #57683).

It also adds checks and type-casting to wp_checkdate() variables before passing them into PHP's checkdate() function to avoid the potential for a TypeError in PHP 8 and newer (see #54186).

Lastly, it includes 2 new unit tests (with data providers) to cover an array of valid and invalid date formats (YYYY-MM-DD, YYYY-MM-DD HH:MM:SS, ISO 8601, RSS, leap years, malformed inputs, etc...)

Props alordiel, desrosj, johnbillion, johnjamesjacoby, johnregan3, modius5150, nacin, pbearne.

Fixes #26798.

Note: See TracTickets for help on using tickets.