Fixed PHP7. Fatal Error: Method XenForo_BbCode_TextWrapper::__toString()

GliX

Active member
Error at PHP 7.0.

Error Info

ErrorException: Fatal Error: Method XenForo_BbCode_TextWrapper::__toString() must not throw an exception -library/XenForo/Template/Abstract.php(265) : eval()'d code:0
Generated By: Unknown Account, 51 minutes ago

Stack Trace
#0 [internal function]: XenForo_Application::handleFatalError()
#1 {main}
 
Error at PHP 7.0, occurs in RSS feeds with threads, which use CODE bb-code.

Error Info
ParseError: Invalid numeric literal - library/XenForo/BbCode/Formatter/Base.php(1420) : highlighted code:5
Generated By: Unknown Account, 29 minutes ago

Stack Trace
PHP:
#0 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(1420): highlight_string('<?php\n[Tue Oct ...', true)
#1 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(745): XenForo_BbCode_Formatter_Base->renderTagPhp(Array, Array, Object(XenForo_BbCode_Formatter_Base))
#2 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(652): XenForo_BbCode_Formatter_Base->renderValidTag(Array, Array, Array)
#3 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(565): XenForo_BbCode_Formatter_Base->renderTag(Array, Array, 0)
#4 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(546): XenForo_BbCode_Formatter_Base->renderTreeElement(Array, Array, 0)
#5 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(526): XenForo_BbCode_Formatter_Base->renderSubTree(Array, Array)
#6 /var/www/demo.local/library/XenForo/BbCode/Parser.php(505): XenForo_BbCode_Formatter_Base->renderTree(Array, Array)
#7 /var/www/demo.local/library/XenForo/ViewPublic/Forum/View.php(64): XenForo_BbCode_Parser->render('\xD0\xA1\xD0\xB5\xD0\xB3\xD0\xBE\xD0\xB4\xD0\xBD\xD1\x8F ...', Array)
#8 /var/www/demo.local/library/XenForo/ViewRenderer/Abstract.php(227): XenForo_ViewPublic_Forum_View->renderRss()
#9 /var/www/demo.local/library/XenForo/ViewRenderer/Rss.php(31): XenForo_ViewRenderer_Abstract->renderViewObject('XenForo_ViewPub...', 'Rss', Array, 'forum_view')
#10 /var/www/demo.local/library/XenForo/FrontController.php(604): XenForo_ViewRenderer_Rss->renderView('XenForo_ViewPub...', Array, 'forum_view', NULL)
#11 /var/www/demo.local/library/XenForo/FrontController.php(158): XenForo_FrontController->renderView(Object(XenForo_ControllerResponse_View), Object(XenForo_ViewRenderer_Rss), Array)
#12 /var/www/demo.local/index.php(13): XenForo_FrontController->run()
#13 {main}
 
I'm unable to reproduce this with PHP 7.

Are you able to share the exact post content within the PHP tag that is causing this?
 
This and your other bug report are almost certainly the same thing and related to the PHP BB code specifically.
 
We're not running PHP 7 here, yet, nor do I have it on this PC at the moment, but thanks for the examples I will try it with PHP 7 later.
 
This and your other bug report are almost certainly the same thing and related to the PHP BB code specifically.
Indeed, they are, so I have merged the threads.

Error at PHP 7.0, occurs in RSS feeds with threads, which use CODE bb-code.

Error Info
ParseError: Invalid numeric literal - library/XenForo/BbCode/Formatter/Base.php(1420) : highlighted code:5
Generated By: Unknown Account, 29 minutes ago

