Skip to content

Commit cee9a59

Browse files
committed
Promote mysqli warnings to exceptions
1 parent cffff1f commit cee9a59

File tree

113 files changed

+1266
-790
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1266
-790
lines changed

ext/mysqli/mysqli.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ zval *mysqli_read_property(zend_object *object, zend_string *name, int type, voi
312312
}
313313

314314
if (hnd) {
315-
if (hnd->read_func(obj, rv, type == BP_VAR_IS) == SUCCESS || type != BP_VAR_IS) {
315+
if (hnd->read_func(obj, rv, type == BP_VAR_IS) == SUCCESS) {
316316
retval = rv;
317317
} else {
318318
retval = &EG(uninitialized_zval);
@@ -409,6 +409,7 @@ HashTable *mysqli_object_get_debug_info(zend_object *object, int *is_temp)
409409
ZEND_HASH_FOREACH_PTR(props, entry) {
410410
zval rv;
411411
zval *value;
412+
412413
value = mysqli_read_property(object, entry->name, BP_VAR_IS, 0, &rv);
413414
if (value != &EG(uninitialized_zval)) {
414415
zend_hash_add(retval, entry->name, value);
@@ -470,7 +471,7 @@ static MYSQLND *mysqli_convert_zv_to_mysqlnd(zval * zv)
470471
mysqli_object *intern = Z_MYSQLI_P(zv);
471472
if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
472473
/* We know that we have a mysqli object, so this failure should be emitted */
473-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));
474+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
474475
return NULL;
475476
}
476477
mysql = (MY_MYSQL *)(my_res->ptr);

ext/mysqli/mysqli_nonapi.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ static int mysqlnd_zval_array_to_mysqlnd_array(zval *in_array, MYSQLND ***out_ar
731731
int i = 0, current = 0;
732732

733733
if (Z_TYPE_P(in_array) != IS_ARRAY) {
734-
return 0;
734+
return SUCCESS;
735735
}
736736
*out_array = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(in_array)) + 1, sizeof(MYSQLND *));
737737
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(in_array), elem) {
@@ -744,18 +744,18 @@ static int mysqlnd_zval_array_to_mysqlnd_array(zval *in_array, MYSQLND ***out_ar
744744
MYSQLI_RESOURCE *my_res;
745745
mysqli_object *intern = Z_MYSQLI_P(elem);
746746
if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
747-
php_error_docref(NULL, E_WARNING, "[%d] Couldn't fetch %s", i, ZSTR_VAL(intern->zo.ce->name));
748-
continue;
747+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
748+
return FAILURE;
749749
}
750750
mysql = (MY_MYSQL*) my_res->ptr;
751751
if (MYSQLI_STATUS_VALID && my_res->status < MYSQLI_STATUS_VALID) {
752-
php_error_docref(NULL, E_WARNING, "Invalid object %d or resource %s", i, ZSTR_VAL(intern->zo.ce->name));
753-
continue;
752+
zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name));
753+
return FAILURE;
754754
}
755755
(*out_array)[current++] = mysql->mysql;
756756
}
757757
} ZEND_HASH_FOREACH_END();
758-
return 0;
758+
return SUCCESS;
759759
}
760760
/* }}} */
761761

@@ -859,10 +859,16 @@ PHP_FUNCTION(mysqli_poll)
859859
}
860860

861861
if (r_array != NULL) {
862-
mysqlnd_zval_array_to_mysqlnd_array(r_array, &new_r_array);
862+
if (mysqlnd_zval_array_to_mysqlnd_array(r_array, &new_r_array) == FAILURE) {
863+
efree(new_r_array);
864+
RETURN_THROWS();
865+
}
863866
}
864867
if (e_array != NULL) {
865-
mysqlnd_zval_array_to_mysqlnd_array(e_array, &new_e_array);
868+
if (mysqlnd_zval_array_to_mysqlnd_array(e_array, &new_e_array) == FAILURE) {
869+
efree(new_e_array);
870+
RETURN_THROWS();
871+
}
866872
}
867873

868874
ret = mysqlnd_poll(new_r_array, new_e_array, &new_dont_poll_array, sec, usec, &desc_num);

ext/mysqli/mysqli_prop.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@
3030
#define CHECK_STATUS(value, quiet) \
3131
if (!obj->ptr || ((MYSQLI_RESOURCE *)obj->ptr)->status < value ) { \
3232
if (!quiet) { \
33-
php_error_docref(NULL, E_WARNING, "Property access is not allowed yet"); \
33+
zend_throw_error(NULL, "Property access is not allowed yet"); \
3434
} \
35-
ZVAL_FALSE(retval); \
3635
return FAILURE; \
3736
} \
3837

