WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 7 months ago

#42761 new defect (bug)

Undefined offset: -1 in wp-includes/post-template.php

Reported by: twisted1919 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.1
Component: Posts, Post Types Keywords: reporter-feedback
Focuses: template Cc:

Description

Hello,

I am getting this notice:

<b>Notice</b>: Undefined offset: -1 in <b>/var/www/vhosts/domain/httpdocs/web/wp-includes/post-template.php</b> on line <b>287</b><br />

The logic that checks for index presence is faulty:

<?php
if ( $page > count( $pages ) ) // if the requested page doesn't exist
                $page = count( $pages ); // give them the highest numbered page that DOES exist

If $page is 0 or 1 and $pages is an empty array, you end up checking an out of bounds array and trigger this notice.

Maybe a more complete fix would be:

<?php
if ( $page > count( $pages ) ) // if the requested page doesn't exist
                $page = count( $pages ); // give them the highest numbered page that DOES exist

        $content = isset($pages[$page - 1]) ? $pages[$page - 1] : '';

Ow whatever you think it will avoid this.

Thanks.

Change History (2)

#1 @dd32
7 months ago

  • Component changed from General to Posts, Post Types
  • Focuses template added
  • Keywords reporter-feedback added

Hey @twisted1919 and welcome back to Trac.

Can you provide some extra details on how you run into this? In what situations is $pages an empty array for example?

For an empty post, $pages should be array( 0 => '' )

#2 @twisted1919
7 months ago

Hello,

I am not exactly sure how to have you reproduce this, but it started when we upgraded woocommerce to latest version and then we also did this for wordpress, so i am not sure, maybe other plugins somehow interacts with that global variable, but eitherway, maybe wordpress should do a more in-depth check at that particular point to avoid this issue.

If you need anything else from me, list of plugins, or whatever info, let me know.

Thanks.

Note: See TracTickets for help on using tickets.