Make WordPress Core

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

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:

// 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 isn'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 - $c is missing from tests/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. $count is 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 $count after that call later on, it will quietly hold the wrong value.

### Testing

tests/phpunit/tests/ajax/wpAjaxAddMeta.php already covers this code path in test_wp_ajax_add_meta_allows_empty_values_on_updating():

phpunit --filter test_wp_ajax_add_meta

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 $c in all three places.

#2 @audrasjb
8 weeks ago

  • Milestone Awaiting Review → 7.2
  • Owner set to audrasjb
  • 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.

#3 @harishtewari
8 weeks ago

Great observation.

#4 @mukesh27
4 weeks ago

  • Keywords commit added

Mark ready for commit

@audrasjb Is there anything pending? Can you process for commit.

#5 @peterwilsoncc
2 weeks ago

  • Component Administration → Options, Meta APIs

Moving this over the the Meta API component for commit.

#6 @peterwilsoncc
2 weeks ago

  • Resolution → fixed
  • Status accepted → closed

In 63959:

Options, Meta APIs: Correct $count argument passed by reference.

In r61224 the $c variable in wp_ajax_add_meta() was renamed $count. This update a third instance of the variable that was missed in the original commit.

Follow up to r61224.

Props mazharul78, audrasjb, harishtewari, mukesh27.
Fixes #65934.

Note: See TracTickets for help on using tickets.