From 0d3e384fa5b144d7cbed285e9da0b5dcf40811cd Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Sun, 13 Sep 2020 01:08:18 +0200 Subject: [PATCH 1/3] Promote warning to Error in ODBC extension --- ext/odbc/php_odbc.c | 57 ++++++++++++------------ ext/odbc/tests/odbc_data_source_001.phpt | 8 +++- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c index af62ecb29dada..f3f7b1910965d 100644 --- a/ext/odbc/php_odbc.c +++ b/ext/odbc/php_odbc.c @@ -787,6 +787,11 @@ void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type) RETURN_THROWS(); } + if (pv_num < 1) { + zend_argument_value_error(2, "must be greater than or equal to 1"); + RETURN_THROWS(); + } + if (result->numcols == 0) { php_error_docref(NULL, E_WARNING, "No tuples available at this result index"); RETURN_FALSE; @@ -797,11 +802,6 @@ void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type) RETURN_FALSE; } - if (pv_num < 1) { - php_error_docref(NULL, E_WARNING, "Field numbering starts at 1"); - RETURN_FALSE; - } - PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, (SQLUSMALLINT) (type?SQL_COLUMN_SCALE:SQL_COLUMN_PRECISION), NULL, 0, NULL, &len); RETURN_LONG(len); @@ -963,7 +963,8 @@ PHP_FUNCTION(odbc_prepare) /* {{{ Execute a prepared statement */ PHP_FUNCTION(odbc_execute) { - zval *pv_res, *pv_param_arr, *tmp; + zval *pv_res, *tmp; + HashTable *pv_param_ht; typedef struct params_t { SQLLEN vallen; int fp; @@ -976,7 +977,7 @@ PHP_FUNCTION(odbc_execute) int numArgs = ZEND_NUM_ARGS(), i, ne; RETCODE rc; - if (zend_parse_parameters(numArgs, "r|a", &pv_res, &pv_param_arr) == FAILURE) { + if (zend_parse_parameters(numArgs, "r|h", &pv_res, &pv_param_ht) == FAILURE) { RETURN_THROWS(); } @@ -991,19 +992,19 @@ PHP_FUNCTION(odbc_execute) } if (result->numparams > 0) { - if ((ne = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr))) < result->numparams) { + if ((ne = zend_hash_num_elements(pv_param_ht)) < result->numparams) { php_error_docref(NULL, E_WARNING,"Not enough parameters (%d should be %d) given", ne, result->numparams); RETURN_FALSE; } - zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr)); + zend_hash_internal_pointer_reset(pv_param_ht); params = (params_t *)safe_emalloc(sizeof(params_t), result->numparams, 0); for(i = 0; i < result->numparams; i++) { params[i].fp = -1; } for(i = 1; i <= result->numparams; i++) { - if ((tmp = zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr))) == NULL) { + if ((tmp = zend_hash_get_current_data(pv_param_ht)) == NULL) { php_error_docref(NULL, E_WARNING,"Error getting parameter"); SQLFreeStmt(result->stmt,SQL_RESET_PARAMS); for (i = 0; i < result->numparams; i++) { @@ -1104,7 +1105,7 @@ PHP_FUNCTION(odbc_execute) efree(params); RETURN_FALSE; } - zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr)); + zend_hash_move_forward(pv_param_ht); } } /* Close cursor, needed for doing multiple selects */ @@ -1246,8 +1247,8 @@ PHP_FUNCTION(odbc_data_source) fetch_type = (SQLSMALLINT) zv_fetch_type; if (!(fetch_type == SQL_FETCH_FIRST || fetch_type == SQL_FETCH_NEXT)) { - php_error_docref(NULL, E_WARNING, "Invalid fetch type (%d)", fetch_type); - RETURN_FALSE; + zend_argument_value_error(2, "must be SQL_FETCH_FIRST or SQL_FETCH_NEXT"); + RETURN_THROWS() } if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(zv_conn), "ODBC-Link", le_conn, le_pconn))) { @@ -2209,8 +2210,9 @@ void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) cur_opt == SQL_CUR_USE_ODBC || cur_opt == SQL_CUR_USE_DRIVER || cur_opt == SQL_CUR_DEFAULT) ) { - php_error_docref(NULL, E_WARNING, "Invalid Cursor type (%d)", cur_opt); - RETURN_FALSE; + zend_argument_value_error(4, "must be one of SQL_CUR_USE_IF_NEEDED, " + "SQL_CUR_USE_ODBC, or SQL_CUR_USE_DRIVER"); + RETURN_THROWS(); } } @@ -2483,6 +2485,11 @@ PHP_FUNCTION(odbc_field_name) RETURN_THROWS(); } + if (pv_num < 1) { + zend_argument_value_error(2, "must be greater than or equal to 1"); + RETURN_THROWS(); + } + if (result->numcols == 0) { php_error_docref(NULL, E_WARNING, "No tuples available at this result index"); RETURN_FALSE; @@ -2493,11 +2500,6 @@ PHP_FUNCTION(odbc_field_name) RETURN_FALSE; } - if (pv_num < 1) { - php_error_docref(NULL, E_WARNING, "Field numbering starts at 1"); - RETURN_FALSE; - } - RETURN_STRING(result->values[pv_num - 1].name); } /* }}} */ @@ -2519,6 +2521,11 @@ PHP_FUNCTION(odbc_field_type) RETURN_THROWS(); } + if (pv_num < 1) { + zend_argument_value_error(2, "must be greater than or equal to 1"); + RETURN_THROWS(); + } + if (result->numcols == 0) { php_error_docref(NULL, E_WARNING, "No tuples available at this result index"); RETURN_FALSE; @@ -2529,11 +2536,6 @@ PHP_FUNCTION(odbc_field_type) RETURN_FALSE; } - if (pv_num < 1) { - php_error_docref(NULL, E_WARNING, "Field numbering starts at 1"); - RETURN_FALSE; - } - PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, SQL_COLUMN_TYPE_NAME, tmp, 31, &tmplen, NULL); RETURN_STRING(tmp); } @@ -2733,9 +2735,8 @@ PHP_FUNCTION(odbc_setoption) } break; default: - php_error_docref(NULL, E_WARNING, "Unknown option type"); - RETURN_FALSE; - break; + zend_argument_value_error(2, "must be 1 for SQLSetConnectOption(), or 2 for SQLSetStmtOption()"); + RETURN_THROWS(); } RETURN_TRUE; diff --git a/ext/odbc/tests/odbc_data_source_001.phpt b/ext/odbc/tests/odbc_data_source_001.phpt index fedbbc1b02b79..e090aeccac8e4 100644 --- a/ext/odbc/tests/odbc_data_source_001.phpt +++ b/ext/odbc/tests/odbc_data_source_001.phpt @@ -14,12 +14,16 @@ include 'config.inc'; $conn = odbc_connect($dsn, $user, $pass); -var_dump(odbc_data_source($conn, NULL)); +try { + var_dump(odbc_data_source($conn, NULL)); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} var_dump(odbc_data_source($conn, SQL_FETCH_FIRST)); ?> --EXPECTF-- -Warning: odbc_data_source(): Invalid fetch type (0) in %s on line %d +odbc_data_source(): Argument #2 ($fetch_type) must be SQL_FETCH_FIRST or SQL_FETCH_NEXT bool(false) array(%d) { %a From d9779ba00992f0beb7ab117bed006e3f70a892e1 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Sun, 13 Sep 2020 15:31:36 +0200 Subject: [PATCH 2/3] Fix compiler error on Win an improve error message --- ext/odbc/php_odbc.c | 4 ++-- ext/odbc/tests/odbc_data_source_001.phpt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c index f3f7b1910965d..a6d6d4ceda985 100644 --- a/ext/odbc/php_odbc.c +++ b/ext/odbc/php_odbc.c @@ -1247,8 +1247,8 @@ PHP_FUNCTION(odbc_data_source) fetch_type = (SQLSMALLINT) zv_fetch_type; if (!(fetch_type == SQL_FETCH_FIRST || fetch_type == SQL_FETCH_NEXT)) { - zend_argument_value_error(2, "must be SQL_FETCH_FIRST or SQL_FETCH_NEXT"); - RETURN_THROWS() + zend_argument_value_error(2, "must either be SQL_FETCH_FIRST or SQL_FETCH_NEXT"); + RETURN_THROWS(); } if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(zv_conn), "ODBC-Link", le_conn, le_pconn))) { diff --git a/ext/odbc/tests/odbc_data_source_001.phpt b/ext/odbc/tests/odbc_data_source_001.phpt index e090aeccac8e4..5f9da651bf369 100644 --- a/ext/odbc/tests/odbc_data_source_001.phpt +++ b/ext/odbc/tests/odbc_data_source_001.phpt @@ -23,7 +23,7 @@ var_dump(odbc_data_source($conn, SQL_FETCH_FIRST)); ?> --EXPECTF-- -odbc_data_source(): Argument #2 ($fetch_type) must be SQL_FETCH_FIRST or SQL_FETCH_NEXT +odbc_data_source(): Argument #2 ($fetch_type) must either be SQL_FETCH_FIRST or SQL_FETCH_NEXT bool(false) array(%d) { %a From 2e5bc1e8ee76203485c36f59dcadc4361efc84cd Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 14 Sep 2020 18:47:59 +0200 Subject: [PATCH 3/3] Use greater than 0 in error messages --- ext/odbc/php_odbc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c index a6d6d4ceda985..2a1081816330a 100644 --- a/ext/odbc/php_odbc.c +++ b/ext/odbc/php_odbc.c @@ -788,7 +788,7 @@ void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type) } if (pv_num < 1) { - zend_argument_value_error(2, "must be greater than or equal to 1"); + zend_argument_value_error(2, "must be greater than 0"); RETURN_THROWS(); } @@ -2486,7 +2486,7 @@ PHP_FUNCTION(odbc_field_name) } if (pv_num < 1) { - zend_argument_value_error(2, "must be greater than or equal to 1"); + zend_argument_value_error(2, "must be greater than 0"); RETURN_THROWS(); } @@ -2522,7 +2522,7 @@ PHP_FUNCTION(odbc_field_type) } if (pv_num < 1) { - zend_argument_value_error(2, "must be greater than or equal to 1"); + zend_argument_value_error(2, "must be greater than 0"); RETURN_THROWS(); }