Skip to content

Commit 8a745ec

Browse files
committed
ext/pgsql: Refactor parameter parsing to remove manual usage of zend_wrong_parameters_count_error();
1 parent 0c577c2 commit 8a745ec

File tree

2 files changed

+99
-106
lines changed

2 files changed

+99
-106
lines changed

ext/pgsql/pgsql.c

Lines changed: 98 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,17 +1159,14 @@ PHP_FUNCTION(pg_query)
11591159

11601160
link = FETCH_DEFAULT_LINK();
11611161
CHECK_DEFAULT_LINK(link);
1162-
} else if (ZEND_NUM_ARGS() == 2) {
1162+
} else {
11631163
ZEND_PARSE_PARAMETERS_START(2, 2)
11641164
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
11651165
Z_PARAM_STRING(query, query_len)
11661166
ZEND_PARSE_PARAMETERS_END();
11671167

11681168
link = Z_PGSQL_LINK_P(pgsql_link);
11691169
CHECK_PGSQL_LINK(link);
1170-
} else {
1171-
zend_wrong_parameters_count_error(1, 2);
1172-
RETURN_THROWS();
11731170
}
11741171

11751172
pgsql = link->conn;
@@ -1260,7 +1257,7 @@ PHP_FUNCTION(pg_query_params)
12601257

12611258
link = FETCH_DEFAULT_LINK();
12621259
CHECK_DEFAULT_LINK(link);
1263-
} else if (ZEND_NUM_ARGS() == 3) {
1260+
} else {
12641261
ZEND_PARSE_PARAMETERS_START(3, 3)
12651262
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
12661263
Z_PARAM_STRING(query, query_len)
@@ -1269,9 +1266,6 @@ PHP_FUNCTION(pg_query_params)
12691266

12701267
link = Z_PGSQL_LINK_P(pgsql_link);
12711268
CHECK_PGSQL_LINK(link);
1272-
} else {
1273-
zend_wrong_parameters_count_error(2, 3);
1274-
RETURN_THROWS();
12751269
}
12761270

12771271
pgsql = link->conn;
@@ -1373,7 +1367,7 @@ PHP_FUNCTION(pg_prepare)
13731367

13741368
link = FETCH_DEFAULT_LINK();
13751369
CHECK_DEFAULT_LINK(link);
1376-
} else if (ZEND_NUM_ARGS() == 3) {
1370+
} else {
13771371
ZEND_PARSE_PARAMETERS_START(3, 3)
13781372
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
13791373
Z_PARAM_STRING(stmtname, stmtname_len)
@@ -1382,9 +1376,6 @@ PHP_FUNCTION(pg_prepare)
13821376

13831377
link = Z_PGSQL_LINK_P(pgsql_link);
13841378
CHECK_PGSQL_LINK(link);
1385-
} else {
1386-
zend_wrong_parameters_count_error(2, 3);
1387-
RETURN_THROWS();
13881379
}
13891380

13901381
pgsql = link->conn;
@@ -1462,7 +1453,7 @@ PHP_FUNCTION(pg_execute)
14621453

14631454
link = FETCH_DEFAULT_LINK();
14641455
CHECK_DEFAULT_LINK(link);
1465-
} else if (ZEND_NUM_ARGS() == 3) {
1456+
} else {
14661457
ZEND_PARSE_PARAMETERS_START(3, 3)
14671458
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
14681459
Z_PARAM_STRING(stmtname, stmtname_len)
@@ -1471,9 +1462,6 @@ PHP_FUNCTION(pg_execute)
14711462

14721463
link = Z_PGSQL_LINK_P(pgsql_link);
14731464
CHECK_PGSQL_LINK(link);
1474-
} else {
1475-
zend_wrong_parameters_count_error(2, 3);
1476-
RETURN_THROWS();
14771465
}
14781466

14791467
pgsql = link->conn;
@@ -1928,7 +1916,7 @@ PHP_FUNCTION(pg_fetch_result)
19281916
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
19291917
Z_PARAM_STR_OR_LONG(field_name, field_offset)
19301918
ZEND_PARSE_PARAMETERS_END();
1931-
} else if (ZEND_NUM_ARGS() == 3) {
1919+
} else {
19321920
ZEND_PARSE_PARAMETERS_START(3, 3)
19331921
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
19341922
if (zend_string_equals_literal(EG(current_execute_data)->func->common.function_name, "pg_result")) {
@@ -1938,9 +1926,6 @@ PHP_FUNCTION(pg_fetch_result)
19381926
}
19391927
Z_PARAM_STR_OR_LONG(field_name, field_offset)
19401928
ZEND_PARSE_PARAMETERS_END();
1941-
} else {
1942-
zend_wrong_parameters_count_error(2, 3);
1943-
RETURN_THROWS();
19441929
}
19451930

