Skip to content

Commit

Permalink
Use proper int type for parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Sep 9, 2020
1 parent 6145cac commit 68b2193
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
42 changes: 16 additions & 26 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1796,39 +1791,34 @@ 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) {
ce = zend_standard_class_def;
}
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");
Expand All @@ -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));
Expand Down
12 changes: 4 additions & 8 deletions ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
10 changes: 5 additions & 5 deletions ext/pgsql/pgsql_arginfo.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 68b2193

Please sign in to comment.