Skip to content

ext/pgsql fixing further calls with flexible arguments number. #17165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,12 +984,15 @@ PHP_FUNCTION(pg_query)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 2) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &query, &query_len) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(1, 2);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down Expand Up @@ -1078,12 +1081,15 @@ PHP_FUNCTION(pg_query_params)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 3) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osa", &pgsql_link, pgsql_link_ce, &query, &query_len, &pv_param_arr) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(2, 3);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down Expand Up @@ -1183,12 +1189,15 @@ PHP_FUNCTION(pg_prepare)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 3) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss", &pgsql_link, pgsql_link_ce, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(2, 3);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down Expand Up @@ -1264,12 +1273,15 @@ PHP_FUNCTION(pg_execute)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 3) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osa", &pgsql_link, pgsql_link_ce, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(2, 3);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down Expand Up @@ -2024,7 +2036,7 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bo
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
Z_PARAM_STR_OR_LONG(field_name, field_offset)
ZEND_PARSE_PARAMETERS_END();
} else {
} else if (ZEND_NUM_ARGS() == 3) {
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
if (nullable_row) {
Expand All @@ -2034,6 +2046,9 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bo
}
Z_PARAM_STR_OR_LONG(field_name, field_offset)
ZEND_PARSE_PARAMETERS_END();
} else {
zend_wrong_parameters_count_error(2, 3);
RETURN_THROWS();
}

pg_result = Z_PGSQL_RESULT_P(result);
Expand Down Expand Up @@ -2849,12 +2864,15 @@ PHP_FUNCTION(pg_set_error_verbosity)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 2) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pgsql_link, pgsql_link_ce, &verbosity) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(1, 2);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down Expand Up @@ -2907,12 +2925,15 @@ PHP_FUNCTION(pg_set_client_encoding)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 2) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &encoding, &encoding_len) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(1, 2);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down Expand Up @@ -2996,12 +3017,15 @@ PHP_FUNCTION(pg_put_line)
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
} else if (ZEND_NUM_ARGS() == 2) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &query, &query_len) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);
} else {
zend_wrong_parameters_count_error(1, 2);
RETURN_THROWS();
}

pgsql = link->conn;
Expand Down
65 changes: 65 additions & 0 deletions ext/pgsql/tests/gh17165.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
Fix pg_query()/pg_query_params()/pg_prepare()/pg_execute()/pg_set_error_verbosity()/pg_set_client_encoding()/pg_put_line() pg field infos calls ArgumentCountError message.
--EXTENSIONS--
pgsql
--SKIPIF--
<?php
include("skipif.inc");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should better be an absolute path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why the test fails on PHP-8.4 and master is that skipif.inc has been moved to inc/skipip.inc (same with config.inc), though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I forgot this, just tested the build as I needed change for higher branches.

?>
--FILE--
<?php


include "config.inc";

$db = pg_connect($conn_str);
try {
pg_query("a", "b", "c");
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
pg_query_params($db, "b", array(), "d");
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
pg_prepare($db, "a", "b", "c");
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
pg_set_error_verbosity($db, 0, PHP_INT_MAX);
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
pg_set_client_encoding($db, "foo", "bar");
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
pg_put_line($db, "my", "data");
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
pg_field_is_null($db, false, "myfield", new stdClass());
} catch (ArgumentCountError $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
pg_query() expects at most 2 arguments, 3 given
pg_query_params() expects at most 3 arguments, 4 given
pg_prepare() expects at most 3 arguments, 4 given
pg_set_error_verbosity() expects at most 2 arguments, 3 given
pg_set_client_encoding() expects at most 2 arguments, 3 given
pg_put_line() expects at most 2 arguments, 3 given
pg_field_is_null() expects at most 3 arguments, 4 given
Loading