[wp-trac] [WordPress Trac] #57401: post-template.php fails to check if $elements['page'] is zero causing an array index error

WordPress Trac noreply at wordpress.org
Tue Oct 24 14:19:48 UTC 2023


#57401: post-template.php fails to check if $elements['page'] is zero causing an
array index error
--------------------------+------------------------------
 Reporter:  mjdewitt      |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by rkyburz):

 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

 {{{#!php
 <?php
     $page_no = $elements['page'];
     $content = $elements['pages'][ $page_no - 1 ];

 }}}

 The problem is that $elements['page']; can evaluate to 0, yielding an
 invalid negative index on line 330. I fixed this be replacing line 330
 with

 {{{#!php
 <?php
     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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57401#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list