Opened 13 months ago

Last modified 2 months ago

#20633 new defect (bug)

wp-includes/comment.php:738 - Undefined property: WP_Query::$comments

Reported by: markjaquith Owned by:
Priority: normal Milestone: 3.6
Component: Warnings/Notices Version: 3.3.2
Severity: normal Keywords: has-patch dev-feedback
Cc:

Description

get_comment_pages_count() can throw a notice. We use $wp_query->comments without checking it first.

Attachments (3)

20633.diff (638 bytes) - added by markjaquith 13 months ago.
20633.patch (716 bytes) - added by kitchin 2 months ago.
20633.2.patch (717 bytes) - added by DrewAPicture 2 weeks ago.
spacing

Download all attachments as: .zip

Change History (10)

  • Version set to 3.3.2

Make sure it's not empty.

  • Keywords has-patch added

comment:4 follow-up: ↓ 5   kitchin2 months ago

The patch is not correct. It does this:

- if ( !$comments || !is_array($comments) ) 
+ if ( ( !$comments || !is_array($comments) ) && !empty( $wp_query->comments ) ) 
    $comments = $wp_query->comments;
  if ( empty($comments) )
    return 0;

The patch defeats the is_array() check. For example, $comments=true, $wp_query->comments=null will not return 0.

Suggest:

- if ( !$comments || !is_array($comments) )
+ if ( empty($comments) || !is_array($comments) ) {
+   if (empty( $wp_query->comments ) )
+     return 0;
    $comments = $wp_query->comments;
+ }
- if ( empty($comments) )
-    return 0;

comment:5 in reply to: ↑ 4   DrewAPicture2 months ago

  • Keywords dev-feedback added

Replying to kitchin:

Suggest:

- if ( !$comments || !is_array($comments) )
+ if ( empty($comments) || !is_array($comments) ) {
+   if (empty( $wp_query->comments ) )
+     return 0;
    $comments = $wp_query->comments;
+ }
- if ( empty($comments) )
-    return 0;

Would you like to submit an updated patch?

Easiest place to see this bug is before the Loop. I put a note about that in the Codex, since then the function returns 0 with or without the bug fix. http://codex.wordpress.org/index.php?title=Function_Reference%2Fget_comment_pages_count&diff=128919&oldid=126895

A test for this bug is to set WP_DEBUG to true. In wp-content/themes/twentythirteen/single.php insert

  <?php printf('<pre>get_comment_pages_count=%s</pre>',
     get_comment_pages_count()); ?>

before and after the loop starts, at

  <?php comments_template(); ?>

And look at the Hello World post.
Expect 0 then 1, with 1 warning before the bug and no warnings after.
Another test is to make sure

  <?php printf('<pre>get_comment_pages_count=%s</pre>',
     get_comment_pages_count($GLOBALS['wp_query']->comments)); ?>

still works after the Loop. Expect 1.

But I didn't see anything similar in the unit tests as far as I could tell.

  • Milestone changed from Awaiting Review to 3.6

spacing

Note: See TracTickets for help on using tickets.