Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ PHP NEWS
(Sjoerd Langkemper)
. Raise a value error when the callback registered with CURLOPT_READFUNCTION
returns an unexpected long. (Sjoerd Langkemper)
. Fix bug GH-16513 (curl: exceptions in callbacks do not abort the request).
(Sjoerd Langkemper)

- Date:
. Update timelib to 2026.02. (Derick, timwolla)
Expand Down Expand Up @@ -42,6 +44,10 @@ PHP NEWS
. Fixed bug GH-23016 (NULL values in long columns come back as garbage
binary strings). (Calvin Buckley, iliaal)

- Readline:
. Fixed the interactive shell not waiting for the pager process to exit.
(Weilin Du)

- Reflection:
. Added ReflectionAttribute::inNamespace(),
ReflectionAttribute::getNamespaceName(), and
Expand Down
3 changes: 3 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ PHP 8.6 INTERNALS UPGRADE NOTES
zend_reflection_property_set_raw_value() to expose the functionality of
ReflectionProperty::setRawValueWithoutLazyInitialization() and
ReflectionProperty::setRawValue() to C extensions.
. Added zend_object_set_properties_reinitable() to centralise temporarily
allowing reinitialisation of initialised readonly properties during
controlled operations such as cloning and unserialisation.
. Added zend_argument_error_ex(), zend_argument_type_error_ex(),
zend_argument_value_error_ex().
. Added zend_ast_dup().
Expand Down
54 changes: 24 additions & 30 deletions Zend/zend_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
return object;
}

ZEND_API void ZEND_FASTCALL zend_object_set_properties_reinitable(zend_object *object, bool reinitable)
{
if (!ZEND_CLASS_HAS_READONLY_PROPS(object->ce)) {
return;
}

for (uint32_t i = 0; i < object->ce->default_properties_count; i++) {
zval *prop = OBJ_PROP_NUM(object, i);
if (reinitable) {
if (!Z_ISUNDEF_P(prop)) {
Z_PROP_FLAG_P(prop) |= IS_PROP_REINITABLE;
}
} else {
Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
}
}
}

ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, const zend_object *old_object)
{
bool has_clone_method = old_object->ce->clone != NULL;
Expand All @@ -206,10 +224,6 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
i_zval_ptr_dtor(dst);
ZVAL_COPY_VALUE_PROP(dst, src);
zval_add_ref(dst);
if (has_clone_method) {
/* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
Z_PROP_FLAG_P(dst) |= IS_PROP_REINITABLE;
}

if (UNEXPECTED(Z_ISREF_P(dst)) &&
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(dst)))) {
Expand Down Expand Up @@ -255,10 +269,7 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
ZVAL_COPY_VALUE(&new_prop, prop);
zval_add_ref(&new_prop);
}
if (has_clone_method) {
/* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
Z_PROP_FLAG_P(&new_prop) |= IS_PROP_REINITABLE;
}

if (EXPECTED(key)) {
_zend_hash_append(new_object->properties, key, &new_prop);
} else {
Expand All @@ -268,15 +279,9 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
}

if (has_clone_method) {
zend_object_set_properties_reinitable(new_object, /* reinitable */ true);
zend_call_known_instance_method_with_0_params(new_object->ce->clone, new_object, NULL);

if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
zval* prop = OBJ_PROP_NUM(new_object, i);
/* Unconditionally remove the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
}
}
zend_object_set_properties_reinitable(new_object, /* reinitable */ false);
}
}

Expand All @@ -285,13 +290,8 @@ ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const
zend_object *new_object = old_object->handlers->clone_obj(old_object);

if (EXPECTED(!EG(exception))) {
/* Unlock readonly properties once more. */
if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
zval* prop = OBJ_PROP_NUM(new_object, i);
Z_PROP_FLAG_P(prop) |= IS_PROP_REINITABLE;
}
}

zend_object_set_properties_reinitable(new_object, /* reinitable */ true);

const zend_class_entry *old_scope = EG(fake_scope);

Expand Down Expand Up @@ -322,13 +322,7 @@ ZEND_API zend_object *zend_objects_clone_obj_with(zend_object *old_object, const

EG(fake_scope) = old_scope;

/* Lock readonly properties once more. */
if (ZEND_CLASS_HAS_READONLY_PROPS(new_object->ce)) {
for (uint32_t i = 0; i < new_object->ce->default_properties_count; i++) {
zval* prop = OBJ_PROP_NUM(new_object, i);
Z_PROP_FLAG_P(prop) &= ~IS_PROP_REINITABLE;
}
}
zend_object_set_properties_reinitable(new_object, /* reinitable */ false);
}

return new_object;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ BEGIN_EXTERN_C()
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce);
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce);
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, const zend_object *old_object);
ZEND_API void ZEND_FASTCALL zend_object_set_properties_reinitable(zend_object *object, bool reinitable);

ZEND_API void zend_object_std_dtor(zend_object *object);
ZEND_API void zend_objects_destroy_object(zend_object *object);
Expand Down
30 changes: 18 additions & 12 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
_php_curl_verify_handlers(ch, /* reporterror */ true);
/* TODO Check callback returns an int or something castable to int */
length = php_curl_get_long(&retval);
} else {
length = -1;
}

zval_ptr_dtor(&argv[0]);
Expand Down Expand Up @@ -632,14 +634,14 @@ static int curl_fnmatch(void *ctx, const char *pattern, const char *string)
static int curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
{
php_curl *ch = (php_curl *)clientp;
int rval = 0;
int rval = 1; // error

#if PHP_CURL_DEBUG
fprintf(stderr, "curl_progress() called\n");
fprintf(stderr, "clientp = %p, dltotal = %f, dlnow = %f, ultotal = %f, ulnow = %f\n", clientp, dltotal, dlnow, ultotal, ulnow);
#endif
if (!ZEND_FCC_INITIALIZED(ch->handlers.progress)) {
return rval;
return 0; // ok
}

zval args[5];
Expand All @@ -659,8 +661,8 @@ static int curl_progress(void *clientp, double dltotal, double dlnow, double ult
if (!Z_ISUNDEF(retval)) {
_php_curl_verify_handlers(ch, /* reporterror */ true);
/* TODO Check callback returns an int or something castable to int */
if (0 != php_curl_get_long(&retval)) {
rval = 1;
if (0 == php_curl_get_long(&retval)) {
rval = 0; // ok
}
}

Expand All @@ -673,14 +675,14 @@ static int curl_progress(void *clientp, double dltotal, double dlnow, double ult
static int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
php_curl *ch = (php_curl *)clientp;
int rval = 0;
int rval = 1; // error

#if PHP_CURL_DEBUG
fprintf(stderr, "curl_xferinfo() called\n");
fprintf(stderr, "clientp = %p, dltotal = %ld, dlnow = %ld, ultotal = %ld, ulnow = %ld\n", clientp, dltotal, dlnow, ultotal, ulnow);
#endif
if (!ZEND_FCC_INITIALIZED(ch->handlers.xferinfo)) {
return rval;
if (UNEXPECTED(!ZEND_FCC_INITIALIZED(ch->handlers.xferinfo))) {
return 0; // ok
}

zval argv[5];
Expand All @@ -700,8 +702,8 @@ static int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, cu
if (!Z_ISUNDEF(retval)) {
_php_curl_verify_handlers(ch, /* reporterror */ true);
/* TODO Check callback returns an int or something castable to int */
if (0 != php_curl_get_long(&retval)) {
rval = 1;
if (0 == php_curl_get_long(&retval)) {
rval = 0; // ok
}
}

Expand All @@ -714,13 +716,13 @@ static int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, cu
static int curl_prereqfunction(void *clientp, char *conn_primary_ip, char *conn_local_ip, int conn_primary_port, int conn_local_port)
{
php_curl *ch = (php_curl *)clientp;
int rval = CURL_PREREQFUNC_OK;
int rval = CURL_PREREQFUNC_ABORT;

// when CURLOPT_PREREQFUNCTION is set to null, curl_prereqfunction still
// gets called. Return CURL_PREREQFUNC_OK immediately in this case to avoid
// zend_call_known_fcc() with an uninitialized FCC.
if (!ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
return rval;
if (UNEXPECTED(!ZEND_FCC_INITIALIZED(ch->handlers.prereq))) {
return CURL_PREREQFUNC_OK;
}

#if PHP_CURL_DEBUG
Expand Down Expand Up @@ -858,6 +860,8 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
}
// TODO Do type error if invalid type?
zval_ptr_dtor(&retval);
} else {
length = CURL_READFUNC_ABORT;
}

zval_ptr_dtor(&argv[0]);
Expand Down Expand Up @@ -952,6 +956,8 @@ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx
// TODO: Check for valid int type for return value
_php_curl_verify_handlers(ch, /* reporterror */ true);
length = php_curl_get_long(&retval);
} else {
length = -1;
}
zval_ptr_dtor(&argv[0]);
zval_ptr_dtor(&argv[1]);
Expand Down
45 changes: 45 additions & 0 deletions ext/curl/tests/curl_headerfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
CURLOPT_HEADERFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_HEADERFUNCTION')) {
die('skip CURLOPT_HEADERFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

echo "Test: header function throws exception\n";
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function (): int {
throw new Exception('header exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_WRITE_ERROR);

echo "Test: header function is null\n";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, null);
curl_exec($ch);
var_dump(curl_errno($ch) === CURLE_OK);

?>
--EXPECTF--
Test: header function throws exception
header exception
bool(true)
Test: header function is null
bool(true)
35 changes: 35 additions & 0 deletions ext/curl/tests/curl_prereqfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
CURLOPT_PREREQFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_PREREQFUNCTION')) {
die('skip CURLOPT_PREREQFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

curl_setopt($ch, CURLOPT_PREREQFUNCTION,
function (): int {
throw new Exception('prereq exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);

?>
--EXPECTF--
prereq exception
bool(true)
46 changes: 46 additions & 0 deletions ext/curl/tests/curl_progressfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
CURLOPT_PROGRESSFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_PROGRESSFUNCTION')) {
die('skip CURLOPT_PROGRESSFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

echo "Test: progress function throws exception\n";
curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,
function (): int {
throw new Exception('info exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);

echo "Test: progress function is null\n";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, null);
curl_exec($ch);
var_dump(curl_errno($ch) === CURLE_OK);

?>
--EXPECTF--
Test: progress function throws exception
info exception
bool(true)
Test: progress function is null
bool(true)
Loading