Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,18 @@ static bool pdo_call_fetch_object_constructor(zend_function *constructor, HashTa
return failed;
}

/* Whether a column name starts with "\0", the prefix of mangled property names */
static bool pdo_any_column_name_starts_with_nul(const pdo_stmt_t *stmt)
{
for (int i = 0; i < stmt->column_count; i++) {
const zend_string *name = stmt->columns[i].name;
if (UNEXPECTED(ZSTR_LEN(name) != 0 && ZSTR_VAL(name)[0] == '\0')) {
return true;
}
}
return false;
}

/* Performs a row fetch, the value is stored into return_value according to HOW.
* return_value MUST be safely destroyable as it will be freed if an error occurs. */
static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type how, enum pdo_fetch_orientation ori, zend_long offset, zval *group_key) /* {{{ */
Expand Down Expand Up @@ -747,7 +759,13 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h

case PDO_FETCH_OBJ:
ce = zend_standard_class_def;
object_init(return_value);
if (!pdo_any_column_name_starts_with_nul(stmt)) {
/* Collect the row into the properties table, the object is created after the loop.
* With a group key the first column is the key, not a property. */
array_init_size(return_value, stmt->column_count - (group_key != NULL));
} else {
object_init(return_value);
}
break;

case PDO_FETCH_CLASS:
Expand Down Expand Up @@ -912,8 +930,13 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &val);
break;

case PDO_FETCH_CLASS:
case PDO_FETCH_OBJ:
if (Z_TYPE_P(return_value) == IS_ARRAY) {
zend_hash_update(Z_ARRVAL_P(return_value), column_name, &val);
break;
}
ZEND_FALLTHROUGH;
case PDO_FETCH_CLASS:
case PDO_FETCH_INTO:
zend_update_property_ex(ce, Z_OBJ_P(return_value), column_name, &val);
zval_ptr_dtor(&val);
Expand All @@ -926,6 +949,10 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
}
}

if (how == PDO_FETCH_OBJ && Z_TYPE_P(return_value) == IS_ARRAY) {
object_and_properties_init(return_value, zend_standard_class_def, Z_ARRVAL_P(return_value));
}

/* Run constructor for objects if not already run and not unserialized */
if (how == PDO_FETCH_CLASS && ce->constructor && !(flags & (PDO_FETCH_PROPS_LATE | PDO_FETCH_SERIALIZE))) {
bool failed = pdo_call_fetch_object_constructor(ce->constructor, ctor_arguments, Z_OBJ_P(return_value));
Expand Down
73 changes: 73 additions & 0 deletions ext/pdo_sqlite/tests/pdo_sqlite_fetch_obj_column_names.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
--TEST--
PDO_sqlite: PDO::FETCH_OBJ with duplicate, numeric and empty column names
--EXTENSIONS--
pdo_sqlite
--FILE--
<?php
$db = new PDO('sqlite::memory:');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$row = $db->query('SELECT 1 AS a, 2 AS a, 3 AS "0", 4 AS "01", 5 AS "", 6 AS "-1"')->fetch(PDO::FETCH_OBJ);
var_dump($row);
var_dump($row->{'0'}, (array) $row);

$row->a = 'changed';
$row->extra = true;
unset($row->{'01'});
var_dump($row);

var_dump($db->query('SELECT 1 AS g, 2 AS v UNION ALL SELECT 1, 3')->fetchAll(PDO::FETCH_OBJ | PDO::FETCH_GROUP));
?>
--EXPECTF--
object(stdClass)#%d (5) {
["a"]=>
int(2)
["0"]=>
int(3)
["01"]=>
int(4)
[""]=>
int(5)
["-1"]=>
int(6)
}
int(3)
array(5) {
["a"]=>
int(2)
[0]=>
int(3)
["01"]=>
int(4)
[""]=>
int(5)
[-1]=>
int(6)
}
object(stdClass)#%d (5) {
["a"]=>
string(7) "changed"
["0"]=>
int(3)
[""]=>
int(5)
["-1"]=>
int(6)
["extra"]=>
bool(true)
}
array(1) {
[1]=>
array(2) {
[0]=>
object(stdClass)#%d (1) {
["v"]=>
int(2)
}
[1]=>
object(stdClass)#%d (1) {
["v"]=>
int(3)
}
}
}
Loading