Skip to content

Commit e4a13d0

Browse files
committed
Another round of review
Indentation Error message Use variadic number position for pdo_stmt_setup_fetch_mode() param cehcks Use values from *args directly instead of preassigning them in pdo_stmt_setup_fetch_mode() Drop todo comment
1 parent c0d2d8b commit e4a13d0

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#define PHP_STMT_GET_OBJ \
3838
pdo_stmt_t *stmt = Z_PDO_STMT_P(ZEND_THIS); \
3939
if (!stmt->dbh) { \
40-
zend_throw_error(NULL, "PDO Object is uninitialized"); \
40+
zend_throw_error(NULL, "PDO object is uninitialized"); \
4141
RETURN_THROWS(); \
4242
} \
4343

@@ -260,7 +260,7 @@ static int really_register_bound_param(struct pdo_bound_param_data *param, pdo_s
260260

261261
for (i = 0; i < stmt->column_count; i++) {
262262
if (ZSTR_LEN(stmt->columns[i].name) == ZSTR_LEN(param->name) &&
263-
strncmp(ZSTR_VAL(stmt->columns[i].name), ZSTR_VAL(param->name), ZSTR_LEN(param->name) + 1) == 0) {
263+
strncmp(ZSTR_VAL(stmt->columns[i].name), ZSTR_VAL(param->name), ZSTR_LEN(param->name) + 1) == 0) {
264264
param->paramno = i;
265265
break;
266266
}
@@ -404,9 +404,9 @@ PHP_METHOD(PDOStatement, execute)
404404

405405
if (PDO_PLACEHOLDER_NONE == stmt->supports_placeholders) {
406406
/* handle the emulated parameter binding,
407-
* stmt->active_query_string holds the query with binds expanded and
407+
* stmt->active_query_string holds the query with binds expanded and
408408
* quoted.
409-
*/
409+
*/
410410

411411
/* string is leftover from previous calls so PDOStatement::debugDumpParams() can access */
412412
if (stmt->active_query_string && stmt->active_query_string != stmt->query_string) {
@@ -730,7 +730,7 @@ static void do_fetch_opt_finish(pdo_stmt_t *stmt, int free_ctor_agrs) /* {{{ */
730730
/* fci.size is used to check if it is valid */
731731
if (stmt->fetch.cls.fci.size && stmt->fetch.cls.fci.params) {
732732
if (!Z_ISUNDEF(stmt->fetch.cls.ctor_args)) {
733-
/* Added to free constructor arguments */
733+
/* Added to free constructor arguments */
734734
zend_fcall_info_args_clear(&stmt->fetch.cls.fci, 1);
735735
} else {
736736
efree(stmt->fetch.cls.fci.params);
@@ -1155,7 +1155,7 @@ static bool pdo_stmt_verify_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode
11551155

11561156
case PDO_FETCH_LAZY:
11571157
if (fetch_all) {
1158-
zend_value_error("Cannot be PDO::FETCH_LAZY in PDOStatement::fetchAll()");
1158+
zend_argument_value_error(mode_arg_num, "cannot be PDO::FETCH_LAZY in PDOStatement::fetchAll()");
11591159
return 0;
11601160
}
11611161
/* fall through */
@@ -1194,7 +1194,7 @@ PHP_METHOD(PDOStatement, fetch)
11941194
Z_PARAM_LONG(off)
11951195
ZEND_PARSE_PARAMETERS_END();
11961196

1197-
PHP_STMT_GET_OBJ;
1197+
PHP_STMT_GET_OBJ;
11981198
PDO_STMT_CLEAR_ERR();
11991199

12001200
if (!pdo_stmt_verify_mode(stmt, how, 1, false)) {
@@ -1578,7 +1578,7 @@ PHP_METHOD(PDOStatement, errorCode)
15781578
PHP_METHOD(PDOStatement, errorInfo)
15791579
{
15801580
int error_count;
1581-
int error_count_diff = 0;
1581+
int error_count_diff = 0;
15821582
int error_expected_count = 3;
15831583

15841584
ZEND_PARSE_PARAMETERS_NONE();
@@ -1743,15 +1743,6 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
17431743
uint32_t arg1_arg_num = mode_arg_num + 1;
17441744
uint32_t constructor_arg_num = mode_arg_num + 2;
17451745
uint32_t total_num_args = mode_arg_num + variadic_num_args;
1746-
zval arg1, constructor_args;
1747-
1748-
/* Assign variadic params to dedicated ones */
1749-
if (variadic_num_args >= 1) {
1750-
arg1 = args[0];
1751-
if (variadic_num_args >= 2) {
1752-
constructor_args = args[1];
1753-
}
1754-
}
17551746

17561747
switch (stmt->default_fetch_type) {
17571748
case PDO_FETCH_INTO:
@@ -1782,7 +1773,7 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
17821773
case PDO_FETCH_BOUND:
17831774
case PDO_FETCH_NAMED:
17841775
case PDO_FETCH_KEY_PAIR:
1785-
if (total_num_args != mode_arg_num) {
1776+
if (variadic_num_args != 0) {
17861777
zend_string *func = get_active_function_or_method_name();
17871778
zend_argument_count_error("%s() expects exactly %d arguments for the fetch mode provided, %d given",
17881779
ZSTR_VAL(func), mode_arg_num, total_num_args);
@@ -1792,26 +1783,26 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
17921783
break;
17931784

17941785
case PDO_FETCH_COLUMN:
1795-
if (total_num_args != arg1_arg_num) {
1786+
if (variadic_num_args != 1) {
17961787
zend_string *func = get_active_function_or_method_name();
17971788
zend_argument_count_error("%s() expects exactly %d arguments for the fetch mode provided, %d given",
17981789
ZSTR_VAL(func), arg1_arg_num, total_num_args);
17991790
zend_string_release(func);
18001791
return false;
18011792
}
1802-
/* arg1 is initialized at the start */
1803-
if (Z_TYPE(arg1) != IS_LONG) {
1804-
zend_argument_type_error(arg1_arg_num, "must be int, %s given", zend_zval_type_name(&arg1));
1793+
if (Z_TYPE(args[0]) != IS_LONG) {
1794+
zend_argument_type_error(arg1_arg_num, "must be int, %s given", zend_zval_type_name(&args[0]));
18051795
return false;
18061796
}
1807-
if (Z_LVAL(arg1) < 0) {
1797+
if (Z_LVAL(args[0]) < 0) {
18081798
zend_argument_value_error(arg1_arg_num, "must be greater than or equal to 0");
18091799
return false;
18101800
}
1811-
stmt->fetch.column = Z_LVAL(arg1);
1801+
stmt->fetch.column = Z_LVAL(args[0]);
18121802
break;
18131803

1814-
case PDO_FETCH_CLASS:
1804+
case PDO_FETCH_CLASS: {
1805+
HashTable *constructor_args = NULL;
18151806
/* Undef constructor arguments */
18161807
ZVAL_UNDEF(&stmt->fetch.cls.ctor_args);
18171808
/* Gets its class name from 1st column */
@@ -1841,27 +1832,38 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
18411832
zend_string_release(func);
18421833
return false;
18431834
}
1844-
/* arg1 is initialized at the start */
1845-
if (Z_TYPE(arg1) != IS_STRING) {
1846-
zend_argument_type_error(arg1_arg_num, "must be string, %s given", zend_zval_type_name(&arg1));
1835+
if (Z_TYPE(args[0]) != IS_STRING) {
1836+
zend_argument_type_error(arg1_arg_num, "must be string, %s given", zend_zval_type_name(&args[0]));
18471837
return false;
18481838
}
1849-
cep = zend_lookup_class(Z_STR(arg1));
1839+
cep = zend_lookup_class(Z_STR(args[0]));
18501840
if (!cep) {
18511841
zend_argument_type_error(arg1_arg_num, "must be a valid class");
18521842
return false;
18531843
}
1844+
/* Verify constructor_args (args[1]) is ?array */
1845+
/* TODO: Improve logic? */
1846+
if (variadic_num_args == 2) {
1847+
if (Z_TYPE(args[1]) != IS_NULL && Z_TYPE(args[1]) != IS_ARRAY) {
1848+
zend_argument_type_error(constructor_arg_num, "must be ?array, %s given",
1849+
zend_zval_type_name(&args[1]));
1850+
return false;
1851+
}
1852+
if (Z_TYPE(args[1]) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL(args[1]))) {
1853+
constructor_args = Z_ARRVAL(args[1]);
1854+
}
1855+
}
18541856
stmt->fetch.cls.ce = cep;
18551857

1856-
/* constructor_args is initialized at the start if variadic_num_args == 2 */
1857-
if (variadic_num_args == 2 && zend_hash_num_elements(Z_ARRVAL(constructor_args))) {
1858-
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(Z_ARRVAL(constructor_args)));
1858+
/* If constructor arguments are present and not empty */
1859+
if (constructor_args) {
1860+
ZVAL_ARR(&stmt->fetch.cls.ctor_args, zend_array_dup(constructor_args));
18591861
}
18601862
}
18611863

18621864
do_fetch_class_prepare(stmt);
18631865
break;
1864-
1866+
}
18651867
case PDO_FETCH_INTO:
18661868
if (total_num_args != arg1_arg_num) {
18671869
zend_string *func = get_active_function_or_method_name();
@@ -1870,13 +1872,12 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
18701872
zend_string_release(func);
18711873
return false;
18721874
}
1873-
/* arg1 is initialized at the start */
1874-
if (Z_TYPE(arg1) != IS_OBJECT) {
1875-
zend_argument_type_error(arg1_arg_num, "must be object, %s given", zend_zval_type_name(&arg1));
1875+
if (Z_TYPE(args[0]) != IS_OBJECT) {
1876+
zend_argument_type_error(arg1_arg_num, "must be object, %s given", zend_zval_type_name(&args[0]));
18761877
return false;
18771878
}
18781879

1879-
ZVAL_COPY(&stmt->fetch.into, &arg1);
1880+
ZVAL_COPY(&stmt->fetch.into, &args[0]);
18801881
break;
18811882
default:
18821883
zend_argument_value_error(mode_arg_num, "must be one of the PDO::FETCH_* constants");
@@ -1892,7 +1893,7 @@ PHP_METHOD(PDOStatement, setFetchMode)
18921893
{
18931894
zend_long fetch_mode;
18941895
zval *args = NULL;
1895-
uint32_t num_args = 0;
1896+
uint32_t num_args = 0;
18961897

18971898
/* Support null for constructor arguments for BC */
18981899
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l*", &fetch_mode, &args, &num_args) == FAILURE) {
@@ -2010,7 +2011,6 @@ PHP_METHOD(PDOStatement, debugDumpParams)
20102011

20112012
PHP_STMT_GET_OBJ;
20122013

2013-
/* TODO: Change to assertion? */
20142014
if (out == NULL) {
20152015
RETURN_FALSE;
20162016
}
@@ -2340,7 +2340,7 @@ static zval *row_prop_read(zend_object *object, zend_string *name, int type, voi
23402340
* numbers */
23412341
for (colno = 0; colno < stmt->column_count; colno++) {
23422342
if (ZSTR_LEN(stmt->columns[colno].name) == ZSTR_LEN(name) &&
2343-
strncmp(ZSTR_VAL(stmt->columns[colno].name), ZSTR_VAL(name), ZSTR_LEN(name)) == 0) {
2343+
strncmp(ZSTR_VAL(stmt->columns[colno].name), ZSTR_VAL(name), ZSTR_LEN(name)) == 0) {
23442344
fetch_value(stmt, rv, colno, NULL);
23452345
return rv;
23462346
}
@@ -2382,7 +2382,7 @@ static zval *row_dim_read(zend_object *object, zval *member, int type, zval *rv)
23822382
* numbers */
23832383
for (colno = 0; colno < stmt->column_count; colno++) {
23842384
if (ZSTR_LEN(stmt->columns[colno].name) == Z_STRLEN_P(member) &&
2385-
strncmp(ZSTR_VAL(stmt->columns[colno].name), Z_STRVAL_P(member), Z_STRLEN_P(member)) == 0) {
2385+
strncmp(ZSTR_VAL(stmt->columns[colno].name), Z_STRVAL_P(member), Z_STRLEN_P(member)) == 0) {
23862386
fetch_value(stmt, rv, colno, NULL);
23872387
return rv;
23882388
}
@@ -2424,7 +2424,7 @@ static int row_prop_exists(zend_object *object, zend_string *name, int check_emp
24242424
* numbers */
24252425
for (colno = 0; colno < stmt->column_count; colno++) {
24262426
if (ZSTR_LEN(stmt->columns[colno].name) == ZSTR_LEN(name) &&
2427-
strncmp(ZSTR_VAL(stmt->columns[colno].name), ZSTR_VAL(name), ZSTR_LEN(name)) == 0) {
2427+
strncmp(ZSTR_VAL(stmt->columns[colno].name), ZSTR_VAL(name), ZSTR_LEN(name)) == 0) {
24282428
int res;
24292429
zval val;
24302430

@@ -2464,7 +2464,7 @@ static int row_dim_exists(zend_object *object, zval *member, int check_empty)
24642464
* numbers */
24652465
for (colno = 0; colno < stmt->column_count; colno++) {
24662466
if (ZSTR_LEN(stmt->columns[colno].name) == Z_STRLEN_P(member) &&
2467-
strncmp(ZSTR_VAL(stmt->columns[colno].name), Z_STRVAL_P(member), Z_STRLEN_P(member)) == 0) {
2467+
strncmp(ZSTR_VAL(stmt->columns[colno].name), Z_STRVAL_P(member), Z_STRLEN_P(member)) == 0) {
24682468
int res;
24692469
zval val;
24702470

0 commit comments

Comments
 (0)