php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55064 Inconsistent error reporting when using highlight_xxx() functions.
Submitted: 2011-06-29 06:13 UTC Modified: 2021-10-18 11:51 UTC
Votes:3
Avg. Score:3.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: RQuadling at GMail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 5.3.6 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: RQuadling at GMail dot com
New email:
PHP Version: OS:

 

 [2011-06-29 06:13 UTC] RQuadling at GMail dot com
Description:
------------
Calling highlight_xxx() with content that, if executed as a normal PHP script, 
would generate an error, results in the error being available to 
error_get_last(), even though, no actual PHP error has been generated by THIS 
code.

Even if all error reporting has been disabled in this script.

Whilst it there is a user note covering that error_get_last() will capture any 
error, even if it is suppressed, there is no error for highlight_xxx(), except 
when you have error_reporting set to show errors, display_errors are set AND you 
are using highlight_file() (rather than highlight_string()) to display some 
code.

So, 2 issues.

1 - Inconsistent error reporting between highlight_file() and highlight_string() 
when error_reporting is enabled and errors are displayed.

2 - Errors generated from non executed code are reported as errors in this 
script even though no error is output (with 1 exception).


I found this when looking into the iconv() notices generated by PhD for fa and 
ro Windows CHM building.

Test script:
---------------
<?php
$badScript = '<?php
/* Broken comment
';

file_put_contents('./badScript.php', $badScript);

function examine($text, $filename, $errorReporting, $displayErrors) {
	// Report all errors and display them.
	error_reporting($errorReporting);
	ini_set('display_errors', $displayErrors);
	ini_set('log_errors', $displayErrors);
	echo
		'error_reporting = ', $errorReporting, PHP_EOL,
		'display_errors  = ', $displayErrors, PHP_EOL,
		'log_errors      = ', $displayErrors, PHP_EOL, PHP_EOL,

		// Highlight the text
		highlight_string($text, True), PHP_EOL,

		// What is the last known error?
		'Last error is for highlight_string() if it is on line 19 :', PHP_EOL, print_r(error_get_last(), True), PHP_EOL,

		// Highlight the file
		highlight_file($filename, True), PHP_EOL,

		// What is the last known error?
		'Last error is for highlight_file() if it is on line 25 :', PHP_EOL, print_r(error_get_last(), True), PHP_EOL;
}

examine($badScript, './badScript.php', -1, 1);
examine($badScript, './badScript.php', -1, 0);
examine($badScript, './badScript.php', 0, 1);
examine($badScript, './badScript.php', 0, 0);
unlink('./badScript.php');

// Turn off all error reporting, display and logging.
error_reporting(0);
ini_set('display_errors', 0);
ini_set('log_errors', 0);

// Generate an error.
echo mktime(), PHP_EOL, 'Last error is for mktime() if it is on line 43 : ', PHP_EOL, print_r(error_get_last(), True), PHP_EOL;


Expected result:
----------------
error_reporting = -1
display_errors  = 1
log_errors      = 1

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :


error_reporting = -1
display_errors  = 0
log_errors      = 0

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :


error_reporting = 0
display_errors  = 1
log_errors      = 1

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :


error_reporting = 0
display_errors  = 0
log_errors      = 0

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error :


1309341644


Actual result:
--------------
error_reporting = -1
display_errors  = 1
log_errors      = 1

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_string() if it is on line 19 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 19
)

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br />
Warning: Unterminated comment starting line 2 in Z:\ts1.php on line 25
</span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_file() if it is on line 25 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 25
)

error_reporting = -1
display_errors  = 0
log_errors      = 0

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_string() if it is on line 19 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 19
)

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_file() if it is on line 25 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 25
)

error_reporting = 0
display_errors  = 1
log_errors      = 1

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_string() if it is on line 19 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 19
)

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_file() if it is on line 25 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 25
)

error_reporting = 0
display_errors  = 0
log_errors      = 0

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_string() if it is on line 19 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 19
)

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #FF9900">/*&nbsp;Broken&nbsp;comment
<br /></span>
</span>
</code>
Last error is for highlight_file() if it is on line 25 :
Array
(
    [type] => 128
    [message] => Unterminated comment starting line 2
    [file] => Z:\ts1.php
    [line] => 25
)

1309342054
Last error is for mktime() if it is on line 43 : 
Array
(
    [type] => 2048
    [message] => mktime(): You should be using the time() function instead
    [file] => Z:\ts1.php
    [line] => 43
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-18 11:51 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-10-18 11:51 UTC] cmb@php.net
See <https://3v4l.org/gHjNG> for a revised script (most notably it
calls error_clear_last()), and its results.  The only difference
is for error_reporting=-1 and display_errors=1, where
highlight_file() raises E_WARNING, while highlight_string() does
not.  This is because for some reason error_reporting is
temporarily set to E_ERROR for highlight_string(), but not for
highlight_file().  Maybe just an oversight in the respective
commit[1]?  It seems to me this inconsistency should be fixed, but
given the long standing behavior, which might be deliberately
employed by existing code, not for a stable PHP version (at least
not for PHP 7.4)

Note that the difference as of PHP 8.0.0 is because $badScript
no longer raises E_WARNING, but rather a ParseError[1].

[1] <https://github.com/php/php-src/commit/b03d3fa5b7a2bbfc69c069c992a39b2c3b1b8967>
[2] <https://3v4l.org/KE5Kq>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 22:01:27 2024 UTC