Skip to content

Commit 7e6b840

Browse files
kamil-tekielanikic
authored andcommitted
Consistent error handling in mysqli_poll
This error condition should not actually be reachable, but change it to be consistent with the other ones. Also fix a memory leak. Closes GH-6340.
1 parent c6784ca commit 7e6b840

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

ext/mysqli/mysqli_nonapi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ static int mysqlnd_zval_array_to_mysqlnd_array(zval *in_array, MYSQLND ***out_ar
740740
MYSQLI_RESOURCE *my_res;
741741
mysqli_object *intern = Z_MYSQLI_P(elem);
742742
if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
743-
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
743+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
744744
return FAILURE;
745745
}
746746
mysql = (MY_MYSQL*) my_res->ptr;
@@ -761,12 +761,11 @@ static int mysqlnd_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *out_a
761761
MYSQLND **p = in_array;
762762
zval dest_array;
763763
zval *elem, *dest_elem;
764-
int ret = 0, i = 0;
764+
int ret = 0;
765765

766766
array_init_size(&dest_array, zend_hash_num_elements(Z_ARRVAL_P(out_array)));
767767

768768
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(out_array), elem) {
769-
i++;
770769
if (Z_TYPE_P(elem) != IS_OBJECT ||
771770
!instanceof_function(Z_OBJCE_P(elem), mysqli_link_class_entry)) {
772771
continue;
@@ -776,8 +775,8 @@ static int mysqlnd_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *out_a
776775
MYSQLI_RESOURCE *my_res;
777776
mysqli_object *intern = Z_MYSQLI_P(elem);
778777
if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
779-
php_error_docref(NULL, E_WARNING, "[%d] Couldn't fetch %s", i, ZSTR_VAL(intern->zo.ce->name));
780-
continue;
778+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
779+
return FAILURE;
781780
}
782781
mysql = (MY_MYSQL *) my_res->ptr;
783782
if (mysql->mysql == *p) {
@@ -867,6 +866,7 @@ PHP_FUNCTION(mysqli_poll)
867866
if (e_array != NULL) {
868867
if (mysqlnd_zval_array_to_mysqlnd_array(e_array, &new_e_array) == FAILURE) {
869868
efree(new_e_array);
869+
efree(new_r_array);
870870
RETURN_THROWS();
871871
}
872872
}

ext/mysqli/mysqli_warning.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static int mysqli_warning_message(mysqli_object *obj, zval *retval, zend_bool qu
201201

202202
if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
203203
if (!quiet) {
204-
zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
204+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
205205
}
206206

207207
return FAILURE;
@@ -221,7 +221,7 @@ static int mysqli_warning_sqlstate(mysqli_object *obj, zval *retval, zend_bool q
221221

222222
if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
223223
if (!quiet) {
224-
zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
224+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
225225
}
226226

227227
return FAILURE;
@@ -241,7 +241,7 @@ static int mysqli_warning_errno(mysqli_object *obj, zval *retval, zend_bool quie
241241

242242
if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
243243
if (!quiet) {
244-
zend_throw_error(NULL, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
244+
zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
245245
}
246246

247247
return FAILURE;

ext/mysqli/tests/mysqli_poll.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ if (!$IS_MYSQLND)
4040
echo $e->getMessage() . \PHP_EOL;
4141
}
4242

43+
$link->close();
44+
$read[0] = get_connection();
45+
try {
46+
mysqli_poll($read, $error, $reject, 0, 1);
47+
} catch (\Error $e) {
48+
echo $e->getMessage() . \PHP_EOL;
49+
}
50+
4351
function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax) {
4452

4553
if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 1000)))
@@ -116,6 +124,7 @@ if (!$IS_MYSQLND)
116124
--EXPECTF--
117125
mysqli_poll(): Argument #4 ($seconds) must be greater than or equal to 0
118126
mysqli_poll(): Argument #5 ($microseconds) must be greater than or equal to 0
127+
mysqli object is already closed
119128
[012 + 6] Rejecting thread %d: 0/
120129
[013 + 6] Rejecting thread %d: 0/
121130
[014 + 6] Rejecting thread %d: 0/

0 commit comments

Comments
 (0)