Skip to content

Commit 4c4ce97

Browse files
committed
Fix bug GH-7746 (mysqli_sql_exception->sqlstate is inaccessible)
Create getter instead of making the property public
1 parent 7daf012 commit 4c4ce97

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

ext/mysqli/mysqli.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ public function next(): bool {}
613613
final class mysqli_sql_exception extends RuntimeException
614614
{
615615
protected string $sqlstate = "00000";
616+
617+
public function getSqlState(): string {}
616618
}
617619

618620
/** @refcount 1 */

ext/mysqli/mysqli_arginfo.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e9f4dd04e7d01864c38033bcaf5e03b63e191deb */
2+
* Stub hash: 78662c05cd463735a8a4101c0357fd0d2698d48e */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
55
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
@@ -720,6 +720,9 @@ ZEND_END_ARG_INFO()
720720

721721
#define arginfo_class_mysqli_warning_next arginfo_mysqli_thread_safe
722722

723+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_mysqli_sql_exception_getSqlState, 0, 0, IS_STRING, 0)
724+
ZEND_END_ARG_INFO()
725+
723726

724727
ZEND_FUNCTION(mysqli_affected_rows);
725728
ZEND_FUNCTION(mysqli_autocommit);
@@ -842,6 +845,7 @@ ZEND_METHOD(mysqli_result, getIterator);
842845
ZEND_METHOD(mysqli_stmt, __construct);
843846
ZEND_METHOD(mysqli_warning, __construct);
844847
ZEND_METHOD(mysqli_warning, next);
848+
ZEND_METHOD(mysqli_sql_exception, getSqlState);
845849

846850

847851
static const zend_function_entry ext_functions[] = {
@@ -1083,6 +1087,7 @@ static const zend_function_entry class_mysqli_warning_methods[] = {
10831087

10841088

10851089
static const zend_function_entry class_mysqli_sql_exception_methods[] = {
1090+
ZEND_ME(mysqli_sql_exception, getSqlState, arginfo_class_mysqli_sql_exception_getSqlState, ZEND_ACC_PUBLIC)
10861091
ZEND_FE_END
10871092
};
10881093

ext/mysqli/mysqli_exception.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "php_mysqli_structs.h"
2727
#include "mysqli_priv.h"
2828
#include "zend_exceptions.h"
29+
#include "zend_string.h"
2930

3031
void php_mysqli_throw_sql_exception(char *sqlstate, int errorno, char *format, ...)
3132
{
@@ -63,3 +64,17 @@ void php_mysqli_throw_sql_exception(char *sqlstate, int errorno, char *format, .
6364

6465
zend_throw_exception_object(&sql_ex);
6566
}
67+
68+
PHP_METHOD(mysqli_sql_exception, getSqlState)
69+
{
70+
zval *prop;
71+
zval rv;
72+
73+
ZEND_PARSE_PARAMETERS_NONE();
74+
75+
prop = zend_read_property(mysqli_exception_class_entry, Z_OBJ_P(ZEND_THIS), "sqlstate", sizeof("sqlstate")-1, 1, &rv);
76+
ZVAL_DEREF(prop);
77+
zend_string *str = zval_get_string(prop);
78+
79+
RETURN_STR(str);
80+
}

ext/mysqli/tests/bugGH7746.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #GH-7746 mysqli_sql_exception->sqlstate is inaccessible
3+
--EXTENSIONS--
4+
mysqli
5+
--SKIPIF--
6+
<?php
7+
require_once "connect.inc";
8+
require_once 'skipifconnectfailure.inc';
9+
?>
10+
--FILE--
11+
<?php
12+
require 'connect.inc';
13+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
14+
15+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
16+
17+
try {
18+
$link->query("stuff");
19+
} catch (mysqli_sql_exception $exception) {
20+
var_dump($exception->getSqlState());
21+
}
22+
23+
?>
24+
--EXPECT--
25+
string(5) "42000"

0 commit comments

Comments
 (0)