Skip to content

Commit 29a96e0

Browse files
committed
Fix GH-11451: Invalid associative array containing duplicate keys
It used the "add_new" variant which assumes the key doesn't already exist. But in case of duplicate keys we have to take the last result. Closes GH-11453.
1 parent 923e726 commit 29a96e0

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ PHP NEWS
6363
. Fix access on NULL pointer in array_merge_recursive(). (ilutov)
6464
. Fix exception handling in array_multisort(). (ilutov)
6565

66+
- SQLite3:
67+
. Fixed bug GH-11451 (Invalid associative array containing duplicate
68+
keys). (nielsdos)
69+
6670
08 Jun 2023, PHP 8.2.7
6771

6872
- Core:

ext/sqlite3/sqlite3.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,9 @@ PHP_METHOD(SQLite3Result, fetchArray)
19821982
Z_ADDREF(data);
19831983
}
19841984
}
1985-
zend_symtable_add_new(Z_ARR_P(return_value), result_obj->column_names[i], &data);
1985+
/* Note: we can't use the "add_new" variant here instead of "update" because
1986+
* when the same column name is encountered, the last result should be taken. */
1987+
zend_symtable_update(Z_ARR_P(return_value), result_obj->column_names[i], &data);
19861988
}
19871989
}
19881990
break;

ext/sqlite3/tests/gh11451.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-11451 (Invalid associative array containing duplicate keys)
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
var_dump((new SQLite3(':memory:'))
8+
->query('SELECT 1 AS key, 2 AS key')
9+
->fetchArray(SQLITE3_ASSOC));
10+
11+
var_dump((new SQLite3(':memory:'))
12+
->query('SELECT 0 AS dummy, 1 AS key, 2 AS key')
13+
->fetchArray(SQLITE3_ASSOC));
14+
?>
15+
--EXPECT--
16+
array(1) {
17+
["key"]=>
18+
int(2)
19+
}
20+
array(2) {
21+
["dummy"]=>
22+
int(0)
23+
["key"]=>
24+
int(2)
25+
}

0 commit comments

Comments
 (0)