Stack Trace
PHP:
#0 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(1420): highlight_string('<?php\n[Tue Oct ...', true)
#1 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(745): XenForo_BbCode_Formatter_Base->renderTagPhp(Array, Array, Object(XenForo_BbCode_Formatter_Base))
#2 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(652): XenForo_BbCode_Formatter_Base->renderValidTag(Array, Array, Array)
#3 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(565): XenForo_BbCode_Formatter_Base->renderTag(Array, Array, 0)
#4 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(546): XenForo_BbCode_Formatter_Base->renderTreeElement(Array, Array, 0)
#5 /var/www/demo.local/library/XenForo/BbCode/Formatter/Base.php(526): XenForo_BbCode_Formatter_Base->renderSubTree(Array, Array)
#6 /var/www/demo.local/library/XenForo/BbCode/Parser.php(505): XenForo_BbCode_Formatter_Base->renderTree(Array, Array)
#7 /var/www/demo.local/library/XenForo/ViewPublic/Forum/View.php(64): XenForo_BbCode_Parser->render('\xD0\xA1\xD0\xB5\xD0\xB3\xD0\xBE\xD0\xB4\xD0\xBD\xD1\x8F ...', Array)
#8 /var/www/demo.local/library/XenForo/ViewRenderer/Abstract.php(227): XenForo_ViewPublic_Forum_View->renderRss()
#9 /var/www/demo.local/library/XenForo/ViewRenderer/Rss.php(31): XenForo_ViewRenderer_Abstract->renderViewObject('XenForo_ViewPub...', 'Rss', Array, 'forum_view')
#10 /var/www/demo.local/library/XenForo/FrontController.php(604): XenForo_ViewRenderer_Rss->renderView('XenForo_ViewPub...', Array, 'forum_view', NULL)
#11 /var/www/demo.local/library/XenForo/FrontController.php(158): XenForo_FrontController->renderView(Object(XenForo_ControllerResponse_View), Object(XenForo_ViewRenderer_Rss), Array)
#12 /var/www/demo.local/index.php(13): XenForo_FrontController->run()
#13 {main}
This is the error that triggers while trying to post the problem content.

Error at PHP 7.0.

Error Info

ErrorException: Fatal Error: Method XenForo_BbCode_TextWrapper::__toString() must not throw an exception -library/XenForo/Template/Abstract.php(265) : eval()'d code:0
Generated By: Unknown Account, 51 minutes ago

Stack Trace
#0 [internal function]: XenForo_Application::handleFatalError()
#1 {main}
This is the error when trying to view that content if it already exists.

I have confirmed this with PHP 7.0.1-dev, though I haven't actually updated this for some time, which I will do shortly. It's possible it's a bug in PHP itself, especially as the ParseError is happening within a core PHP function which we don't actually have any control over, short of working out what cases the parse error and sanitising or escaping it.

EDIT: Now running 7.0.2-dev, and the issue is still present.

EDIT 2: Simpler reproduction case:
PHP:
09
o_O

As far as I can work out so far, the specific change in PHP 7 is this:
Invalid octal literals in source code now produce compile errors, fixes PHPSadness #31.
Which relates to: http://phpsadness.com/sad/31

EDIT 3: Have reported it as a PHP bug: https://bugs.php.net/bug.php?id=71086

The behaviour here seems unexpected. It is caused by a fairly valid change in PHP 7, but not one I think that should affect the highlight_string function.
 
Last edited:
It has indeed been confirmed as a PHP bug and it has been fixed. I haven't tested yet to see if that fix is available in the latest nightly build, but for most people I guess we'll see this in PHP 7.0.2.

In terms of how we handle this in XF we are catching this parse error and falling back to rendering a standard [CODE] tag.

If this bug is affecting you, find the following in library/XenForo/BbCode/Formatter/Base.php:
PHP:
$content = highlight_string($content, true);
And replace with:
PHP:
try
{
   $content = highlight_string($content, true);
}
catch (Throwable $e)
{
   // Workaround for PHP (7.0.0) bug #71086
   return $this->renderTagCode($tag, $rendererStates) ;
}

EDIT: The nightly build has indeed fixed this:
Code:
PHP 7.0.2-dev (cli) (built: Dec 11 2015 20:13:30) ( NTS )
 
Last edited:
Top Bottom