3938
#define MYSQLI_GET_MYSQL(statusval) \
4039
MYSQL *p; \
4140
if (!obj->ptr || !(MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr) { \
4241
if (!quiet) { \
43-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name)); \
42+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name)); \
4443
} \
45-
ZVAL_FALSE(retval);\
4644
return FAILURE; \
4745
} else { \
4846
CHECK_STATUS(statusval, quiet);\
@@ -53,9 +51,8 @@ if (!obj->ptr || !(MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr) { \
5351
MYSQL_RES *p; \
5452
if (!obj->ptr) { \
5553
if (!quiet) { \
56-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name)); \
54+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name)); \
5755
} \
58-
ZVAL_NULL(retval);\
5956
return FAILURE; \
6057
} else { \
6158
CHECK_STATUS(statusval, quiet);\
@@ -66,9 +63,8 @@ if (!obj->ptr) { \
6663
MYSQL_STMT *p; \
6764
if (!obj->ptr) { \
6865
if (!quiet) { \
69-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name)); \
66+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name)); \
7067
} \
71-
ZVAL_NULL(retval);\
7268
return FAILURE; \
7369
} else { \
7470
CHECK_STATUS(statusval, quiet); \

ext/mysqli/mysqli_warning.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@ static int mysqli_warning_message(mysqli_object *obj, zval *retval, zend_bool qu
203203

204204
if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
205205
if (!quiet) {
206-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
206+
zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
207207
}
208-
ZVAL_NULL(retval);
209208

210209
return FAILURE;
211210
}
@@ -224,9 +223,8 @@ static int mysqli_warning_sqlstate(mysqli_object *obj, zval *retval, zend_bool q
224223

225224
if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
226225
if (!quiet) {
227-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
226+
zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
228227
}
229-
ZVAL_NULL(retval);
230228

231229
return FAILURE;
232230
}
@@ -245,9 +243,8 @@ static int mysqli_warning_errno(mysqli_object *obj, zval *retval, zend_bool quie
245243

246244
if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
247245
if (!quiet) {
248-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
246+
zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
249247
}
250-
ZVAL_NULL(retval);
251248

252249
return FAILURE;
253250
}

ext/mysqli/php_mysqli_structs.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,26 +246,26 @@ extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * resul
246246
MYSQLI_RESOURCE *my_res; \
247247
mysqli_object *intern = Z_MYSQLI_P(__id); \
248248
if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
249-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
250-
RETURN_FALSE;\
249+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));\
250+
RETURN_THROWS();\
251251
}\
252252
__ptr = (__type)my_res->ptr; \
253253
if (__check && my_res->status < __check) { \
254-
php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
255-
RETURN_FALSE;\
254+
zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name)); \
255+
RETURN_THROWS();\
256256
}\
257257
}
258258

259259
#define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
260260
{ \
261261
MYSQLI_RESOURCE *my_res; \
262262
if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
263-
php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
264-
return;\
265-
}\
263+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));\
264+
return;\
265+
}\
266266
__ptr = (__type)my_res->ptr; \
267267
if (__check && my_res->status < __check) { \
268-
php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
268+
zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name)); \
269269
return;\
270270
}\
271271
}

ext/mysqli/tests/bug28817.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ require_once('skipifconnectfailure.inc');
2222
$mysql = new my_mysql();
2323

2424
var_dump($mysql->p_test);
25-
var_dump($mysql->errno);
25+
try {
26+
$mysql->errno;
27+
} catch (Error $exception) {
28+
echo $exception->getMessage() . "\n";
29+
}
2630

2731
$mysql->connect($host, $user, $passwd, $db, $port, $socket);
2832
$mysql->select_db("nonexistingdb");
@@ -38,7 +42,5 @@ array(2) {
3842
[1]=>
3943
%s(3) "bar"
4044
}
41-
42-
Warning: main(): Couldn't fetch my_mysql in %s on line %d
43-
bool(false)
45+
my_mysql object is already closed
4446
bool(true)

ext/mysqli/tests/bug36420.phpt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ $mysqli = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
1414
$result = $mysqli->query('select 1');
1515

1616
$result->close();
17-
echo $result->num_rows;
17+
try {
18+
$result->num_rows;
19+
} catch (Error $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
1822

1923
$mysqli->close();
20-
echo $result->num_rows;
24+
try {
25+
$result->num_rows;
26+
} catch (Error $exception) {
27+
echo $exception->getMessage() . "\n";
28+
}
2129

2230
echo "Done\n";
2331
?>
2432
--EXPECTF--
25-
Warning: main(): Couldn't fetch mysqli_result in %s on line %d
26-
27-
Warning: main(): Couldn't fetch mysqli_result in %s on line %d
33+
mysqli_result object is already closed
34+
mysqli_result object is already closed
2835
Done

ext/mysqli/tests/bug36802.phpt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,35 @@ Bug #36802 (crashes with with mysqli_set_charset())
1515

1616
/* following operations should not work */
1717
if (method_exists($mysql, 'set_charset')) {
18-
$x[0] = @$mysql->set_charset('utf8');
18+
try {
19+
$mysql->set_charset('utf8');
20+
} catch (Error $exception) {
21+
echo $exception->getMessage() . "\n";
22+
}
1923
} else {
2024
$x[0] = false;
2125
}
22-
$x[1] = @$mysql->query("SELECT 'foo' FROM DUAL");
26+
27+
try {
28+
$mysql->query("SELECT 'foo' FROM DUAL");
29+
} catch (Error $exception) {
30+
echo $exception->getMessage() . "\n";
31+
}
2332

2433
/* following operations should work */
25-
$x[2] = ($mysql->client_version > 0);
26-
$x[3] = $mysql->errno;
34+
$x[1] = ($mysql->client_version > 0);
35+
$x[2] = $mysql->errno;
36+
2737
$mysql->close();
2838

2939
var_dump($x);
3040
?>
3141
--EXPECT--
32-
array(4) {
33-
[0]=>
34-
bool(false)
42+
mysqli object is not fully initialized
43+
mysqli object is not fully initialized
44+
array(2) {
3545
[1]=>
36-
bool(false)
37-
[2]=>
3846
bool(true)
39-
[3]=>
47+
[2]=>
4048
int(0)
4149
}

ext/mysqli/tests/bug63398.phpt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@ mysqli_close($link);
1919
$read = $error = $reject = array();
2020
$read[] = $error[] = $reject[] = $link;
2121

22-
mysqli_poll($read, $error, $reject, 1);
22+
try {
23+
mysqli_poll($read, $error, $reject, 1);
24+
} catch (Error $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
2327

2428
echo "okey";
2529
?>
2630
--EXPECTF--
27-
Warning: mysqli_poll(): [1] Couldn't fetch mysqli in %sbug63398.php on line %d
28-
29-
Warning: mysqli_poll(): [1] Couldn't fetch mysqli in %sbug63398.php on line %d
30-
31-
Warning: mysqli_poll(): No stream arrays were passed in %sbug63398.php on line %d
32-
33-
Warning: mysqli_poll(): [1] Couldn't fetch mysqli in %sbug63398.php on line %d
34-
35-
Warning: mysqli_poll(): [1] Couldn't fetch mysqli in %sbug63398.php on line %d
31+
mysqli object is already closed
3632
okey

ext/mysqli/tests/bug73462.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ require_once('skipifconnectfailure.inc');
1919

2020
/* Failed connection to invalid host */
2121
$mysql_2 = @new mysqli(' !!! invalid !!! ', $user, $passwd, $db);
22-
@$mysql_2->close();
22+
try {
23+
$mysql_2->close();
24+
} catch (Error $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
2327

2428
/* Re-use persistent connection */
2529
$mysql_3 = new mysqli('p:'.$host, $user, $passwd, $db);
@@ -38,4 +42,5 @@ require_once('skipifconnectfailure.inc');
3842
print "done!";
3943
?>
4044
--EXPECT--
45+
mysqli object is already closed
4146
done!

ext/mysqli/tests/bug75448.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ require_once('skipifconnectfailure.inc');
1111
require_once 'connect.inc';
1212
$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
1313
mysqli_close($link);
14-
$stmt = mysqli_prepare($link, 'SELECT VERSION()');
15-
var_dump($stmt);
16-
?>
17-
--EXPECTF--
18-
Warning: mysqli_prepare(): Couldn't fetch mysqli in %s on line %d
19-
bool(false)
14+
15+
try {
16+
mysqli_prepare($link, 'SELECT VERSION()');
17+
} catch (Error $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
20+
--EXPECT--
21+
mysqli object is already closed

ext/mysqli/tests/mysqli_affected_rows.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ mysqli_affected_rows()
110110

111111
mysqli_close($link);
112112

113-
if (false !== ($tmp = @mysqli_affected_rows($link)))
114-
printf("[033] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
113+
try {
114+
mysqli_affected_rows($link);
115+
} catch (Error $exception) {
116+
echo $exception->getMessage() . "\n";
117+
}
115118

116119
print "done!";
117120
?>
@@ -120,4 +123,5 @@ mysqli_affected_rows()
120123
require_once("clean_table.inc");
121124
?>
122125
--EXPECT--
126+
mysqli object is already closed
123127
done!

0 commit comments

Comments
 (0)