Bug #52867 [Com]: array_intersect_ukey does not check all keys

From: Date: Wed, 13 Oct 2010 02:17:06 +0000
Subject: Bug #52867 [Com]: array_intersect_ukey does not check all keys
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-155126@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52867&edit=1

 ID:                 52867
 Comment by:         uramihsayibok at gmail dot com
 Reported by:        jason at linuxbox dot com
 Summary:            array_intersect_ukey does not check all keys
 Status:             Open
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

Not a bug.

The manual page for array_intersect_ukey says
>It must return an integer less than, equal to, or greater than zero if
the
>first key is considered to be respectively less than, equal to, or
greater than
>the second. 
Your callback does not do that and by violating the postcondition you
get undefined behavior.

function func($key1, $key2) {
  // equivalent to 'return strcmp($key1, $key2);'
  if ($key1 == $key2) {
    return 0;
  } else if ($key1 < $key2) {
    return -1;
  } else /* if ($key1 > $key2) */ {
    return 1;
  }
}


Previous Comments:
------------------------------------------------------------------------
[2010-09-17 04:18:19] jason at linuxbox dot com

Description:
------------
The array_intersect_ukey() function appears to have an invalid algorithm
for 
iterating over array keys to be passed to the user function.  A simple
test case 
that implements the same behavior as array_intersect_key() illustrates
the bug.

Test script:
---------------
<?php
function func($key1, $key2) {
  if ($key1 == $key2) {
    return 0;
  }
  return 1;
} 

$all = array('cat' => 'brown',
  'dog' => 'yellow',
  'mouse' => 'grey',
  'horse' => 'white'
);

$keep = array('dog' => 1, 'horse' => 1);

// Output of the following should be identical
print_r(array_intersect_key($all, $keep));
print_r(array_intersect_ukey($all, $keep, 'func'));
?>

Expected result:
----------------
Array
(
    [dog] => yellow
    [horse] => white
)
Array
(
    [dog] => yellow
    [horse] => white
)

Actual result:
--------------
Array
(
    [dog] => yellow
    [horse] => white
)
Array
(
)


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52867&edit=1


Thread (3 messages)

« previous php.bugs (#155126) next »