Skip to content

Commit 8765e9f

Browse files
committed
ext/zip: Throw a TypeError if the return value of the cancel callback is not an int
1 parent dccd4af commit 8765e9f

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

ext/zip/php_zip.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -3066,18 +3066,24 @@ PHP_METHOD(ZipArchive, registerProgressCallback)
30663066
static int php_zip_cancel_callback(zip_t *arch, void *ptr)
30673067
{
30683068
zval cb_retval;
3069-
int retval = 0;
30703069
ze_zip_object *obj = ptr;
30713070

30723071
zend_call_known_fcc(&obj->cancel_callback, &cb_retval, 0, NULL, NULL);
30733072
if (Z_ISUNDEF(cb_retval)) {
30743073
/* Cancel if an exception has been thrown */
30753074
return -1;
30763075
}
3077-
retval = zval_get_long(&cb_retval);
3076+
bool failed = false;
3077+
zend_long retval = zval_try_get_long(&cb_retval, &failed);
3078+
if (failed) {
3079+
zend_type_error("Return value of callback provided to ZipArchive::registerCancelCallback()"
3080+
" must be of type int, %s returned", zend_zval_value_name(&cb_retval));
3081+
zval_ptr_dtor(&cb_retval);
3082+
return -1;
3083+
}
30783084
zval_ptr_dtor(&cb_retval);
30793085

3080-
return retval;
3086+
return (int) retval;
30813087
}
30823088

30833089
/* {{{ register a progression callback: int callback(double state); */

ext/zip/tests/oo_cancel_non_int_return.phpt

+10-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ var_dump($zip->registerCancelCallback(function () {
2424
return [];
2525
}));
2626
var_dump($zip->addFromString(PHP_BINARY, 'entry #1'));
27-
28-
var_dump($zip->close());
27+
try {
28+
var_dump($zip->close());
29+
} catch (Throwable $e) {
30+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
31+
}
2932
var_dump($zip->status == ZipArchive::ER_CANCELLED);
3033
var_dump($zip->getStatusString());
3134
@unlink($file);
@@ -38,10 +41,12 @@ $file = $dirname . '__tmp_oo_cancel_incorrect_return.zip';
3841

3942
@unlink($file);
4043
?>
41-
--EXPECT--
44+
--EXPECTF--
4245
bool(true)
4346
bool(true)
47+
48+
Warning: ZipArchive::close(): Operation cancelled in %s on line %d
49+
TypeError: Return value of callback provided to ZipArchive::registerCancelCallback() must be of type int, array returned
4450
bool(true)
45-
bool(false)
46-
string(8) "No error"
51+
string(19) "Operation cancelled"
4752
Done

0 commit comments

Comments
 (0)