#23690 [NEW]: Config_Container::toArray() Bug + FIX :-)

From: Date: Mon, 19 May 2003 02:38:08 +0000
Subject: #23690 [NEW]: Config_Container::toArray() Bug + FIX :-)
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-16448@lists.php.net to get a copy of this message
From: info at rhalff dot com Operating system: All PHP version: 4.3.2RC3 PHP Bug Type: PEAR related Bug description: Config_Container::toArray() Bug + FIX :-) Reproduction of this bug: 1. Create the two files below: ------------------------- php_snippet.ini: [PHP] extension = bcmath.so extension = bz2.so extension = calendar.so extension = cdpf.so extension = crack.so extension = curl.so ------------------------- <?php require_once('Config.php'); $datasrc = 'php_snippet.ini'; $phpIni = new Config(); $root =& $phpIni->parseConfig($datasrc, 'inicommented'); if (PEAR::isError($root)) { die($root->getMessage()); } print_r($root->toArray()); ?> ------------------------ 2. Run test.php The result will be: Fatal error: [] operator not supported for strings in /usr/lib/php/Config/Container.php on line 658 Solution: Original Code: 650: foreach ($newArr as $key => $value) { 651: if (isset($array[$this->name][$key])) { 652: // duplicate name/type 653: if (!isset($array[$this->name][$key][0])) { 654: $old = $array[$this->name][$key]; 655: unset($array[$this->name][$key]); 656: $array[$this->name][$key][0] = $old; 657: } 658: $array[$this->name][$key][] = $value; 659: } elseif (isset($array[$this->name][$key][0])) { 660: $array[$this->name][$key][] = $value; 661: } else { 662: $array[$this->name][$key] = $value; 663: } 664: } Fixed Code: 650: foreach ($newArr as $key => $value) { 651: if (isset($array[$this->name][$key])) { 652: // duplicate name/type if(!is_array($array[$this->name][$key])) { $old = $array[$this->name][$key]; unset($array[$this->name][$key]); $array[$this->name][$key][0] = $old; } else { $array[$this->name][$key][] = $value; } 661: } else { 662: $array[$this->name][$key] = $value; 663: } 664: } Explaination: The original code checks if $array[$this->name][$key][0] is set and falsly assumes that if [0] exists it will be an array. In the example first it will set $array['PHP']['extension'] = bcmath.so The second time around it will detect that $array['PHP']['extension'] allready exists. But $array['PHP']['extension'][0] will be 'b' as $array['PHP']['extension'][1] will be 'c' and $array['PHP']['extension'][2] will be 'm' and $array['PHP']['extension'][3] will be 'a' and $array['PHP']['extension'][4] will be 't' and $array['PHP']['extension'][5] will be 'h' etc.. So when the code reaches $array[$this->name][$key][] = $value; $array[$this->name][$key] will still be a string and this explains the error message. That's why the check should be like: !is_array($array[$this->name][$key]) The rest of the changes in the Fixed code should be obvious. Greetings, Rob Halff -- Edit bug report at http://bugs.php.net/?id=23690&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=23690&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=23690&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=23690&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=23690&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=23690&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=23690&r=support Expected behavior: http://bugs.php.net/fix.php?id=23690&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=23690&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=23690&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=23690&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=23690&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=23690&r=dst IIS Stability: http://bugs.php.net/fix.php?id=23690&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=23690&r=gnused

« previous php.pear.dev (#16448) next »