Skip to content

Make ext/odbc default value handling more consistent #13910

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

Merged
merged 3 commits into from
Apr 10, 2024
Merged
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
10 changes: 10 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ PHP 8.4 UPGRADE NOTES
is converted to Unicode. This is significant because around 40 SJIS-Mac characters
convert to a sequence of multiple Unicode codepoints.

- ODBC:
. odbc_fetch_row() returns false when a value less than or equal to 0 is
passed for parameter $row. Now, a warning is emitted in this case.

- Opcache:
. The JIT config defaults changed from opcache.jit=tracing and
opcache.jit_buffer_size=0 to opcache.jit=disable and
Expand Down Expand Up @@ -360,6 +364,12 @@ PHP 8.4 UPGRADE NOTES
. New serial_hex parameter added to openssl_csr_sign to allow setting serial
number in the hexadecimal format.

- ODBC:
. Parameter $row of odbc_fetch_object(), odbc_fetch_array(), and
odbc_fetch_into() now has a default value of null, consistent with
odbc_fetch_row(). Previously, the default values were -1, -1, and 0,
respectively.

- Output:
. Output handler status flags passed to the flags parameter of ob_start
are now cleared.
Expand Down
6 changes: 3 additions & 3 deletions ext/odbc/odbc.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,17 @@ function odbc_do($odbc, string $query) {}

#ifdef PHP_ODBC_HAVE_FETCH_HASH
/** @param resource $statement */
function odbc_fetch_object($statement, int $row = -1): stdClass|false {}
function odbc_fetch_object($statement, ?int $row = null): stdClass|false {}

/** @param resource $statement */
function odbc_fetch_array($statement, int $row = -1): array|false {}
function odbc_fetch_array($statement, ?int $row = null): array|false {}
#endif

/**
* @param resource $statement
* @param array $array
*/
function odbc_fetch_into($statement, &$array, int $row = 0): int|false {}
function odbc_fetch_into($statement, &$array, ?int $row = null): int|false {}

/** @param resource $statement */
function odbc_fetch_row($statement, ?int $row = null): bool {}
Expand Down
8 changes: 4 additions & 4 deletions ext/odbc/odbc_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 46 additions & 35 deletions ext/odbc/php_odbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1269,29 +1269,29 @@ static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
RETCODE rc;
SQLSMALLINT sql_c_type;
char *buf = NULL;
zend_long pv_row = 0;
bool pv_row_is_null = true;
zval *pv_res, tmp;
#ifdef HAVE_SQL_EXTENDED_FETCH
SQLULEN crow;
SQLUSMALLINT RowStatus[1];
SQLLEN rownum;
zval *pv_res, tmp;
zend_long pv_row = -1;
#endif

if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l!", &pv_res, &pv_row, &pv_row_is_null) == FAILURE) {
RETURN_THROWS();
}

rownum = pv_row;
#else
zval *pv_res, tmp;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
RETURN_THROWS();
}
#endif

if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
RETURN_THROWS();
/* TODO deprecate $row argument values less than 1 after PHP 8.4 */

#ifndef HAVE_SQL_EXTENDED_FETCH
if (!pv_row_is_null && pv_row > 0) {
php_error_docref(NULL, E_WARNING, "Extended fetch functionality is not available, argument #3 ($row) is ignored");
}
#endif

if (result->numcols == 0) {
php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
Expand All @@ -1300,8 +1300,8 @@ static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)

#ifdef HAVE_SQL_EXTENDED_FETCH
if (result->fetch_abs) {
if (rownum > 0) {
rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
if (!pv_row_is_null && pv_row > 0) {
rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,(SQLLEN)pv_row,&crow,RowStatus);
} else {
rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
}
Expand All @@ -1316,8 +1316,8 @@ static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
array_init(return_value);

#ifdef HAVE_SQL_EXTENDED_FETCH
if (rownum > 0 && result->fetch_abs)
result->fetched = rownum;
if (!pv_row_is_null && pv_row > 0 && result->fetch_abs)
result->fetched = (SQLLEN)pv_row;
else
#endif
result->fetched++;
Expand Down Expand Up @@ -1430,28 +1430,28 @@ PHP_FUNCTION(odbc_fetch_into)
SQLSMALLINT sql_c_type;
char *buf = NULL;
zval *pv_res, *pv_res_arr, tmp;
#ifdef HAVE_SQL_EXTENDED_FETCH
zend_long pv_row = 0;
bool pv_row_is_null = true;
#ifdef HAVE_SQL_EXTENDED_FETCH
SQLULEN crow;
SQLUSMALLINT RowStatus[1];
SQLLEN rownum = -1;
#endif /* HAVE_SQL_EXTENDED_FETCH */

#ifdef HAVE_SQL_EXTENDED_FETCH
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|l", &pv_res, &pv_res_arr, &pv_row) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|l!", &pv_res, &pv_res_arr, &pv_row, &pv_row_is_null) == FAILURE) {
RETURN_THROWS();
}

