Skip to content

Commit

Permalink
Promote some warnings to ValueError in OCI8
Browse files Browse the repository at this point in the history
Closes GH-6116

Co-authored-by: Christopher Jones <sixd@php.net>
  • Loading branch information
Girgias and cjbj committed Sep 15, 2020
1 parent 452f7b0 commit 824a2bf
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 137 deletions.
12 changes: 6 additions & 6 deletions ext/oci8/oci8.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ function oci_lob_copy(OCILob $lob_descriptor_to, OCILob $lob_descriptor_from, ?i

function oci_lob_is_equal(OCILob $lob_descriptor_first, OCILob $lob_descriptor_second): bool {}

function oci_lob_export(OCILob $lob_descriptor, string $path, ?int $start = null, ?int $length = null): bool {}
function oci_lob_export(OCILob $lob_descriptor, string $filename, ?int $start = null, ?int $length = null): bool {}

/**
* @alias oci_lob_export
* @deprecated
*/
function ociwritelobtofile(OCILob $lob_descriptor, string $path, ?int $start = null, ?int $length = null): bool {}
function ociwritelobtofile(OCILob $lob_descriptor, string $filename, ?int $start = null, ?int $length = null): bool {}

/**
* @param resource $connection_resource
Expand Down Expand Up @@ -412,14 +412,14 @@ function oci_get_implicit_resultset($statement_resource) {}
/**
* @param resource $statement_resource
*/
function oci_set_prefetch($statement_resource, int $number_of_rows): ?bool {}
function oci_set_prefetch($statement_resource, int $number_of_rows): bool {}

/**
* @param resource $statement_resource
* @alias oci_set_prefetch
* @deprecated
*/
function ocisetprefetch($statement_resource, int $number_of_rows): ?bool {}
function ocisetprefetch($statement_resource, int $number_of_rows): bool {}

/**
* @param resource $connection_resource
Expand Down Expand Up @@ -718,13 +718,13 @@ public function getbuffering() {}
* @alias oci_lob_export
* @return bool
*/
public function writetofile(string $path, ?int $start = null, ?int $length = null) {}
public function writetofile(string $filename, ?int $start = null, ?int $length = null) {}

/**
* @alias oci_lob_export
* @return bool
*/
public function export(string $path, ?int $start = null, ?int $length = null) {}
public function export(string $filename, ?int $start = null, ?int $length = null) {}

/**
* @alias oci_lob_write_temporary
Expand Down
8 changes: 4 additions & 4 deletions ext/oci8/oci8_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: 2d553815c21edd58bc29b1ca8d294d5750fd7312 */
* Stub hash: 6cdc7c967ce80c39eaef1c860ba8f8aa2cb3c979 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_oci_define_by_name, 0, 3, _IS_BOOL, 0)
ZEND_ARG_INFO(0, statement_resource)
Expand Down Expand Up @@ -124,7 +124,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_oci_lob_export, 0, 2, _IS_BOOL, 0)
ZEND_ARG_OBJ_INFO(0, lob_descriptor, OCILob, 0)
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, start, IS_LONG, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -292,7 +292,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_oci_get_implicit_resultset, 0, 0, 1)
ZEND_ARG_INFO(0, statement_resource)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_oci_set_prefetch, 0, 2, _IS_BOOL, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_oci_set_prefetch, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, statement_resource)
ZEND_ARG_TYPE_INFO(0, number_of_rows, IS_LONG, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -498,7 +498,7 @@ ZEND_END_ARG_INFO()
#define arginfo_class_OCILob_getbuffering arginfo_class_OCILob_load

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_OCILob_writetofile, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, start, IS_LONG, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
Expand Down
112 changes: 52 additions & 60 deletions ext/oci8/oci8_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#define OCI_STMT_CALL 10
#endif

#define ERROR_ARG_POS(arg_num) (getThis() ? (arg_num-1) : (arg_num))

/* {{{ Register a callback function for Oracle Transparent Application Failover (TAF) */
PHP_FUNCTION(oci_register_taf_callback)
{
Expand Down Expand Up @@ -109,8 +111,8 @@ PHP_FUNCTION(oci_define_by_name)
ZEND_PARSE_PARAMETERS_END();

if (!name_len) {
php_error_docref(NULL, E_WARNING, "Column name cannot be empty");
RETURN_FALSE;
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

PHP_OCI_ZVAL_TO_STATEMENT(stmt, statement);
Expand Down Expand Up @@ -210,8 +212,8 @@ PHP_FUNCTION(oci_bind_array_by_name)
}

if (max_array_len <= 0) {
php_error_docref(NULL, E_WARNING, "Maximum array length must be greater than zero");
RETURN_FALSE;
zend_argument_value_error(4, "must be greater than 0");
RETURN_THROWS();
}

if (php_oci_bind_array_by_name(statement, name, (sb4) name_len, bind_var, max_array_len, max_item_len, type)) {
Expand Down Expand Up @@ -257,18 +259,18 @@ PHP_FUNCTION(oci_lob_save)
RETURN_THROWS();
}

if (offset < 0) {
zend_argument_value_error(ERROR_ARG_POS(3), "must be greater than or equal to 0");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
RETURN_FALSE;
}

PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);

if (offset < 0) {
php_error_docref(NULL, E_WARNING, "Offset parameter must be greater than or equal to 0");
RETURN_FALSE;
}

if (php_oci_lob_write(descriptor, (ub4) offset, data, (ub4) data_len, &bytes_written)) {
RETURN_FALSE;
}
Expand All @@ -284,7 +286,7 @@ PHP_FUNCTION(oci_lob_import)
char *filename;
size_t filename_len;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len) == FAILURE) {
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Op", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len) == FAILURE) {
RETURN_THROWS();
}

Expand All @@ -293,11 +295,6 @@ PHP_FUNCTION(oci_lob_import)
RETURN_FALSE;
}

if (CHECK_NULL_PATH(filename, filename_len)) {
php_error_docref(NULL, E_WARNING, "filename must not contain null bytes");
RETURN_FALSE;
}

PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);

if (php_oci_lob_import(descriptor, filename)) {
Expand Down Expand Up @@ -354,18 +351,18 @@ PHP_FUNCTION(oci_lob_read)
RETURN_THROWS();
}

if (length <= 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than 0");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
RETURN_FALSE;
}

PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);

if (length <= 0) {
php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
RETURN_FALSE;
}

if (php_oci_lob_read(descriptor, length, descriptor->lob_current_position, &buffer, &buffer_len)) {
RETURN_FALSE;
}
Expand Down Expand Up @@ -604,13 +601,13 @@ PHP_FUNCTION(oci_lob_truncate)
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
RETURN_FALSE;
if (trim_length < 0) {
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}

if (trim_length < 0) {
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to zero");
if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
RETURN_FALSE;
}

Expand Down Expand Up @@ -640,15 +637,15 @@ PHP_FUNCTION(oci_lob_erase)
if (offset_is_null) {
offset = -1;
} else if (offset < 0) {
php_error_docref(NULL, E_WARNING, "Offset must be greater than or equal to 0");
RETURN_FALSE;
zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
RETURN_THROWS();
}

if (length_is_null) {
length = -1;
} else if (length < 0) {
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0");
RETURN_FALSE;
zend_argument_value_error(ERROR_ARG_POS(3), "must be greater than or equal to 0");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
Expand Down Expand Up @@ -756,6 +753,13 @@ PHP_FUNCTION(oci_lob_copy)
RETURN_THROWS();
}

if (length_is_null) {
length = -1;
} else if (length < 0) {
zend_argument_value_error(3, "must be greater than or equal to 0");
RETURN_THROWS();
}

if ((tmp_dest = zend_hash_str_find(Z_OBJPROP_P(z_descriptor_dest), "descriptor", sizeof("descriptor")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
RETURN_FALSE;
Expand All @@ -769,13 +773,6 @@ PHP_FUNCTION(oci_lob_copy)
PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_dest, descriptor_dest);
PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp_from, descriptor_from);

if (length_is_null) {
length = -1;
} else if (length < 0) {
php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
RETURN_FALSE;
}

if (php_oci_lob_copy(descriptor_dest, descriptor_from, length)) {
RETURN_FALSE;
}
Expand Down Expand Up @@ -831,34 +828,29 @@ PHP_FUNCTION(oci_lob_export)
php_stream *stream;
ub4 lob_length;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|l!l!", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len, &start, &start_is_null, &length, &length_is_null) == FAILURE) {
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Op|l!l!", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len, &start, &start_is_null, &length, &length_is_null) == FAILURE) {
RETURN_THROWS();
}

if (start_is_null) {
start = -1;
} else if (start < 0) {
php_error_docref(NULL, E_WARNING, "Start parameter must be greater than or equal to 0");
RETURN_FALSE;
zend_argument_value_error(ERROR_ARG_POS(3), "must be greater than or equal to 0");
RETURN_THROWS();
}

if (length_is_null) {
length = -1;
} else if (length < 0) {
php_error_docref(NULL, E_WARNING, "length parameter must be greater than or equal to 0");
RETURN_FALSE;
zend_argument_value_error(ERROR_ARG_POS(4), "must be greater than or equal to 0");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
RETURN_FALSE;
}

