Skip to content

Commit 68b2193

Browse files
committed
Use proper int type for parameter
1 parent 6145cac commit 68b2193

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

ext/pgsql/pgsql.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,18 +1120,13 @@ PHP_FUNCTION(pg_query_params)
11201120
if (Z_TYPE_P(tmp) == IS_NULL) {
11211121
params[i] = NULL;
11221122
} else {
1123-
zval tmp_val;
1124-
1125-
ZVAL_COPY(&tmp_val, tmp);
1126-
convert_to_string(&tmp_val);
1127-
if (Z_TYPE(tmp_val) != IS_STRING) {
1128-
php_error_docref(NULL, E_WARNING,"Error converting parameter");
1129-
zval_ptr_dtor(&tmp_val);
1123+
zend_string *param_str = zval_try_get_string(tmp);
1124+
if (!param_str) {
11301125
_php_pgsql_free_params(params, num_params);
1131-
RETURN_FALSE;
1126+
RETURN_THROWS();
11321127
}
1133-
params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
1134-
zval_ptr_dtor(&tmp_val);
1128+
params[i] = estrndup(ZSTR_VAL(param_str), ZSTR_LEN(param_str));
1129+
zend_string_release(param_str);
11351130
}
11361131
i++;
11371132
} ZEND_HASH_FOREACH_END();
@@ -1796,39 +1791,34 @@ PHP_FUNCTION(pg_fetch_result)
17961791
/* {{{ void php_pgsql_fetch_hash */
17971792
static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object)
17981793
{
1799-
zval *result, *zrow = NULL;
1794+
zval *result;
18001795
PGresult *pgsql_result;
18011796
pgsql_result_handle *pg_result;
1802-
int i, num_fields, pgsql_row, use_row;
1803-
zend_long row = -1;
1797+
int i, num_fields, pgsql_row;
1798+
zend_long row;
1799+
bool row_is_null = 1;
18041800
char *field_name;
18051801
zval *ctor_params = NULL;
18061802
zend_class_entry *ce = NULL;
18071803

18081804
if (into_object) {
1809-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z!Cz", &result, &zrow, &ce, &ctor_params) == FAILURE) {
1805+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l!Cz", &result, &row, &row_is_null, &ce, &ctor_params) == FAILURE) {
18101806
RETURN_THROWS();
18111807
}
18121808
if (!ce) {
18131809
ce = zend_standard_class_def;
18141810
}
18151811
result_type = PGSQL_ASSOC;
18161812
} else {
1817-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z!l", &result, &zrow, &result_type) == FAILURE) {
1813+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l!l", &result, &row, &row_is_null, &result_type) == FAILURE) {
18181814
RETURN_THROWS();
18191815
}
18201816
}
1821-
if (zrow == NULL) {
1822-
row = -1;
1823-
} else {
1824-
convert_to_long(zrow);
1825-
row = Z_LVAL_P(zrow);
1826-
if (row < 0) {
1827-
php_error_docref(NULL, E_WARNING, "The row parameter must be greater or equal to zero");
1828-
RETURN_FALSE;
1829-
}
1817+
1818+
if (!row_is_null && row < 0) {
1819+
php_error_docref(NULL, E_WARNING, "The row parameter must be greater or equal to zero");
1820+
RETURN_FALSE;
18301821
}
1831-
use_row = ZEND_NUM_ARGS() > 1 && row != -1;
18321822

18331823
if (!(result_type & PGSQL_BOTH)) {
18341824
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_
18411831

18421832
pgsql_result = pg_result->result;
18431833

1844-
if (use_row) {
1834+
if (!row_is_null) {
18451835
if (row < 0 || row >= PQntuples(pgsql_result)) {
18461836
php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
18471837
row, Z_LVAL_P(result));

ext/pgsql/pgsql.stub.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,28 +169,24 @@ function pg_result($result, $row_number, $field = UNKNOWN): string|false|null {}
169169

170170
/**
171171
* @param resource $result
172-
* @param int|null $row_number
173172
*/
174-
function pg_fetch_row($result, $row_number = null, int $result_type = PGSQL_NUM): array|false {}
173+
function pg_fetch_row($result, ?int $row_number = null, int $result_type = PGSQL_NUM): array|false {}
175174

176175
/**
177176
* @param resource $result
178-
* @param int|null $row_number
179177
*/
180-
function pg_fetch_assoc($result, $row_number = null): array|false {}
178+
function pg_fetch_assoc($result, ?int $row_number = null): array|false {}
181179

182180
/**
183181
* @param resource $result
184-
* @param int|null $row_number
185182
*/
186-
function pg_fetch_array($result, $row_number = null, int $result_type = PGSQL_BOTH): array|false {}
183+
function pg_fetch_array($result, ?int $row_number = null, int $result_type = PGSQL_BOTH): array|false {}
187184

188185
/**
189186
* @param resource $result
190-
* @param int|null $row_number
191187
* @param array|null $ctor_params
192188
*/
193-
function pg_fetch_object($result, $row_number = null, string $class_name = "stdClass", $ctor_params = null): object|false {}
189+
function pg_fetch_object($result, ?int $row_number = null, string $class_name = "stdClass", $ctor_params = null): object|false {}
194190

195191
/** @param resource $result */
196192
function pg_fetch_all($result, int $result_type = PGSQL_ASSOC): array|false {}

ext/pgsql/pgsql_arginfo.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 9364c37bf47b6aaa5c3d2e67a09d82e5b04c15ff */
2+
* Stub hash: 2ae99621cf060e986e354587ec34766d12b89d6c */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, connection_string, IS_STRING, 0)
@@ -133,24 +133,24 @@ ZEND_END_ARG_INFO()
133133

134134
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_row, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
135135
ZEND_ARG_INFO(0, result)
136-
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null")
136+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null")
137137
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_NUM")
138138
ZEND_END_ARG_INFO()
139139

140140
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_assoc, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
141141
ZEND_ARG_INFO(0, result)
142-
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null")
142+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null")
143143
ZEND_END_ARG_INFO()
144144

145145
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
146146
ZEND_ARG_INFO(0, result)
147-
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null")
147+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null")
148148
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, result_type, IS_LONG, 0, "PGSQL_BOTH")
149149
ZEND_END_ARG_INFO()
150150

151151
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_fetch_object, 0, 1, MAY_BE_OBJECT|MAY_BE_FALSE)
152152
ZEND_ARG_INFO(0, result)
153-
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, row_number, "null")
153+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, row_number, IS_LONG, 1, "null")
154154
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, class_name, IS_STRING, 0, "\"stdClass\"")
155155
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, ctor_params, "null")
156156
ZEND_END_ARG_INFO()

0 commit comments

Comments
 (0)