Make WordPress Core

Opened 7 years ago

Last modified 3 days ago

#41079 new defect (bug)

PHP Notice: Undefined offset: -1 in /var/www/html/live/wp-includes/rewrite.php

Reported by: myrmidon16's profile myrmidon16 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.3
Component: Rewrite Rules Keywords:
Focuses: Cc:

Description

Low priority, but I'm trying to reduce the size of my error logs, and this notice is a vast majority of it.

Issue lies at lines 376 & 378 in the "wp_resolve_numeric_slug_conflicts" function. Occurs when $postname_index is 0. With my limited understanding, it seems like that scenario can be skipped, so I added the below snippet to the "if" statement at line 363 in my build:

|| 0 === $postname_index

Please resolve in the next update. Thanks.

Change History (4)

#1 follow-up: @Presskopp
7 years ago

array_search() only returns NULL if invalid parameters are passed to it. So the question is, why the parameters are invalid in your case. 2c

#2 in reply to: ↑ 1 @myrmidon16
7 years ago

Replying to Presskopp:

array_search() only returns NULL if invalid parameters are passed to it. So the question is, why the parameters are invalid in your case. 2c

array_search() returns false if invalid parameters are passed to it. There is a gate at line 363 that checks for that, yet the result is getting past that.

Since $permastructs is the result of array_values(), it's safe to presume it is an indexed array. So if array_search() is not returning false, $postname_index is likely an index.

And since $permastructs[$postname_index - 1] at lines 376 & 378 are looking for an offset of -1, I presume $postname_index is 0.

#3 @Presskopp
7 years ago

ah, NULL vs 0

#4 @boceksumuklu
2 weeks ago

It seems like you've deduced that $postname_index is likely 0, which is causing the issue with the offsets of -1 at lines 376 and 378. If $postname_index is indeed 0, it would mean that the slug being checked is at the first position in the array.

Given this context, your proposed solution of adding an additional condition to skip the scenario when $postname_index is 0 seems appropriate:

if ( 0 === $postname_index || in_array( $slug_index, $check_existing_slugs, true ) ) {
    // Skip the problematic scenario when $postname_index is 0
    // Other existing conditions and code here...
}

By adding 0 === $postname_index to the if statement, you're ensuring that the block of code is skipped when $postname_index is 0. This should prevent the offsets of -1 from causing any issues.

Last edited 3 days ago by boceksumuklu (previous) (diff)
Note: See TracTickets for help on using tickets.