php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25725 highlight_string doesn't parse strings containing \ properly
Submitted: 2003-10-02 07:17 UTC Modified: 2004-11-12 08:15 UTC
Votes:341
Avg. Score:5.0 ± 0.1
Reproduced:341 of 341 (100.0%)
Same Version:340 (99.7%)
Same OS:340 (99.7%)
From: kjartan at zind dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS, 5CVS OS: *
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: kjartan at zind dot net
New email:
PHP Version: OS:

 

 [2003-10-02 07:17 UTC] kjartan at zind dot net
Description:
------------
Given a string that contains this PHP code:

<?php
$a = 2;
\;
$b = 3;
?>

Then highlight_string() generates this:

<?php
$a = 2;

Warning: Unexpected character in input:  '\' (ASCII=92) state=1 in /home/kjartan/scripts/highlight_string on line 9
;
$b = 3;
?>

Granted its not valid PHP, but should hightlight_string() function as a PHP validation system? Not that it does a good job at it as this gets colored just fine:

<?php
$a = 2
$b = 3
$c = 5
?>

What gives?

Reproduce code:
---------------
<?php
$string = '<?php
$a = 2;
\
$c = 5;
?>';

highlight_string($string);


Expected result:
----------------
I expect it to just leave the \ alone and not cause warnings.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-02 16:51 UTC] moriyoshi@php.net
If you just want to suppress warnings, error_reporting(0) might help you. I'm not sure if this needs to be fixed anyhow though...

 [2003-10-02 17:03 UTC] kjartan at zind dot net