19461931
pg_result = Z_PGSQL_RESULT_P(result);
@@ -1984,39 +1969,12 @@ PHP_FUNCTION(pg_fetch_result)
19841969
/* }}} */
19851970

19861971
/* {{{ void php_pgsql_fetch_hash */
1987-
static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object)
1972+
static void php_pgsql_fetch_hash(zval *return_value, const zval *result, zend_long row, bool row_is_null, zend_long result_type)
19881973
{
1989-
zval *result;
19901974
PGresult *pgsql_result;
19911975
pgsql_result_handle *pg_result;
19921976
int i, num_fields, pgsql_row;
1993-
zend_long row;
1994-
bool row_is_null = true;
19951977
char *field_name;
1996-
HashTable *ctor_params = NULL;
1997-
zend_class_entry *ce = NULL;
1998-
1999-
if (into_object) {
2000-
ZEND_PARSE_PARAMETERS_START(1, 4)
2001-
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2002-
Z_PARAM_OPTIONAL
2003-
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2004-
Z_PARAM_CLASS(ce)
2005-
Z_PARAM_ARRAY_HT(ctor_params)
2006-
ZEND_PARSE_PARAMETERS_END();
2007-
2008-
if (!ce) {
2009-
ce = zend_standard_class_def;
2010-
}
2011-
result_type = PGSQL_ASSOC;
2012-
} else {
2013-
ZEND_PARSE_PARAMETERS_START(1, 3)
2014-
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2015-
Z_PARAM_OPTIONAL
2016-
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2017-
Z_PARAM_LONG(result_type)
2018-
ZEND_PARSE_PARAMETERS_END();
2019-
}
20201978

20211979
if (!row_is_null && row < 0) {
20221980
zend_argument_value_error(2, "must be greater than or equal to 0");
@@ -2060,7 +2018,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
20602018
add_assoc_null(return_value, field_name);
20612019
}
20622020
} else {
2063-
char *element = PQgetvalue(pgsql_result, pgsql_row, i);
2021+
const char *element = PQgetvalue(pgsql_result, pgsql_row, i);
20642022
if (element) {
20652023
const size_t element_len = strlen(element);
20662024

@@ -2075,63 +2033,110 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
20752033
}
20762034
}
20772035
}
2078-
2079-
if (into_object) {
2080-
zval dataset;
2081-
2082-
ZVAL_COPY_VALUE(&dataset, return_value);
2083-
object_init_ex(return_value, ce);
2084-
if (!ce->default_properties_count && !ce->__set) {
2085-
Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
2086-
} else {
2087-
zend_merge_properties(return_value, Z_ARRVAL(dataset));
2088-
zval_ptr_dtor(&dataset);
2089-
}
2090-
2091-
if (ce->constructor) {
2092-
zend_call_known_function(ce->constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value),
2093-
/* retval */ NULL, /* argc */ 0, /* params */ NULL, ctor_params);
2094-
} else if (ctor_params && zend_hash_num_elements(ctor_params) > 0) {
2095-
zend_argument_value_error(3,
2096-
"must be empty when the specified class (%s) does not have a constructor",
2097-
ZSTR_VAL(ce->name)
2098-
);
2099-
}
2100-
}
21012036
}
21022037
/* }}} */
21032038

21042039
/* {{{ Get a row as an enumerated array */
21052040
PHP_FUNCTION(pg_fetch_row)
21062041
{
2107-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM, 0);
2042+
zval *result;
2043+
zend_long row;
2044+
bool row_is_null = true;
2045+
zend_long result_type = PGSQL_NUM;
2046+
2047+
ZEND_PARSE_PARAMETERS_START(1, 3)
2048+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2049+
Z_PARAM_OPTIONAL
2050+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2051+
Z_PARAM_LONG(result_type)
2052+
ZEND_PARSE_PARAMETERS_END();
2053+
2054+
php_pgsql_fetch_hash(return_value, result, row, row_is_null, result_type);
21082055
}
21092056
/* }}} */
21102057

21112058
/* {{{ Fetch a row as an assoc array */
21122059
PHP_FUNCTION(pg_fetch_assoc)
21132060
{
2114-
/* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when
2115-
there is 3rd parameter */
2116-
if (ZEND_NUM_ARGS() > 2)
2117-
WRONG_PARAM_COUNT;
2118-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 0);
2061+
zval *result;
2062+
zend_long row;
2063+
bool row_is_null = true;
2064+
2065+
ZEND_PARSE_PARAMETERS_START(1, 2)
2066+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2067+
Z_PARAM_OPTIONAL
2068+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2069+
ZEND_PARSE_PARAMETERS_END();
2070+
2071+
php_pgsql_fetch_hash(return_value, result, row, row_is_null, PGSQL_ASSOC);
21192072
}
21202073
/* }}} */
21212074

21222075
/* {{{ Fetch a row as an array */
21232076
PHP_FUNCTION(pg_fetch_array)
21242077
{
2125-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH, 0);
2078+
zval *result;
2079+
zend_long row;
2080+
bool row_is_null = true;
2081+
zend_long result_type = PGSQL_BOTH;
2082+
2083+
ZEND_PARSE_PARAMETERS_START(1, 3)
2084+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2085+
Z_PARAM_OPTIONAL
2086+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2087+
Z_PARAM_LONG(result_type)
2088+
ZEND_PARSE_PARAMETERS_END();
2089+
2090+
php_pgsql_fetch_hash(return_value, result, row, row_is_null, result_type);
21262091
}
21272092
/* }}} */
21282093

21292094
/* {{{ Fetch a row as an object */
21302095
PHP_FUNCTION(pg_fetch_object)
21312096
{
2097+
zval *result;
2098+
zend_long row;
2099+
bool row_is_null = true;
2100+
zend_class_entry *ce = NULL;
2101+
HashTable *ctor_params = NULL;
2102+
2103+
ZEND_PARSE_PARAMETERS_START(1, 4)
2104+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2105+
Z_PARAM_OPTIONAL
2106+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2107+
Z_PARAM_CLASS(ce)
2108+
Z_PARAM_ARRAY_HT(ctor_params)
2109+
ZEND_PARSE_PARAMETERS_END();
2110+
2111+
if (!ce) {
2112+
ce = zend_standard_class_def;
2113+
}
2114+
2115+
if (!ce->constructor && ctor_params && zend_hash_num_elements(ctor_params) > 0) {
2116+
zend_argument_value_error(3,
2117+
"must be empty when the specified class (%s) does not have a constructor",
2118+
ZSTR_VAL(ce->name)
2119+
);
2120+
RETURN_THROWS();
2121+
}
2122+
21322123
/* pg_fetch_object() allowed result_type used to be. 3rd parameter
21332124
must be allowed for compatibility */
2134-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 1);
2125+
zval dataset;
2126+
php_pgsql_fetch_hash(&dataset, result, row, row_is_null, PGSQL_ASSOC);
2127+
2128+
object_init_ex(return_value, ce);
2129+
if (!ce->default_properties_count && !ce->__set) {
2130+
Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
2131+
} else {
2132+
zend_merge_properties(return_value, Z_ARRVAL(dataset));
2133+
zval_ptr_dtor(&dataset);
2134+
}
2135+
2136+
if (ce->constructor) {
2137+
zend_call_known_function(ce->constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value),
2138+
/* retval */ NULL, /* argc */ 0, /* params */ NULL, ctor_params);
2139+
}
21352140
}
21362141
/* }}} */
21372142

@@ -2265,7 +2270,7 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bo
22652270
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
22662271
Z_PARAM_STR_OR_LONG(field_name, field_offset)
22672272
ZEND_PARSE_PARAMETERS_END();
2268-
} else if (ZEND_NUM_ARGS() == 3) {
2273+
} else {
22692274
ZEND_PARSE_PARAMETERS_START(3, 3)
22702275
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
22712276
if (nullable_row) {
@@ -2275,9 +2280,6 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bo
22752280
}
22762281
Z_PARAM_STR_OR_LONG(field_name, field_offset)
22772282
ZEND_PARSE_PARAMETERS_END();
2278-
} else {
2279-
zend_wrong_parameters_count_error(2, 3);
2280-
RETURN_THROWS();
22812283
}
22822284

