Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#54751 closed defect (bug) (fixed)

Invalid argument in class-wp-list-util.php

Reported by: marv2's profile marv2 Owned by: peterwilsoncc's profile peterwilsoncc
Milestone: 6.0 Priority: normal
Severity: normal Version: 3.1
Component: General Keywords: good-first-bug has-patch has-unit-tests
Focuses: Cc:

Description

I've been getting this error since v5.8.2.

Seems to be coming from WP core.

PHP Warning:  Invalid argument supplied for foreach() in /htdocs/wp-includes/class-wp-list-util.php on line 164

Attachments (2)

54751.diff (4.1 KB) - added by mkox 3 years ago.
Fix for the invalid argument warning
54751.2.diff (4.1 KB) - added by mkox 3 years ago.
Corrected diff direction

Download all attachments as: .zip

Change History (16)

#1 @davidbaumwald
3 years ago

  • Keywords good-first-bug needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 6.0
  • Version changed from 5.8.2 to 3.1

@marv2 Welcome to WordPress Core Trac, and thanks for the ticket!

This can happen when using the wp_list_pluck function which, in the underlying WP_List_Util class expects the list to be an array.

I think adding some checks before iterating over the array in WP_List_Util->pluck to ensure the list is an actual array would be nice to get rid of this warning. Perhaps throwing a doing_it_wrong is also needed?

Also, going to tag this for needs-unit-tests. It would be good to have some "unhpappy path" tests to check some invalid types sent to the pluck method.

Version 0, edited 3 years ago by davidbaumwald (next)

@mkox
3 years ago

Fix for the invalid argument warning

#2 @mkox
3 years ago

Hello to everybody!

I'am using WordPress a lot and now there is the time for me to give something back. Found this ticket from 'good-first-bug'.

So this would be my first patch and I tried to follow all the contribution guides and the recommendations of @davidbaumwald.

I added some more test as suggested, too.

Maybe someone e.g. @davidbaumwald can check this patch.

#3 @SergeyBiryukov
3 years ago

Thanks for the patch and for including a unit test!

This looks like a great start, but the patch seems reversed: it shows the added lines as removed, and vice versa. Once that is corrected, it should be easier to continue the review.

#4 @SergeyBiryukov
3 years ago

  • Keywords has-patch has-unit-tests needs-refresh added; needs-patch needs-unit-tests removed

@mkox
3 years ago

Corrected diff direction

#5 @mkox
3 years ago

Thank you for the hint @SergeyBiryukov, new file has the right diff direction.

#6 @mkox
3 years ago

  • Keywords needs-refresh removed

This ticket was mentioned in PR #2492 on WordPress/wordpress-develop by dd32.


3 years ago
#8

This matches the behaviour of wp_filter_object_list(), wp_list_filter(), and wp_list_sort(). wp_list_pluck() was the only function related to WP_List_Util that doesn't validate the input.

Trac Ticket: https://core.trac.wordpress.org/ticket/54751

#9 @dd32
3 years ago

PR2492 is an alternative, simpler take on this.

Simply validating the input values, before passing invalid data to WP_List_Util, and matches the other utility functions.

The benefit to taking this direction is that existing 'invalid' uses of wp_list_pluck() won't need to do anything to gain the benefit.

I've not included a unit test, as the above patches and #55300 cover it appropriately.

#10 @peterwilsoncc
3 years ago

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

In 53045:

General: Validate input of wp_list_pluck().

wp_list_pluck() is used by WordPress to pluck items from a list. Of course, this requires a list. This validates the input of wp_list_pluck() to ensure it is a list.

This matches the behaviour of wp_list_sort() and wp_filter_object_list().

Props marv2, davidbaumwald, mkox, SergeyBiryukov, dd32.
Fixes #54751.

peterwilsoncc commented on PR #2492:


3 years ago
#11

Committed in https://core.trac.wordpress.org/changeset/53045

Also, best to create patches in your own fork. I think it sometimes creates work for the meta folk or something.

dd32 commented on PR #2492:


3 years ago
#12

best to create patches in your own fork. I think it sometimes creates work for the meta folk or something.

😱 Well that was a total accident! I've gotten too used to clicking edit on GitHub and it opening in my own fork..

#13 @RMarks
3 years ago

Should a condition be added to see if the array is empty? The error below was encountered when the array was empty:

Warning: Invalid argument supplied for foreach() in /code/wp-includes/class-wp-list-util.php on line 165

Or should I open a new ticket?

#14 @peterwilsoncc
3 years ago

@RMarks I'm unable to reproduce this on the trunk branch that includes this fix. The function returns an empty array, I tried various combinations:

wp> wp_list_pluck( [], 'bob' );
=> :
array(0) {
}
 wp_list_pluck( [[],[],[]], 'bob' );
=> :
array(3) {
  [0] =>
  NULL
  [1] =>
  NULL
  [2] =>
  NULL
}
wp> wp_list_pluck( [(object)[],(object)[],(object)[]], 'bob' );
=> :
array(3) {
  [0] =>
  NULL
  [1] =>
  NULL
  [2] =>
  NULL
}

If you're able to reproduce the error on trunk, a new ticket would be better to allow this ticket to remain focused on one bug.

Note: See TracTickets for help on using tickets.