Fix rest_is_integer validation logic#11883
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| $integer_pattern = '/^[+-]?\d+$/'; | ||
| $scientific_pattern = '/^[+-]?\d+e\+?\d+$/i'; | ||
|
|
||
| $value = trim( (string) $maybe_integer ); |
There was a problem hiding this comment.
This is incorrectly resulting in rest_is_integer( true ) returning true, since (string) true === '1'.
It should return false: https://playground.wordpress.net/php-playground.html#eyJjb2RlIjoiPD9waHBcbnJlcXVpcmUgJy93b3JkcHJlc3Mvd3AtbG9hZC5waHAnO1xuXG52YXJfZHVtcCggcmVzdF9pc19pbnRlZ2VyKCB0cnVlICkgKTsiLCJwaHAiOiI4LjQiLCJ3cCI6IjcuMCJ9
|
The problem with this PR is that there's a bunch of cases where it doesn't return the same result as the old implementation. Here is what the old implementation does: Here is what the implementation in this PR does: I don't think we should be trying to rewrite the entire My PR (#11893) aims to fix the bug with large integers but otherwise attempts to preserve the existing behavior of the function. I think this is the safest approach. |
|
@siliconforks I fixed the boolean case. I am still not sure why would someone need 123.0 or 10e-1 as an integer here, considering its purpose. I'd even go as far as to say that this is an unnoticed bug that slipped under eyeballs for years and must be corrected now. But even if it is needed, I would still prefer a regex based approach over a float approach. But that could be just me. |
This changeset updates
rest_is_integer()to avoid using floating-point conversion when determining whether a value is integer-like.Previously, the function relied on:
This approach breaks for large integers because PHP floating-point values cannot precisely represent all integers above IEEE-754 precision limits (~2^53). As a result, valid large integer values — including values still valid for MySQL/MariaDB
BIGINT UNSIGNEDcolumns — could incorrectly fail REST API validation.The updated implementation:
PHP_INT_MAX)This fixes cases where very large post IDs can cause REST API requests to fail, which may prevent the block editor from loading posts correctly when auto-increment values have been manually increased or otherwise become unusually large.
Trac ticket: https://core.trac.wordpress.org/ticket/65271
Use of AI Tools
AI assistance: Yes
Tool(s): ChatGPT
Model(s): GPT-5.5
Used for: Generating regex pattern
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.