Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#24827 closed defect (bug) (wontfix)

bug in post-template.php

Reported by: crysman's profile crysman Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.2
Component: Template Keywords:
Focuses: Cc:

Description

The bug

There is a bug in wp-includes/post-template.php, line 208:

if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
...

$post variable is being inicialized on line 183 like this:

$post = get_post();

What is wrong is that get_post() may return NULL, and it sometimes does. So the code

$post->post_content

is obviously incorrect, because it is trying to access an object, which does not exist. Therefore, you get a PHP notice, thus getting "header already send" errors...

the fix

is simple - just check first if non-empty:

  if(!empty($post))
	if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
...

Change History (7)

#1 @johnbillion
10 years ago

  • Keywords reporter-feedback added

In what situation might get_post() return null here? The function get_the_content() should only get called inside the loop where there'll be a global post object.

#2 @SergeyBiryukov
10 years ago

  • Component changed from General to Template

#3 @crysman
10 years ago

It is not about what it "might return". It is actually returning it, that is why I came up to this bug in the first place.

Problems occured after installing Content Headings Tree plugin.

But I don't think the plugin contains an error, because this is obviously a core bug - you cannot access an undefined variable/object... You cannot think like "OK, this function might return NULL but it will not I hope".

#4 @crysman
10 years ago

  • Keywords needs-patch added; reporter-feedback removed

#5 @Frank Klein
10 years ago

  • Cc contact@… added
  • Keywords needs-patch removed
  • Resolution set to invalid
  • Status changed from new to closed

The ​Content Headings Tree plugin uses the get_the_content() function outside the loop without passing any arguments, which is the source of the error.

#6 @SergeyBiryukov
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution changed from invalid to wontfix

Yes, we should not hide notices caused by plugin or theme bugs. That would only make debugging harder.

#7 @SergeyBiryukov
10 years ago

For reference, looks like the error is in lib/replaceTags.php, line 3:
http://plugins.trac.wordpress.org/browser/content-headings-tree/trunk/lib/replaceTags.php#L3

That file is included on plugin loading, which is too early to use any template tags:
http://plugins.trac.wordpress.org/browser/content-headings-tree/trunk/plugin.php#L38

Last edited 10 years ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.