Doing @highlight_string seems to work to, but it strips the \. Might not fixing, but it still seems like a bug.
 [2003-10-02 20:56 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The warning you are seeing is a lexical parser warning, it cannot be avoided simply because it is the same parser as the one parsing normal PHP source. If PHP were to execute the file it too would strip/ignore the \ character.
 [2004-09-25 06:53 UTC] keithm at aoeex dot com
This bug is not bogus.  We are experiencing it semi-randomly with our pastebin code at http://dalphp.shoggoth.net/pastebin.php (http://dalphp.shoggoth.net/pastebin_view.php?356 is one post being affected)

The code that is paste is generating the warning for \'s which are valid.  In this case, the \ in the \n in the print statements.

Here's some debugging code/output I added to see if this was a bug or not.

-=-=-= code -=-=-=-=-
$src=implode("\n", $Source);
echo '<!-- Source: ', NL, $src, '-->';
if (strpos($src, '<?php') !== false || strpos($src, '<?') !== false){
  $source=highlight_string($src, 1);
  echo '<!-- Source 2: ', NL, $source, ' -->';
  $source=str_replace(array('<code>', '</code>'), '', $source);
}
else {
  $source=nl2br(htmlentities($src));
}
echo $src;
-=-=-=-=-=-=-=-=-=-=-

-=-=-= output -=-=-=-=-
<!-- Source: 
<?php

/* Trying to figure out wtf is thrown when an array is accessed out of bounds. */

print "Before!\n";
    
try {
    print "Trying!\n";
    $x = array(1, 2, 3);
    print_r($x);
    $y = $x[3];
    print_r($y);
}
catch(Exception $e) {
    print "Cought!\n";
    print_r($e);
}

print "Done!\n";

/* Expected output:
 *   Before!
 *   Trying!
 *   Array ( etc.
 *   Cought!
 *   Object ( etc.
 *   Done!
 *
 * Actual output:
 *   Before!
 *   Trying!
 *   Array ( etc.
 *   Done!
 */

?>--><!-- Source 2: 
<code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />/* Trying to figure out wtf is thrown when an array is accessed out of bounds. */<br /><br />print </font><font color="#DD0000">"Before!<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/home/trollboy/public_html/dalphp/pastebin_view.php</b> on line <b>153</b><br />
n";<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />try {<br />&nbsp;&nbsp;&nbsp;&nbsp;print "Trying!<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/home/trollboy/public_html/dalphp/pastebin_view.php</b> on line <b>153</b><br />
n"</font><font color="#0000BB">;<br />&nbsp;&nbsp;&nbsp;&nbsp;$x = array(1, 2, 3);<br />&nbsp;&nbsp;&nbsp;&nbsp;print_r($x);<br />&nbsp;&nbsp;&nbsp;&nbsp;$y = $x</font><font color="#007700">[</font><font color="#0000BB">3</font><font color="#007700">]</font><font color="#0000BB">;<br />&nbsp;&nbsp;&nbsp;&nbsp;print_r($y);<br /></font><font color="#007700">}</font><font color="#0000BB"><br />catch(Exception $e) </font><font color="#007700">{</font><font color="#0000BB"><br />&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"Cought!<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/home/trollboy/public_html/dalphp/pastebin_view.php</b> on line <b>153</b><br />
n"</font><font color="#0000BB">;<br />&nbsp;&nbsp;&nbsp;&nbsp;print_r($e);<br /></font><font color="#007700">}</font><font color="#0000BB"><br /><br />print </font><font color="#DD0000">"Done!<br />
<b>Warning</b>:  Unexpected character in input:  '\' (ASCII=92) state=1 in <b>/home/trollboy/public_html/dalphp/pastebin_view.php</b> on line <b>153</b><br />
n"</font><font color="#0000BB">;<br /><br />/* Expected output:<br /> *&nbsp;&nbsp;&nbsp;Before!<br /> *&nbsp;&nbsp;&nbsp;Trying!<br /> *&nbsp;&nbsp;&nbsp;Array ( etc.<br /> *&nbsp;&nbsp;&nbsp;Cought!<br /> *&nbsp;&nbsp;&nbsp;Object ( etc.<br /> *&nbsp;&nbsp;&nbsp;Done!<br /> *<br /> * Actual output:<br /> *&nbsp;&nbsp;&nbsp;Before!<br /> *&nbsp;&nbsp;&nbsp;Trying!<br /> *&nbsp;&nbsp;&nbsp;Array ( etc.<br /> *&nbsp;&nbsp;&nbsp;Done!<br /> */<br /><br />?&gt;</font>
</font>
</code> -->
-=-=-=-=-=-=-=-=-=-=-=-


As you can see, the highlight_string call is causing the warning to be injected into the source where they should not be.  The code is fine under normal circumstances.
 [2004-09-25 07:03 UTC] jed at jed dot bz
I am the submitter of Keith's failed post.
The problem only produces itself in Mozilla 1.0 PR.
When I viewed this page in Internet Explorer, the problem was not there.

As proof, I have taken a screenshot:
http://labs.jed.bz/phpbug.png

PHP developers are too quick to mark bugs bogus. Please review this bug's status.
 [2004-09-25 07:05 UTC] jed at jed dot bz
I apologize, an addendum: Mozilla *Firefox* 1.0 PR.
 [2004-09-27 13:37 UTC] kjartan at zind dot net
Still seems to be happening, both for me and for others.
 [2004-10-18 02:43 UTC] Stephen2417 at gmail dot com
This problem still exists in 4.3.9, and I beleive it has been fixed in 5.*. Half the time just shows up blank, so this is a definite bug.
 [2004-10-18 02:50 UTC] Stephen2417 at gmail dot com
[Correction] This problem still exists in 5.*.
 [2004-10-18 03:41 UTC] admin at cpgnuke dot com
Correct this is a annoying bug because it happens randomly.
My CVS viewer and BB just throw the error occasional without a pattern.

I also found additional problems so maybe someone can trace it.
when you have "<?php" in top sometimes the whole text has blue color and a different time it is in 2 colors (<? = orange, php = blue)
And also the content between <?php and ?> is sometimes colored and sometimes completely blue.

To me this recognizes that the function has trouble with converting the text.

But where does the conversion goes bad in base_functions.c:
zend_parse_parameters(), highlight_string() or even before that ????
 [2004-11-10 01:34 UTC] Stephen2417 at gmail dot com
Bumping.. This is still an issue.
 [2004-11-12 08:15 UTC] curt@php.net
There is no issue.. simply running:
<?php
$a = 2;
\
$c = 5;
?>

does the same as a php script or running it through highlight_string. stray '\'s outside of a string isn't allowed.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 20:01:32 2024 UTC