php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44100 Inconsistent handling of static array declarations with duplicate keys
Submitted: 2008-02-11 17:38 UTC Modified: 2008-08-01 14:24 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: robin_fernandes at uk dot ibm dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2CVS-2008-02-11 (snap) OS: Windows
Private report: No CVE-ID: None
 [2008-02-11 17:38 UTC] robin_fernandes at uk dot ibm dot com
Description:
------------
There is an inconsistency when handling array declarations that include multiple entries with the same key but different values, such as array('a'=>0, 'a'=>1). In almost all cases, the last value will be used (1 in the example above).

But the last part of the testcase below shows an inconsistent case, where the array is a static variable AND the first duplicate key is a constant rather than a literal.

This is because in zval_update_constant_ex(), array entries with keys marked IS_CONSTANT_INDEX are always updated, regardless of the presence and position of other elements with the same key.

Reproduce code:
---------------
<?php
echo "Duplicate key in non static array declaration:\n";
$nonStatic1 = array('a'=>0, 'a'=>1);
print_r($nonStatic1); // [a] => 1 --> OK

echo "\nDuplicate key in static array declaration:\n";
static $static1 = array('a'=>0, 'a'=>1);
print_r($static1); // [a] => 1 --> OK

define('DUP_KEY', 'a');
echo "\nDuplicate key in non static array declaration using constant as first key:\n";
$nonStatic2 = array(DUP_KEY=>0, 'a'=>1);
print_r($nonStatic2); // [a] => 1 --> OK

echo "\nDuplicate key in static array declaration using constant as first key:\n";
static $static2 = array(DUP_KEY=>0, 'a'=>1);
print_r($static2); // [a] => 0 --> bug?
?>

Expected result:
----------------
Duplicate key in non static array declaration:
Array
(
    [a] => 1
)

Duplicate key in static array declaration:
Array
(
    [a] => 1
)

Duplicate key in non static array declaration using constant as first key:
Array
(
    [a] => 1
)

Duplicate key in static array declaration using constant as first key:
Array
(
    [a] => 1
)


Actual result:
--------------
Duplicate key in non static array declaration:
Array
(
    [a] => 1
)

Duplicate key in static array declaration:
Array
(
    [a] => 1
)

Duplicate key in non static array declaration using constant as first key:
Array
(
    [a] => 1
)

Duplicate key in static array declaration using constant as first key:
Array
(
    [a] => 0
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-01 14:24 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_3 (it won't be fixed in PHP_5_2).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC