|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61612 execution of code is inconsistent
Submitted: 2012-04-03 19:21 UTC Modified: 2013-02-18 00:35 UTC
From: jrsmeets at xs4all dot nl Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.3.10 OS: Linux 2.6.32 Debian
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-04-03 19:21 UTC] jrsmeets at xs4all dot nl
I've been hit several times by what looks like a bug in the the scripting engine, or maybe an over-active post-parsing optimizer.

What I see are error messages in my httpd log that make no sense when I inspect the program to check the offending code.

Isolating the code in a small script does not replicate these problems: they only occur in the environment of the application I'm working on.

	# make sure that $n is a numeric scalar
	if (!isset($n) || !is_numeric($n)) {
		return $this;
	# some code omitted, no changes to $n here
	if (!isset($testvalue) || !is_array($testvalue) || !array_key_exists($n, $testvalue)) { return $this; } # line 174

And PHP complains thusly in the error log:

array_key_exists(): The first argument should be either a string or an integer in [FILENAME.php] on line 174

As you can see: the first argument of array_key_exists() in line 174 is certainly a scalar, so why complain?

Another one:

	$i = $first; # $first is an integer
	# some code omitted, no changes to $i here
	while ($i <= $last) {
		# some code omitted, no changes to $i here
		$displayName_utf8 = (is_array($displayNames) && array_key_exists($i, $displayNames) && isset($displayNames[$i])) ? bc_utf8($displayNames[$i]) : $i;
		# some code omitted, no changes to $i here			
		$i ++;

produces this error message:

array_key_exists(): The first argument should be either a string or an integer in ...

Again this makes no sense: $i is an integer all the way.

I've also had problems with code like this

if (is_array($a) && is_scalar($k) && array_key_exists($k, $a)) {

And PHP complains that the second argument to array_key_exists() must be an array, or in other cases that the first argument should be a string or int, which it is!

I've changed code like that to 
if (isset($a[$k])) {
AFAIK, that is almost the same thing, except when $a is an object or $k is set, but not a scalar.

Last example:

	$res = sendMailToUser(); # returns -1, 0 or 1
	if ($res == -1) {
		$errorMessage = 'Action not allowed';
	else if ($res == 0) {
		$errorMessage = 'Mail could not be sent';
	else {
		$errorMessage = 'Mail has been sent';
	echo $res.'<br />';
	echo $errorMessage.'<br />';

Expected result:
any of the three $res values with the corresponding $errorMessage

Actual result:
Action not allowed

Those two values do not belong together. [I'm not making this up.]

I've been able to work around most of these issue by changing the code a little bit. Like in the last example, I wrote 

	$res = 0 + sendMailToUser();

and that solved the problem.

All of the other issues could also be solved by making this type of ridiculous changes to the code. But that does not solve the underlying issue. And new issues might popup anywhere.

My setup: [from phpinfo()]

Apache/2.2.16 (Debian)

PHP Version 5.3.3-7+squeeze7 [ = 5.3.10]

Suhosin Patch, with Suhosin v0.9.32.1
Zend Engine v2.3.0

System 	Linux 2.6.32-5-686 #1 SMP Mon Jan 16 16:04:25 UTC 2012 i686
Build Date 	Feb 2 2012 18:20:23 
Additional .ini files parsed 	/etc/php5/apache2/conf.d/gd.ini, /etc/php5/apache2/conf.d/imap.ini, /etc/php5/apache2/conf.d/mcrypt.ini, /etc/php5/apache2/conf.d/mysql.ini, /etc/php5/apache2/conf.d/mysqli.ini, /etc/php5/apache2/conf.d/pdo.ini, /etc/php5/apache2/conf.d/pdo_mysql.ini, /etc/php5/apache2/conf.d/suhosin.ini 
PHP API 	20090626
PHP Extension 	20090626
Zend Extension 	220090626
Zend Extension Build 	API220090626,NTS
PHP Extension Build 	API20090626,NTS
Debug Build 	no
Thread Safety 	disabled
Zend Memory Manager 	enabled
Zend Multibyte Support 	disabled
IPv6 Support 	enabled
Registered PHP Streams 	https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports 	tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
Registered Stream Filters 	zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, mcrypt.*, mdecrypt.* 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-03 19:53 UTC]
-Status: Open +Status: Suspended
 [2012-04-03 19:53 UTC]
Sorry, without some sort of reproducable code snippet I don't see anything to go 
on here. I can't recall any similar bug reports from anyone either.
 [2012-04-03 21:43 UTC] jrsmeets at xs4all dot nl
Dear Rasmus,

I expected that this would be difficult. I've searched this website and I also did some google searches, and I found nothing like these bugs.

I'll try to install the application on another machine, which has Linux 2.4.31/Slackware 10.2.0 and PHP 5.2.11. Let's see what happens there.

As I said, I've already tried to create the bug in a small script and failed, but maybe in slightly bigger test scripts these bugs will surface.

You can keep the ticket in suspended state for a while. And I'll try to report my findings within a few weeks.
 [2012-04-04 05:15 UTC]
-Status: Suspended +Status: Feedback
 [2012-04-04 05:15 UTC]
Just because it passes a scalar or numeric test, it doesn't mean it's an integer 
or string, it could well be a float or anything...

$ php -d error_reporting=-1 -r '$a=[]; array_key_exists(1.1,$a);'

Warning: array_key_exists(): The first argument should be either a string or an 
integer in Command line code on line 1

In any case we'd need a reproduce script, but I bet your variables do not 
contain what you expect them to contain.
 [2013-02-18 00:35 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 24 19:01:29 2024 UTC