php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62491 Iterating over items + dba_delete not working
Submitted: 2012-07-06 08:57 UTC Modified: 2013-12-03 13:45 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: marc-bennewitz at arcor dot de Assigned:
Status: Wont fix Package: DBM/DBA related
PHP Version: 5.4.4 OS: openSUSE 11.3 (x86_64)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
12 + 26 = ?
Subscribe to this entry?

 
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-03 13:13 UTC] mike@php.net
-Status: Open +Status: Analyzed
 [2013-12-03 13:13 UTC] mike@php.net
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";
}
 [2013-12-03 13:45 UTC] mike@php.net
-Status: Analyzed +Status: Wont fix
 [2013-12-03 13:45 UTC] mike@php.net
Yes, so the inifile handler directly operates on the file stream.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC