From 233f507fe62d2f166c32e92d6471969c72fe4643 Mon Sep 17 00:00:00 2001 From: Dharman Date: Tue, 24 Nov 2020 23:02:04 +0000 Subject: [PATCH] Fixed error reporting in mysqli_stmt::__construct For the sake of simplicity, I've synchronized the implementation with PHP 8, which means null values are also accepted. Closes GH-6454. --- ext/mysqli/mysqli.c | 47 ++++++++++++++------------------------- ext/mysqli/tests/063.phpt | 13 +++++++++-- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/ext/mysqli/mysqli.c b/ext/mysqli/mysqli.c index dcc5a16d5e..c4dbcb251a 100644 --- a/ext/mysqli/mysqli.c +++ b/ext/mysqli/mysqli.c @@ -1041,51 +1041,38 @@ PHP_FUNCTION(mysqli_stmt_construct) zval *mysql_link; MY_STMT *stmt; MYSQLI_RESOURCE *mysqli_resource; - char *statement; + char *statement = NULL; size_t statement_len; - switch (ZEND_NUM_ARGS()) - { - case 1: /* mysql_stmt_init */ - if (zend_parse_parameters(1, "O", &mysql_link, mysqli_link_class_entry)==FAILURE) { - return; - } - MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID); - - stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT)); - - stmt->stmt = mysql_stmt_init(mysql->mysql); - break; - case 2: - if (zend_parse_parameters(2, "Os", &mysql_link, mysqli_link_class_entry, &statement, &statement_len)==FAILURE) { - return; - } - MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID); - - stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT)); - - if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) { - mysql_stmt_prepare(stmt->stmt, (char *)statement, statement_len); - } - break; - default: - WRONG_PARAM_COUNT; - break; + if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|s!", &mysql_link, mysqli_link_class_entry, &statement, &statement_len) == FAILURE) { + return; } + MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID); - if (!stmt->stmt) { + stmt = (MY_STMT *) ecalloc(1, sizeof(MY_STMT)); + + if (!(stmt->stmt = mysql_stmt_init(mysql->mysql))) { efree(stmt); RETURN_FALSE; } + #ifndef MYSQLI_USE_MYSQLND ZVAL_COPY(&stmt->link_handle, mysql_link); #endif mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE)); mysqli_resource->ptr = (void *)stmt; - mysqli_resource->status = (ZEND_NUM_ARGS() == 1) ? MYSQLI_STATUS_INITIALIZED : MYSQLI_STATUS_VALID; + mysqli_resource->status = MYSQLI_STATUS_INITIALIZED; MYSQLI_REGISTER_RESOURCE_EX(mysqli_resource, getThis()); + + if (statement) { + if(mysql_stmt_prepare(stmt->stmt, statement, statement_len)) { + MYSQLI_REPORT_STMT_ERROR(stmt->stmt); + RETURN_FALSE; + } + mysqli_resource->status = MYSQLI_STATUS_VALID; + } } /* }}} */ diff --git a/ext/mysqli/tests/063.phpt b/ext/mysqli/tests/063.phpt index 26bfe48ee5..b76b2c9985 100644 --- a/ext/mysqli/tests/063.phpt +++ b/ext/mysqli/tests/063.phpt @@ -16,9 +16,18 @@ require_once('skipifconnectfailure.inc'); $stmt->bind_result($foo); $stmt->fetch(); $stmt->close(); - $mysql->close(); - var_dump($foo); + + mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); + try { + // an exception should be thrown from prepare (i.e. constructor) not from execute + $stmt = new mysqli_stmt($mysql, "SELECT invalid FROM DUAL"); + } catch(mysqli_sql_exception $e) { + echo $e->getMessage()."\n"; + } + + $mysql->close(); ?> --EXPECT-- string(3) "foo" +Unknown column 'invalid' in 'field list' -- 2.40.0