Skip to content

Commit ef29ddc

Browse files
committed
Fix GH-8068: mysqli_fetch_object creates inaccessible properties
When fetching into objects, we need to create object style hash tables, i.e. where numeric column names are stored as string keys instead of integer keys. Instead of the slightly more efficient alternative to create the desired hash table in the first place, we go for the more readable implementation and convert the array style hash table using `zend_symtable_to_proptable()`. Co-authored-by: Kamil Tekiela <[email protected]> Closes GH-8189.
1 parent 2b7431c commit ef29ddc

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ PHP NEWS
1111
- Intl:
1212
. Fixed bug GH-8142 (Compilation error on cygwin). (David Carlier)
1313

14+
- MySQLi:
15+
. Fixed bug GH-8068 (mysqli_fetch_object creates inaccessible properties).
16+
(cmb)
17+
1418
- Pcntl:
1519
. Fixed bug GH-8142 (Compilation error on cygwin). (David Carlier)
1620

ext/mysqli/mysqli.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,11 +1198,13 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags
11981198
ZVAL_COPY_VALUE(&dataset, return_value);
11991199

12001200
object_init_ex(return_value, ce);
1201+
HashTable *prop_table = zend_symtable_to_proptable(Z_ARR(dataset));
1202+
zval_ptr_dtor(&dataset);
12011203
if (!ce->default_properties_count && !ce->__set) {
1202-
Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
1204+
Z_OBJ_P(return_value)->properties = prop_table;
12031205
} else {
1204-
zend_merge_properties(return_value, Z_ARRVAL(dataset));
1205-
zval_ptr_dtor(&dataset);
1206+
zend_merge_properties(return_value, prop_table);
1207+
zend_array_release(prop_table);
12061208
}
12071209

12081210
if (ce->constructor) {

ext/mysqli/tests/gh8068.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-8068 (mysqli_fetch_object creates inaccessible properties)
3+
--SKIPIF--
4+
<?php
5+
require_once 'skipif.inc';
6+
require_once 'skipifconnectfailure.inc';
7+
?>
8+
--FILE--
9+
<?php
10+
require_once "connect.inc";
11+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
12+
$mysqli = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
13+
$res = $mysqli->query('SELECT 42');
14+
$obj = $res->fetch_object();
15+
var_dump(
16+
$obj,
17+
$obj->{42}
18+
);
19+
?>
20+
--EXPECT--
21+
object(stdClass)#4 (1) {
22+
["42"]=>
23+
string(2) "42"
24+
}
25+
string(2) "42"

0 commit comments

Comments
 (0)