Opened 8 weeks ago
Closed 2 weeks ago
#65934 closed defect (bug) (fixed)
Coding Standards: Incomplete variable rename in wp_ajax_add_meta() leaves an undefined $c
| Reported by: | mazharul78 | Owned by: | audrasjb |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | Options, Meta APIs | Version: | 7.0 |
| Severity: | normal | Keywords: | has-patch commit |
| Cc: | Focuses: | coding-standards |
Description
While reading wp_ajax_add_meta() I noticed it passes a variable that doesn't exist.
In [61224] the variable $c was renamed to $count. It's used in three places, but only two of them were changed:
<?php // src/wp-admin/includes/ajax-actions.php $count = 0; // line 1637 - changed _list_meta_row( $meta, $count ) // line 1703 - changed _list_meta_row( array( ... ), $c ) // line 1750 - still says $c
So the last call passes $c, and $c is not defined anywhere in the function.
Normally PHP would warn about that. It doesn't here, because the second argument is taken by reference:
<?php function _list_meta_row( $entry, &$count ) {
When you pass an undefined variable by reference, PHP simply creates it and sets it to null. No notice, no error. That's also why PHPStan doesn't report it - $c is missing from tests/phpstan/baselines/variable.undefined.neon, even though five other variables from this same file are listed there.
What breaks
Nothing right now. $count is never read after either call, because $response->send() runs immediately, so the output is exactly the same before and after.
I'm reporting it because the two calls no longer match each other, and if someone uses $count after that call later on, it will quietly hold the wrong value.
Versions
Affects 7.0, 7.1 and trunk. 6.9 and earlier are fine - they still use $c in all three places.
Testing
tests/phpunit/tests/ajax/wpAjaxAddMeta.php already covers this code path in test_wp_ajax_add_meta_allows_empty_values_on_updating(). It passes before and after, which is what you'd expect since the behavior doesn't change.
Follow-up to [61224]. See #64226.
PR: https://github.com/WordPress/wordpress-develop/pull/13227
Change History (6)
This ticket was mentioned in PR #13227 on WordPress/wordpress-develop by @mazharul78.
8 weeks ago
#1
#2
@
8 weeks ago
- Milestone Awaiting Review → 7.2
- Owner set to
- Status new → accepted
Thanks for the ticket and the pull request. This looks good to me at a glance, so I'm moving it to 7.2.
#4
@
4 weeks ago
- Keywords commit added
Mark ready for commit
@audrasjb Is there anything pending? Can you process for commit.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
While reading
wp_ajax_add_meta()I noticed it passes a variable that doesn't exist.In [61224] the variable
$cwas renamed to$count. It's used in three places, but only two of them were changed:So the last call passes
$c, and$cisn't defined anywhere in the function.Normally PHP would warn about that. It doesn't here, because the second argument is taken by reference:
function _list_meta_row( $entry, &$count ) {When you pass an undefined variable by reference, PHP just creates it and sets it to null. No notice, no error. That's also why PHPStan doesn't flag it -
$cis missing fromtests/phpstan/baselines/variable.undefined.neon, even though five other variables from this same file are listed there.This changes that one line to
$count.### What breaks
Nothing right now.
$countis never read after either call, since$response->send()runs immediately, so the output is identical before and after.I'm raising it because the two calls no longer match each other, and if someone uses
$countafter that call later on, it will quietly hold the wrong value.### Testing
tests/phpunit/tests/ajax/wpAjaxAddMeta.phpalready covers this code path intest_wp_ajax_add_meta_allows_empty_values_on_updating():It passes before and after, which is what you'd expect since the behavior doesn't change.
Affects 7.0, 7.1 and trunk. 6.9 and earlier are fine - they still use
$cin all three places.