Skip to content

Conversation

@dmsnell
Copy link
Member

@dmsnell dmsnell commented May 2, 2022

This PR for discussion of code and tests - see companion ticket

Trac ticket: https://core.trac.wordpress.org/ticket/55635

Resolves #17725 (https://core.trac.wordpress.org/ticket/17725)

When wp_convert_hr_to_bytes() was introduced in [4388] it provided a simplified
mechanism to parse the values returned by functions like ini_get() which
represent byte sizes. The over-simplified approach has led to issues in that
function reporting the wrong byte sizes for various php.ini directives, leading
to confusing problems such as uploading files that are rejected improperly or
accepted improperly.

In this patch we're porting the parser from PHP's own source (which has
remained stable for decades and probably can't change without major breakage)
in order to more accurately reflect the values it uses when it reads those
configurations. This is available in the new function wp_ini_parse_quantity() found
inside of wp-includes/php-compat.php and loaded automatically in load.php.

Unfortunately PHP doesn't offer a mechanism to read its own internal value for
these fields and a 100% port is extremely cumbersome (at best) due to the
different ways that PHP and C handle signed integer overflow. These differences
should only appear when supplying discouraged/invalid values to the system
anyway, and PHP warns that in these situations things are likely to break
anyway.

Sentinel overlap

There is semantic overlap between two values:

  • Impose no limit on the memory use.
  • Allow infinite memory use.

These behave differently under comparison, because imposing no limit on memory
use implies that any other actual limit takes precedence over it. Some limit
is more restrictive than no limit.

But then when examining unlimited use, this restriction is more liberal than
any other memory limit than itself. It will always return as not-less-than any
other limit.

There lacks a clear way to disambiguate these.

Testing

Since initially proposing this, PHP updated its own internal parser and now issues warnings for invalid values. It also sets a default value in cases where the value fails to parse.

PHP version memory_limit allowed memory size crashed when… note
8.5.1 -1 killed after allocating 210 GB…
8.5.1 0 134,217,728 (128 MB) immediate crash PHP Warning: Failed to set memory limit to 0 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0
8.5.1 1 134,217,728 (128 MB) immediate crash PHP Warning: Failed to set memory limit to 1 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0
8.5.1 1G 1,073,741,824 (1 GB) after 29 doublings
8.5.1 ship 134,217,728 (128 MB) after 26 doublings PHP Warning: Invalid "memory_limit" setting. Invalid quantity "ship": no valid leading digits, interpreting as "0" for backwards compatibility in Unknown on line 0
8.5.1 1e5 134,217,728 (128 MB) immediately PHP Warning: Invalid "memory_limit" setting. Invalid quantity "1e5": unknown multiplier "5", interpreting as "1" for backwards compatibility in Unknown on line 0
8.5.1 9223372036854775807g -1,073,741,824 (-1 GB) immediately PHP Warning: Invalid "memory_limit" setting. Invalid quantity "9223372036854775807g": value is out of range, using overflow result for backwards compatibility in Unknown on line 0

@github-actions
Copy link

github-actions bot commented May 2, 2022

Hi @dmsnell! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in this blog post.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@dmsnell
Copy link
Member Author

dmsnell commented May 3, 2022

I suppose that the compatibility check won't block this PR, but I heeded it's advice an in this PHP-compat module fill in the PHP_INT_MAX and PHP_INT_MIN constants. Since this is pulled in at the front of load.php I think that means we should be able to relax the lint rule, because as long as WordPress is running it should be defined.

@dmsnell dmsnell force-pushed the add/wp_ini_bytes branch from 758c52f to b3ba88d Compare May 4, 2022 20:32
@dmsnell dmsnell force-pushed the add/wp_ini_bytes branch 2 times, most recently from bdabf4e to f130e89 Compare May 13, 2022 19:29
@dmsnell dmsnell changed the title Add wp_ini_bytes() to report numeric php.ini directive values Add wp_ini_parse_quantity() to report numeric php.ini directive values May 13, 2022
dmsnell and others added 17 commits January 13, 2026 17:26
Partner to #55635 (https://core.trac.wordpress.org/ticket/55635)
Resolves #17725  (https://core.trac.wordpress.org/ticket/17725)

When wp_convert_hr_to_bytes() was introduced in [4388] it provided a simplified
mechanism to parse the values returned by functions like ini_get() which
represent byte sizes. The over-simplified approach has led to issues in that
function reporting the wrong byte sizes for various php.ini directives, leading
to confusing problems such as uploading files that are rejected improperly or
accepted improperly.

In this patch we're porting the parser from PHP's own source (which has
remained stable for decades and probably can't change without major breakage)
in order to more accurately reflect the values it uses when it reads those
configurations. This is available in the new function `wp_ini_bytes()` found
inside of `wp-includes/php-compat.php` and loaded automatically in `load.php`.

Unfortunately PHP doesn't offer a mechanism to read its own internal value for
these fields and a 100% port is extremely cumbersome (at best) due to the
different ways that PHP and C handle signed integer overflow. These differences
should only appear when supplying discouraged/invalid values to the system
anyway, and PHP warns that in these situations things are likely to break
anyway.
Co-authored-by: Ayesh Karunaratne <Ayesh@users.noreply.github.com>
@github-actions
Copy link

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props dmsnell, ayeshrajans.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants