From: Nikita Popov Date: Wed, 9 Sep 2020 15:18:58 +0000 (+0200) Subject: Use proper int type for parameter X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=68b21939ec8981892cf8a3ecf3b86c0ac3bc7187;p=php Use proper int type for parameter --- diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 57feca194e..30f9772656 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -1120,18 +1120,13 @@ PHP_FUNCTION(pg_query_params) if (Z_TYPE_P(tmp) == IS_NULL) { params[i] = NULL; } else { - zval tmp_val; - - ZVAL_COPY(&tmp_val, tmp); - convert_to_string(&tmp_val); - if (Z_TYPE(tmp_val) != IS_STRING) { - php_error_docref(NULL, E_WARNING,"Error converting parameter"); - zval_ptr_dtor(&tmp_val); + zend_string *param_str = zval_try_get_string(tmp); + if (!param_str) { _php_pgsql_free_params(params, num_params); - RETURN_FALSE; + RETURN_THROWS(); } - params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val)); - zval_ptr_dtor(&tmp_val); + params[i] = estrndup(ZSTR_VAL(param_str), ZSTR_LEN(param_str)); + zend_string_release(param_str); } i++; } ZEND_HASH_FOREACH_END(); @@ -1796,17 +1791,18 @@ PHP_FUNCTION(pg_fetch_result) /* {{{ void php_pgsql_fetch_hash */ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object) { - zval *result, *zrow = NULL; + zval *result; PGresult *pgsql_result; pgsql_result_handle *pg_result; - int i, num_fields, pgsql_row, use_row; - zend_long row = -1; + int i, num_fields, pgsql_row; + zend_long row; + bool row_is_null = 1; char *field_name; zval *ctor_params = NULL; zend_class_entry *ce = NULL; if (into_object) { - if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z!Cz", &result, &zrow, &ce, &ctor_params) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l!Cz", &result, &row, &row_is_null, &ce, &ctor_params) == FAILURE) { RETURN_THROWS(); } if (!ce) { @@ -1814,21 +1810,15 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_ } result_type = PGSQL_ASSOC; } else { - if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z!l", &result, &zrow, &result_type) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l!l", &result, &row, &row_is_null, &result_type) == FAILURE) { RETURN_THROWS(); } } - if (zrow == NULL) { - row = -1; - } else { - convert_to_long(zrow); - row = Z_LVAL_P(zrow); - if (row < 0) { - php_error_docref(NULL, E_WARNING, "The row parameter must be greater or equal to zero"); - RETURN_FALSE; - } + + if (!row_is_null && row < 0) { + php_error_docref(NULL, E_WARNING, "The row parameter must be greater or equal to zero"); + RETURN_FALSE; } - use_row = ZEND_NUM_ARGS() > 1 && row != -1; if (!(result_type & PGSQL_BOTH)) { php_error_docref(NULL, E_WARNING, "Invalid result type"); @@ -1841,7 +1831,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_ pgsql_result = pg_result->result; - if (use_row) { + if (!row_is_null) { if (row < 0 || row >= PQntuples(pgsql_result)) { php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT, row, Z_LVAL_P(result)); diff --git a/ext/pgsql/pgsql.stub.php b/ext/pgsql/pgsql.stub.php index e5b9d5a607..c524763ece 100644 --- a/ext/pgsql/pgsql.stub.php +++ b/ext/pgsql/pgsql.stub.php @@ -169,28 +169,24 @@ function pg_result($result, $row_number, $field = UNKNOWN): string|false|null {} /** * @param resource $result - * @param int|null $row_number */ -function pg_fetch_row($result, $row_number = null, int $result_type = PGSQL_NUM): array|false {} +function pg_fetch_row($result, ?int $row_number = null, int $result_type = PGSQL_NUM): array|false {} /** * @param resource $result - * @param int|null $row_number */ -function pg_fetch_assoc($result, $row_number = null): array|false {} +function pg_fetch_assoc($result, ?int $row_number = null): array|false {} /** * @param resource $result - * @param int|null $row_number */ -function pg_fetch_array($result, $row_number = null, int $result_type = PGSQL_BOTH): array|false {} +function pg_fetch_array($result, ?int $row_number = null, int $result_type = PGSQL_BOTH): array|false {} /** * @param resource $result - * @param int|null $row_number * @param array|null $ctor_params */ -function pg_fetch_object($result, $row_number = null, string $class_name = "stdClass", $ctor_params = null): object|false {} +function pg_fetch_object($result, ?int $row_number = null, string $class_name = "stdClass", $ctor_params = null): object|false {} /** @param resource $result */ function pg_fetch_all($result, int $result_type = PGSQL_ASSOC): array|false {} diff --git a/ext/pgsql/pgsql_arginfo.h b/ext/pgsql/pgsql_arginfo.h index d5b42d48ba..2724464432 100644 --- a/ext/pgsql/pgsql_arginfo.h +++ b/ext/pgsql/pgsql_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 9364c37bf47b6aaa5c3d2e67a09d82e5b04c15ff */ + * Stub hash: 2ae99621cf060e986e354587ec34766d12b89d6c */ ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, connection_string, IS_STRING, 0) @@ -133,24 +133,24 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_row, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE) ZEND_ARG_INFO(0, result) - ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_NUM") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_assoc, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE) ZEND_ARG_INFO(0, result) - ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE) ZEND_ARG_INFO(0, result) - ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_BOTH") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_object, 0, 1, MAY_BE_OBJECT|MAY_BE_FALSE) ZEND_ARG_INFO(0, result) - ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class_name, IS_STRING, 0, "\"stdClass\"") ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, ctor_params, "null") ZEND_END_ARG_INFO()