if (CHECK_NULL_PATH(filename, filename_len)) {
php_error_docref(NULL, E_WARNING, "filename must not contain null bytes");
RETURN_FALSE;
}

PHP_OCI_ZVAL_TO_DESCRIPTOR(tmp, descriptor);

if (php_oci_lob_get_length(descriptor, &lob_length)) {
Expand Down Expand Up @@ -1630,8 +1622,8 @@ PHP_FUNCTION(oci_set_prefetch)
PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);

if (size < 0) {
php_error_docref(NULL, E_WARNING, "Number of rows to be prefetched has to be greater than or equal to 0");
return;
zend_argument_value_error(2, "must be greater than or equal to 0");
RETURN_THROWS();
}

if (php_oci_statement_set_prefetch(statement, (ub4)size)) {
Expand Down Expand Up @@ -1899,16 +1891,16 @@ PHP_FUNCTION(oci_password_change)
PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);

if (!user_len) {
php_error_docref(NULL, E_WARNING, "Username cannot be empty");
RETURN_FALSE;
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}
if (!pass_old_len) {
php_error_docref(NULL, E_WARNING, "Old password cannot be empty");
RETURN_FALSE;
zend_argument_value_error(3, "cannot be empty");
RETURN_THROWS();
}
if (!pass_new_len) {
php_error_docref(NULL, E_WARNING, "New password cannot be empty");
RETURN_FALSE;
zend_argument_value_error(4, "cannot be empty");
RETURN_THROWS();
}

if (php_oci_password_change(connection, user, (int) user_len, pass_old, (int) pass_old_len, pass_new, (int) pass_new_len)) {
Expand All @@ -1918,16 +1910,16 @@ PHP_FUNCTION(oci_password_change)
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "ssss", &dbname, &dbname_len, &user, &user_len, &pass_old, &pass_old_len, &pass_new, &pass_new_len) == SUCCESS) {

if (!user_len) {
php_error_docref(NULL, E_WARNING, "Username cannot be empty");
RETURN_FALSE;
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}
if (!pass_old_len) {
php_error_docref(NULL, E_WARNING, "Old password cannot be empty");
RETURN_FALSE;
zend_argument_value_error(3, "cannot be empty");
RETURN_THROWS();
}
if (!pass_new_len) {
php_error_docref(NULL, E_WARNING, "New password cannot be empty");
RETURN_FALSE;
zend_argument_value_error(4, "cannot be empty");
RETURN_THROWS();
}

connection = php_oci_do_connect_ex(user, (int) user_len, pass_old, (int) pass_old_len, pass_new, (int) pass_new_len, dbname, (int) dbname_len, NULL, OCI_DEFAULT, 0, 0);
Expand Down
8 changes: 6 additions & 2 deletions ext/oci8/tests/array_bind_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ $statement = oci_parse($c, "BEGIN array_bind_002_pkg.iobind(:c1); END;");

$array = Array("06-DEC-05","10-DEC-80","21-AUG-91","26-OCT-17","05-NOV-05");

oci_bind_array_by_name($statement, ":c1", $array, 0, 0, SQLT_ODT);
try {
oci_bind_array_by_name($statement, ":c1", $array, 0, 0, SQLT_ODT);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

oci_execute($statement);

Expand All @@ -59,7 +63,7 @@ var_dump($array);
echo "Done\n";
?>
--EXPECTF--
Warning: oci_bind_array_by_name(): Maximum array length must be greater than zero in %s on line %d
oci_bind_array_by_name(): Argument #4 ($maximum_array_length) must be greater than 0

Warning: oci_execute(): ORA-%r(01008|57000)%r: %s in %s on line %d
array(5) {
Expand Down
10 changes: 6 additions & 4 deletions ext/oci8/tests/define1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ $stmt = oci_parse($c, "select string from define1_tab");
$string = '';
var_dump(oci_define_by_name($stmt, "STRING", $string, 20));
var_dump(oci_define_by_name($stmt, "STRING", $string, 20));
var_dump(oci_define_by_name($stmt, "", $string, 20));
try {
var_dump(oci_define_by_name($stmt, "", $string, 20));
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

oci_execute($stmt);

Expand All @@ -48,8 +52,6 @@ echo "Done\n";
--EXPECTF--
bool(true)
bool(false)

Warning: oci_define_by_name(): Column name cannot be empty in %s on line %d
bool(false)
oci_define_by_name(): Argument #2 ($column_name) cannot be empty
string(4) "some"
Done
Binary file modified ext/oci8/tests/lob_003.phpt
Binary file not shown.
Binary file modified ext/oci8/tests/lob_020.phpt
Binary file not shown.

0 comments on commit 824a2bf

Please sign in to comment.