|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-07-06 08:57 UTC] marc-bennewitz at arcor dot de
Description:
------------
With some handlers it's not possible to iterate over items if there will be items deleted within iteration.
Test script:
---------------
// Script to delete items with specific prefix
$dba = dba_open(sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('zfcache_dba_'), 'c', 'qdbm');
dba_insert('key1', 'value', $dba);
dba_insert('test2', 'value', $dba);
dba_insert('key3', 'value', $dba);
dba_insert('test4', 'value', $dba);
dba_insert('key5', 'value', $dba);
$key = dba_firstkey($dba);
var_dump($key);
while ($key !== false) {
$key = dba_nextkey($dba);
var_dump($key);
}
echo '#############################' . PHP_EOL;
$key = dba_firstkey($dba);
while ($key !== false) {
if (substr($key, 0, 3) === 'key') {
var_dump($key);
dba_delete($key, $dba);
}
$key = dba_nextkey($dba);
}
echo '#############################' . PHP_EOL;
$key = dba_firstkey($dba);
var_dump($key);
while ($key !== false) {
$key = dba_nextkey($dba);
var_dump($key);
}
Expected result:
----------------
string(4) "key1"
string(5) "test2"
string(4) "key3"
string(5) "test4"
string(4) "key5"
bool(false)
#############################
string(4) "key1"
string(4) "key3"
string(4) "key5"
#############################
string(5) "test2"
string(5) "test4"
bool(false)
Actual result:
--------------
flatfile -> correct
inifile -> wrong (breaks iteration after first delete)
gdbm -> wrong (breaks iteration after first delete)
qdbm -> correct
db4 -> correct
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 14:00:01 2025 UTC |
Two failing assumptions here: .) Changing the set of an active iterator usually results in undefined behaviour. .) Trying to fetch the next item after deleting the current. So, gdbm seems to work if you fetch the next kex prior delete, but, anyway, the inifile handler fails. I'll have a look at the inifile handler, but am inclined to mark this report as "Won't fix". Changed test script: <?php foreach (["gdbm","db4","flatfile","inifile"] as $hnd) { echo "###################### $hnd ########################\n"; $dba = dba_open(sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('zfcache_dba_'), 'c', $hnd); if (!$dba) continue; dba_insert('key1', 'value', $dba); dba_insert('test2', 'value', $dba); dba_insert('key3', 'value', $dba); dba_insert('test4', 'value', $dba); dba_insert('key5', 'value', $dba); $key = dba_firstkey($dba); var_dump($key); while ($key) { $key = dba_nextkey($dba); $key && var_dump($key); } $key = dba_firstkey($dba); while ($key) { $nxt = dba_nextkey($dba); if (substr($key, 0, 3) === 'key') { dba_delete($key, $dba); } $key = $nxt; } echo "\n### after delete\n"; $key = dba_firstkey($dba); var_dump($key); while ($key) { $key = dba_nextkey($dba); $key && var_dump($key); } echo "\n\n"; }