22832285
pg_result = Z_PGSQL_RESULT_P(result);
@@ -2865,19 +2867,19 @@ PHP_FUNCTION(pg_lo_import)
28652867
Oid returned_oid;
28662868
pgsql_link_handle *link;
28672869

2868-
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
2869-
"OP|z", &pgsql_link, pgsql_link_ce, &file_in, &oid) == SUCCESS) {
2870-
link = Z_PGSQL_LINK_P(pgsql_link);
2871-
CHECK_PGSQL_LINK(link);
2872-
}
2873-
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
2874-
"P|z", &file_in, &oid) == SUCCESS) {
2870+
/* Deprecated signature with implicit connection */
2871+
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "P|z", &file_in, &oid) == SUCCESS) {
28752872
link = FETCH_DEFAULT_LINK();
28762873
CHECK_DEFAULT_LINK(link);
2874+
goto fn_body;
28772875
}
2878-
else {
2879-
WRONG_PARAM_COUNT;
2876+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|z", &pgsql_link, pgsql_link_ce, &file_in, &oid) == FAILURE) {
2877+
RETURN_THROWS();
28802878
}
2879+
link = Z_PGSQL_LINK_P(pgsql_link);
2880+
CHECK_PGSQL_LINK(link);
2881+
2882+
fn_body:
28812883

28822884
if (php_check_open_basedir(ZSTR_VAL(file_in))) {
28832885
RETURN_FALSE;
@@ -3108,17 +3110,14 @@ PHP_FUNCTION(pg_set_error_verbosity)
31083110

31093111
link = FETCH_DEFAULT_LINK();
31103112
CHECK_DEFAULT_LINK(link);
3111-
} else if (ZEND_NUM_ARGS() == 2) {
3113+
} else {
31123114
ZEND_PARSE_PARAMETERS_START(2, 2)
31133115
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
31143116
Z_PARAM_LONG(verbosity)
31153117
ZEND_PARSE_PARAMETERS_END();
31163118

31173119
link = Z_PGSQL_LINK_P(pgsql_link);
31183120
CHECK_PGSQL_LINK(link);
3119-
} else {
3120-
zend_wrong_parameters_count_error(1, 2);
3121-
RETURN_THROWS();
31223121
}
31233122

31243123
pgsql = link->conn;
@@ -3189,17 +3188,14 @@ PHP_FUNCTION(pg_set_client_encoding)
31893188

31903189
link = FETCH_DEFAULT_LINK();
31913190
CHECK_DEFAULT_LINK(link);
3192-
} else if (ZEND_NUM_ARGS() == 2) {
3191+
} else {
31933192
ZEND_PARSE_PARAMETERS_START(2, 2)
31943193
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
31953194
Z_PARAM_STRING(encoding, encoding_len)
31963195
ZEND_PARSE_PARAMETERS_END();
31973196

31983197
link = Z_PGSQL_LINK_P(pgsql_link);
31993198
CHECK_PGSQL_LINK(link);
3200-
} else {
3201-
zend_wrong_parameters_count_error(1, 2);
3202-
RETURN_THROWS();
32033199
}
32043200

32053201
pgsql = link->conn;
@@ -3286,17 +3282,14 @@ PHP_FUNCTION(pg_put_line)
32863282

32873283
link = FETCH_DEFAULT_LINK();
32883284
CHECK_DEFAULT_LINK(link);
3289-
} else if (ZEND_NUM_ARGS() == 2) {
3285+
} else {
32903286
ZEND_PARSE_PARAMETERS_START(2, 2)
32913287
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
32923288
Z_PARAM_STRING(query, query_len)
32933289
ZEND_PARSE_PARAMETERS_END();
32943290

32953291
link = Z_PGSQL_LINK_P(pgsql_link);
32963292
CHECK_PGSQL_LINK(link);
3297-
} else {
3298-
zend_wrong_parameters_count_error(1, 2);
3299-
RETURN_THROWS();
33003293
}
33013294

33023295
pgsql = link->conn;

ext/pgsql/tests/gh17158.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ try {
1313

1414
?>
1515
--EXPECT--
16-
pg_fetch_result() expects at least 2 arguments, 1 given
16+
pg_fetch_result() expects exactly 3 arguments, 1 given

0 commit comments

Comments
 (0)