Make WordPress Core

Opened 17 months ago

Last modified 7 months ago

#57401 new defect (bug)

post-template.php fails to check if $elements['page'] is zero causing an array index error

Reported by: mjdewitt's profile mjdewitt Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:


This throws a warning:

Warning: Undefined array key -1 in /wp-includes/post-template.php on line 327

I'm not sure when this started showing up. I think it's happening in the handling scheduled posts.

Perhaps this is all that is needed?

326: $page_no = $elementspage? ? $elementspage? : 1;

Change History (3)

#1 @mjdewitt
17 months ago

Apologies, the code got mangled when posted.

#2 @rkyburz
7 months ago

I see this with WP 6.3.2 / PHP 8.1.24. In my case, the line number referred to in the error message is 330. The relevant lines (329/330) are

    $page_no = $elements['page'];
    $content = $elements['pages'][ $page_no - 1 ];

The problem is that $page_no can evaluate to 0, yielding an invalid negative index on line 330. I fixed this be replacing line 330 with

    if ( $page_no > 0 ) {
        $content = $elements['pages'][ $page_no - 1 ];
    } else {
        $content = $elements['pages'][ 0 ];

With this, the error is gone. I suspect that this is linked to PHP > 8.0

Last edited 7 months ago by rkyburz (previous) (diff)

#3 @rkyburz
7 months ago

Addendum: I upgraded to WP 6.4, which reverted post-template.php (line 330 in particular) to its original form, but the error message so far did not show up again. I'm still on PHP 8.1.24 — will keep watching out.

Note: See TracTickets for help on using tickets.