rownum = pv_row;
#else
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_res_arr) == FAILURE) {
if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
RETURN_THROWS();
}
#endif /* HAVE_SQL_EXTENDED_FETCH */

if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
RETURN_THROWS();
/* TODO deprecate $row argument values less than 1 after PHP 8.4 */

#ifndef HAVE_SQL_EXTENDED_FETCH
if (!pv_row_is_null && pv_row > 0) {
php_error_docref(NULL, E_WARNING, "Extended fetch functionality is not available, argument #3 ($row) is ignored");
}
#endif

if (result->numcols == 0) {
php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
Expand All @@ -1465,8 +1465,8 @@ PHP_FUNCTION(odbc_fetch_into)

#ifdef HAVE_SQL_EXTENDED_FETCH
if (result->fetch_abs) {
if (rownum > 0) {
rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
if (!pv_row_is_null && pv_row > 0) {
rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,(SQLLEN)pv_row,&crow,RowStatus);
} else {
rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
}
Expand All @@ -1479,8 +1479,8 @@ PHP_FUNCTION(odbc_fetch_into)
}

#ifdef HAVE_SQL_EXTENDED_FETCH
if (rownum > 0 && result->fetch_abs)
result->fetched = rownum;
if (!pv_row_is_null && pv_row > 0 && result->fetch_abs)
result->fetched = (SQLLEN)pv_row;
else
#endif
result->fetched++;
Expand Down Expand Up @@ -1560,8 +1560,8 @@ PHP_FUNCTION(odbc_fetch_row)
odbc_result *result;
RETCODE rc;
zval *pv_res;
zend_long pv_row;
bool pv_row_is_null = 1;
zend_long pv_row = 0;
bool pv_row_is_null = true;
#ifdef HAVE_SQL_EXTENDED_FETCH
SQLULEN crow;
SQLUSMALLINT RowStatus[1];
Expand All @@ -1575,6 +1575,17 @@ PHP_FUNCTION(odbc_fetch_row)
RETURN_THROWS();
}

#ifndef HAVE_SQL_EXTENDED_FETCH
if (!pv_row_is_null) {
php_error_docref(NULL, E_WARNING, "Extended fetch functionality is not available, argument #3 ($row) is ignored");
}
#else
if (!pv_row_is_null && pv_row < 1) {
php_error_docref(NULL, E_WARNING, "Argument #3 ($row) must be greater than or equal to 1");
Copy link
Member Author

Choose a reason for hiding this comment

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

I realized that it's still possible to warn in case of odbc_fetch_row() because null is already the default value since PHP 8.0 (9b50fd2#diff-36f2362bf160e8a01f19ba44e282f7a2f69b460d57a5b23d9e71c59af5de8810R1689).

RETURN_FALSE;
}
#endif

if (result->numcols == 0) {
php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
RETURN_FALSE;
Expand All @@ -1594,12 +1605,12 @@ PHP_FUNCTION(odbc_fetch_row)
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
RETURN_FALSE;
}

#ifdef HAVE_SQL_EXTENDED_FETCH
Copy link
Member Author

Choose a reason for hiding this comment

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

this looks like an omission. The result of the affected functions already used the same #ifdefs

if (!pv_row_is_null) {
result->fetched = (SQLLEN)pv_row;
} else {
} else
#endif
result->fetched++;
}

RETURN_TRUE;
}
Expand Down
1 change: 1 addition & 0 deletions ext/odbc/tests/odbc_fetch_row_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $conn = odbc_connect($dsn, $user, $pass);
odbc_exec($conn, 'DROP TABLE fetch_row');
?>
--EXPECTF--
Warning: odbc_fetch_row(): Argument #3 ($row) must be greater than or equal to 1 in %s on line %d
bool(false)
bool(true)
string(1) "1"
Expand Down