Skip to content

Commit

Permalink
Fix bug GH-7746 (mysqli_sql_exception->sqlstate is inaccessible)
Browse files Browse the repository at this point in the history
Closes GH-7747
  • Loading branch information
kamil-tekiela committed Dec 30, 2021
1 parent 24be11f commit 8869bbe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PHP NEWS
. Fixed bug #81658 (MYSQL_OPT_LOAD_DATA_LOCAL_DIR not available in MariaDB).
(devnexen)
. Introduced MYSQLI_IS_MARIADB. (devnexen)
. Fixed bug GH-7746 (mysqli_sql_exception->getSqlState()). (Kamil Tekiela)

- OCI8:
. Fixed bug GH-7765 (php_oci_cleanup_global_handles segfaults at second
Expand Down
2 changes: 2 additions & 0 deletions ext/mysqli/mysqli.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@ public function next(): bool {}
final class mysqli_sql_exception extends RuntimeException
{
protected string $sqlstate = "00000";

public function getSqlState(): string {}
}

/** @refcount 1 */
Expand Down
7 changes: 6 additions & 1 deletion ext/mysqli/mysqli_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: e9f4dd04e7d01864c38033bcaf5e03b63e191deb */
* Stub hash: 78662c05cd463735a8a4101c0357fd0d2698d48e */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
Expand Down Expand Up @@ -720,6 +720,9 @@ ZEND_END_ARG_INFO()

#define arginfo_class_mysqli_warning_next arginfo_mysqli_thread_safe

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_mysqli_sql_exception_getSqlState, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()


ZEND_FUNCTION(mysqli_affected_rows);
ZEND_FUNCTION(mysqli_autocommit);
Expand Down Expand Up @@ -842,6 +845,7 @@ ZEND_METHOD(mysqli_result, getIterator);
ZEND_METHOD(mysqli_stmt, __construct);
ZEND_METHOD(mysqli_warning, __construct);
ZEND_METHOD(mysqli_warning, next);
ZEND_METHOD(mysqli_sql_exception, getSqlState);


static const zend_function_entry ext_functions[] = {
Expand Down Expand Up @@ -1083,6 +1087,7 @@ static const zend_function_entry class_mysqli_warning_methods[] = {


static const zend_function_entry class_mysqli_sql_exception_methods[] = {
ZEND_ME(mysqli_sql_exception, getSqlState, arginfo_class_mysqli_sql_exception_getSqlState, ZEND_ACC_PUBLIC)
ZEND_FE_END
};

Expand Down
14 changes: 14 additions & 0 deletions ext/mysqli/mysqli_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ void php_mysqli_throw_sql_exception(char *sqlstate, int errorno, char *format, .

zend_throw_exception_object(&sql_ex);
}

PHP_METHOD(mysqli_sql_exception, getSqlState)
{
zval *prop;
zval rv;

ZEND_PARSE_PARAMETERS_NONE();

prop = zend_read_property(mysqli_exception_class_entry, Z_OBJ_P(ZEND_THIS), "sqlstate", sizeof("sqlstate")-1, 1, &rv);
ZVAL_DEREF(prop);
zend_string *str = zval_get_string(prop);

RETURN_STR(str);
}
25 changes: 25 additions & 0 deletions ext/mysqli/tests/gh7746.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Bug #GH-7746 mysqli_sql_exception->sqlstate is inaccessible
--EXTENSIONS--
mysqli
--SKIPIF--
<?php
require_once "connect.inc";
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php
require 'connect.inc';
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {
$link->query("stuff");
} catch (mysqli_sql_exception $exception) {
var_dump($exception->getSqlState());
}

?>
--EXPECT--
string(5) "42000"

0 comments on commit 8869bbe

Please sign in to comment.