Skip to content

Commit 5a420c8

Browse files
committed
fix bug #62490
inifiles delete handler did not return false if the key was not found
1 parent f0c85fa commit 5a420c8

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

ext/dba/dba_inifile.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,15 @@ DBA_EXISTS_FUNC(inifile)
124124
DBA_DELETE_FUNC(inifile)
125125
{
126126
int res;
127+
zend_bool found = 0;
127128

128129
INIFILE_DATA;
129130
INIFILE_GKEY;
130131

131-
res = inifile_delete(dba, &ini_key TSRMLS_CC);
132+
res = inifile_delete_ex(dba, &ini_key, &found TSRMLS_CC);
132133

133134
INIFILE_DONE;
134-
return (res == -1 ? FAILURE : SUCCESS);
135+
return (res == -1 || !found ? FAILURE : SUCCESS);
135136
}
136137

137138
DBA_FIRSTKEY_FUNC(inifile)

ext/dba/libinifile/inifile.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ static int inifile_copy_to(inifile *dba, size_t pos_start, size_t pos_end, inifi
413413
/* {{{ inifile_filter
414414
* copy from to dba while ignoring key name (group must equal)
415415
*/
416-
static int inifile_filter(inifile *dba, inifile *from, const key_type *key TSRMLS_DC)
416+
static int inifile_filter(inifile *dba, inifile *from, const key_type *key, zend_bool *found TSRMLS_DC)
417417
{
418418
size_t pos_start = 0, pos_next = 0, pos_curr;
419419
int ret = SUCCESS;
@@ -424,6 +424,9 @@ static int inifile_filter(inifile *dba, inifile *from, const key_type *key TSRML
424424
while(inifile_read(from, &ln TSRMLS_CC)) {
425425
switch(inifile_key_cmp(&ln.key, key TSRMLS_CC)) {
426426
case 0:
427+
if (found) {
428+
*found = (zend_bool) 1;
429+
}
427430
pos_curr = php_stream_tell(from->fp);
428431
if (pos_start != pos_next) {
429432
php_stream_seek(from->fp, pos_start, SEEK_SET);
@@ -458,7 +461,7 @@ static int inifile_filter(inifile *dba, inifile *from, const key_type *key TSRML
458461

459462
/* {{{ inifile_delete_replace_append
460463
*/
461-
static int inifile_delete_replace_append(inifile *dba, const key_type *key, const val_type *value, int append TSRMLS_DC)
464+
static int inifile_delete_replace_append(inifile *dba, const key_type *key, const val_type *value, int append, zend_bool *found TSRMLS_DC)
462465
{
463466
size_t pos_grp_start=0, pos_grp_next;
464467
inifile *ini_tmp = NULL;
@@ -516,7 +519,7 @@ static int inifile_delete_replace_append(inifile *dba, const key_type *key, cons
516519
if (key->name && strlen(key->name)) {
517520
/* 6 */
518521
if (!append && ini_tmp) {
519-
ret = inifile_filter(dba, ini_tmp, key TSRMLS_CC);
522+
ret = inifile_filter(dba, ini_tmp, key, found TSRMLS_CC);
520523
}
521524

522525
/* 7 */
@@ -563,23 +566,39 @@ static int inifile_delete_replace_append(inifile *dba, const key_type *key, cons
563566
*/
564567
int inifile_delete(inifile *dba, const key_type *key TSRMLS_DC)
565568
{
566-
return inifile_delete_replace_append(dba, key, NULL, 0 TSRMLS_CC);
569+
return inifile_delete_replace_append(dba, key, NULL, 0, NULL TSRMLS_CC);
570+
}
571+
/* }}} */
572+
573+
/* {{{ inifile_delete_ex
574+
*/
575+
int inifile_delete_ex(inifile *dba, const key_type *key, zend_bool *found TSRMLS_DC)
576+
{
577+
return inifile_delete_replace_append(dba, key, NULL, 0, found TSRMLS_CC);
567578
}
568579
/* }}} */
569580

570581
/* {{{ inifile_relace
571582
*/
572583
int inifile_replace(inifile *dba, const key_type *key, const val_type *value TSRMLS_DC)
573584
{
574-
return inifile_delete_replace_append(dba, key, value, 0 TSRMLS_CC);
585+
return inifile_delete_replace_append(dba, key, value, 0, NULL TSRMLS_CC);
586+
}
587+
/* }}} */
588+
589+
/* {{{ inifile_replace_ex
590+
*/
591+
int inifile_replace_ex(inifile *dba, const key_type *key, const val_type *value, zend_bool *found TSRMLS_DC)
592+
{
593+
return inifile_delete_replace_append(dba, key, value, 0, found TSRMLS_CC);
575594
}
576595
/* }}} */
577596

578597
/* {{{ inifile_append
579598
*/
580599
int inifile_append(inifile *dba, const key_type *key, const val_type *value TSRMLS_DC)
581600
{
582-
return inifile_delete_replace_append(dba, key, value, 1 TSRMLS_CC);
601+
return inifile_delete_replace_append(dba, key, value, 1, NULL TSRMLS_CC);
583602
}
584603
/* }}} */
585604

ext/dba/libinifile/inifile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ val_type inifile_fetch(inifile *dba, const key_type *key, int skip TSRMLS_DC);
4949
int inifile_firstkey(inifile *dba TSRMLS_DC);
5050
int inifile_nextkey(inifile *dba TSRMLS_DC);
5151
int inifile_delete(inifile *dba, const key_type *key TSRMLS_DC);
52+
int inifile_delete_ex(inifile *dba, const key_type *key, zend_bool *found TSRMLS_DC);
5253
int inifile_replace(inifile *dba, const key_type *key, const val_type *val TSRMLS_DC);
54+
int inifile_replace_ex(inifile *dba, const key_type *key, const val_type *val, zend_bool *found TSRMLS_DC);
5355
int inifile_append(inifile *dba, const key_type *key, const val_type *val TSRMLS_DC);
5456
char *inifile_version();
5557

ext/dba/tests/bug62490.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Bug #62490 (dba_delete returns true on missing item (inifile))
3+
--SKIPIF--
4+
<?php
5+
$handler = "inifile";
6+
include "skipif.inc";
7+
?>
8+
--FILE--
9+
<?php
10+
$handler = "inifile";
11+
include "test.inc";
12+
13+
$dba = dba_open($db_filename, "n", $handler)
14+
or die;
15+
for ($i = 0; $i < 3; ++$i) {
16+
echo "insert $i:";
17+
var_dump(dba_insert("a", $i, $dba));
18+
}
19+
20+
echo "exists:";
21+
var_dump(dba_exists("a", $dba));
22+
echo "delete:";
23+
var_dump(dba_delete("a", $dba));
24+
echo "exists:";
25+
var_dump(dba_exists("a", $dba));
26+
echo "delete:";
27+
var_dump(dba_delete("a", $dba));
28+
29+
?>
30+
===DONE===
31+
--CLEAN--
32+
<?php
33+
include "clean.inc";
34+
?>
35+
--EXPECT--
36+
insert 0:bool(true)
37+
insert 1:bool(true)
38+
insert 2:bool(true)
39+
exists:bool(true)
40+
delete:bool(true)
41+
exists:bool(false)
42+
delete:bool(false)
43+
===DONE===

0 commit comments

Comments
 (0)