From: Nikita Popov Date: Fri, 14 Aug 2020 09:32:41 +0000 (+0200) Subject: Simplify mysqli_stmt_bind_param() implementation X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=196671a2b2505f042de3790ae5830121c7feb475;p=php Simplify mysqli_stmt_bind_param() implementation By using zpp. --- diff --git a/ext/mysqli/mysqli_api.c b/ext/mysqli/mysqli_api.c index 76f336c004..d2dbcb7939 100644 --- a/ext/mysqli/mysqli_api.c +++ b/ext/mysqli/mysqli_api.c @@ -176,8 +176,7 @@ PHP_FUNCTION(mysqli_autocommit) /* {{{ mysqli_stmt_bind_param_do_bind */ #ifndef MYSQLI_USE_MYSQLND static -int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars, - zval *args, unsigned int start, const char * const types) +int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int num_vars, zval *args, const char * const types, unsigned int num_extra_args) { int i, ofs; MYSQL_BIND *bind; @@ -192,7 +191,7 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in bind = (MYSQL_BIND *) ecalloc(num_vars, sizeof(MYSQL_BIND)); ofs = 0; - for (i = start; i < argc; i++) { + for (i = 0; i < num_vars; i++) { zval *param; if (Z_ISREF(args[i])) { param = Z_REFVAL(args[i]); @@ -229,7 +228,7 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in break; default: - php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i+1); + php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i + num_extra_args + 1); rc = 1; goto end_1; } @@ -245,7 +244,7 @@ end_1: stmt->param.vars = safe_emalloc(num_vars, sizeof(zval), 0); for (i = 0; i < num_vars; i++) { if (bind[i].buffer_type != MYSQL_TYPE_LONG_BLOB) { - ZVAL_COPY(&stmt->param.vars[i], &args[i+start]); + ZVAL_COPY(&stmt->param.vars[i], &args[i]); } else { ZVAL_UNDEF(&stmt->param.vars[i]); } @@ -257,22 +256,21 @@ end_1: } #else static -int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars, - zval *args, unsigned int start, const char * const types) +int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int num_vars, zval *args, const char * const types, unsigned int num_extra_args) { unsigned int i; MYSQLND_PARAM_BIND *params; enum_func_status ret = FAIL; /* If no params -> skip binding and return directly */ - if (argc == start) { + if (num_vars == 0) { return PASS; } params = mysqlnd_stmt_alloc_param_bind(stmt->stmt); if (!params) { goto end; } - for (i = 0; i < (argc - start); i++) { + for (i = 0; i < num_vars; i++) { zend_uchar type; switch (types[i]) { case 'd': /* Double */ @@ -293,12 +291,12 @@ int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned in break; default: /* We count parameters from 1 */ - php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[i], i + start + 1); + php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[i], i + num_extra_args + 1); ret = FAIL; mysqlnd_stmt_free_param_bind(stmt->stmt, params); goto end; } - ZVAL_COPY_VALUE(¶ms[i].zv, &args[i + start]); + ZVAL_COPY_VALUE(¶ms[i].zv, &args[i]); params[i].type = type; } ret = mysqlnd_stmt_bind_param(stmt->stmt, params); @@ -313,41 +311,24 @@ end: PHP_FUNCTION(mysqli_stmt_bind_param) { zval *args; - int argc = ZEND_NUM_ARGS(); - int num_vars; - int start = 2; + int argc; MY_STMT *stmt; zval *mysql_stmt; char *types; size_t types_len; - zend_ulong rc; - /* calculate and check number of parameters */ - if (argc < 2) { - /* there has to be at least one pair */ - WRONG_PARAM_COUNT; - } - - if (zend_parse_method_parameters((getThis()) ? 1:2, getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry, - &types, &types_len) == FAILURE) { + if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os*", &mysql_stmt, mysqli_stmt_class_entry, &types, &types_len, &args, &argc) == FAILURE) { RETURN_THROWS(); } MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID); - num_vars = argc - 1; - if (getThis()) { - start = 1; - } else { - /* ignore handle parameter in procedural interface*/ - --num_vars; - } if (!types_len) { php_error_docref(NULL, E_WARNING, "Invalid type or no types specified"); RETURN_FALSE; } - if (types_len != (size_t)(argc - start)) { + if (types_len != (size_t) argc) { /* number of bind variables doesn't match number of elements in type definition string */ php_error_docref(NULL, E_WARNING, "Number of elements in type definition string doesn't match number of bind variables"); RETURN_FALSE; @@ -358,19 +339,8 @@ PHP_FUNCTION(mysqli_stmt_bind_param) RETURN_FALSE; } - args = safe_emalloc(argc, sizeof(zval), 0); - - if (zend_get_parameters_array_ex(argc, args) == FAILURE) { - zend_wrong_param_count(); - rc = 1; - } else { - rc = mysqli_stmt_bind_param_do_bind(stmt, argc, num_vars, args, start, types); - MYSQLI_REPORT_STMT_ERROR(stmt->stmt); - } - - efree(args); - - RETURN_BOOL(!rc); + RETVAL_BOOL(!mysqli_stmt_bind_param_do_bind(stmt, argc, args, types, getThis() ? 1 : 2)); + MYSQLI_REPORT_STMT_ERROR(stmt->stmt); } /* }}} */