Skip to content

main/streams: Add Fast ZPP specifier to retrieve stream context #17964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
14 changes: 8 additions & 6 deletions ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,21 +746,23 @@ PHP_FUNCTION(hash_update_stream)
/* {{{ Pump data into the hashing algorithm from a file */
PHP_FUNCTION(hash_update_file)
{
zval *zhash, *zcontext = NULL;
zend_object *hash_obj;
php_hashcontext_object *hash;
php_stream_context *context = NULL;
php_stream *stream;
zend_string *filename;
char buf[1024];
ssize_t n;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJ_OF_CLASS(hash_obj, php_hashcontext_ce)
Z_PARAM_PATH_STR(filename)
Z_PARAM_OPTIONAL
PHP_Z_PARAM_STREAM_CONTEXT_OR_NULL_AS_DEFAULT_CONTEXT(context)
ZEND_PARSE_PARAMETERS_END();

hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
hash = php_hashcontext_from_object(hash_obj);
PHP_HASHCONTEXT_VERIFY(hash);
context = php_stream_context_from_zval(zcontext, 0);

stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
if (!stream) {
Expand Down
54 changes: 27 additions & 27 deletions ext/phar/func_interceptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
{
char *filename;
size_t filename_len;
zval *zcontext = NULL;
php_stream_context *context = NULL;

if (!PHAR_G(intercepted)) {
goto skip_phar;
Expand All @@ -35,9 +35,11 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
goto skip_phar;
}

if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|r!", &filename, &filename_len, &zcontext) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_QUIET, 1, 2)
Z_PARAM_PATH(filename, filename_len)
Z_PARAM_OPTIONAL
PHP_Z_PARAM_STREAM_CONTEXT_OR_NULL_AS_DEFAULT_CONTEXT(context)
ZEND_PARSE_PARAMETERS_END_EX(goto skip_phar;);

if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
char *arch, *entry;
Expand All @@ -51,7 +53,6 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
}

if (SUCCESS == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, &entry, &entry_len, 2, 0)) {
php_stream_context *context = NULL;
php_stream *stream;
char *name;

Expand All @@ -69,9 +70,7 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
}
efree(entry);
efree(arch);
if (zcontext) {
context = php_stream_context_from_zval(zcontext, 0);
}

stream = php_stream_opendir(name, REPORT_ERRORS, context);
efree(name);
if (!stream) {
Expand Down Expand Up @@ -163,7 +162,7 @@ PHP_FUNCTION(phar_file_get_contents) /* {{{ */
zend_long offset = -1;
zend_long maxlen;
bool maxlen_is_null = 1;
zval *zcontext = NULL;
php_stream_context *context = NULL;

if (!PHAR_G(intercepted)) {
goto skip_phar;
Expand All @@ -175,9 +174,14 @@ PHP_FUNCTION(phar_file_get_contents) /* {{{ */
}

/* Parse arguments */
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "P|br!ll!", &filename, &use_include_path, &zcontext, &offset, &maxlen, &maxlen_is_null) == FAILURE) {
goto skip_phar;
}
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_QUIET, 1, 5)
Z_PARAM_PATH_STR(filename)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(use_include_path)
PHP_Z_PARAM_STREAM_CONTEXT_OR_NULL_AS_DEFAULT_CONTEXT(context)
Z_PARAM_LONG(offset)
Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null)
ZEND_PARSE_PARAMETERS_END_EX(goto skip_phar;);

if (maxlen_is_null) {
maxlen = (ssize_t) PHP_STREAM_COPY_ALL;
Expand All @@ -192,13 +196,7 @@ PHP_FUNCTION(phar_file_get_contents) /* {{{ */
goto skip_phar;
}

php_stream_context *context = NULL;
php_stream *stream;

if (zcontext) {
context = php_stream_context_from_zval(zcontext, 0);
}
stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), "rb", 0 | REPORT_ERRORS, NULL, context);
php_stream *stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), "rb", 0 | REPORT_ERRORS, NULL, context);

zend_string_release_ex(name, false);

Expand Down Expand Up @@ -236,7 +234,7 @@ PHP_FUNCTION(phar_readfile) /* {{{ */
{
zend_string *filename;
bool use_include_path = 0;
zval *zcontext = NULL;
php_stream_context *context = NULL;

if (!PHAR_G(intercepted)) {
goto skip_phar;
Expand All @@ -246,19 +244,21 @@ PHP_FUNCTION(phar_readfile) /* {{{ */
&& !HT_IS_INITIALIZED(&cached_phars)) {
goto skip_phar;
}
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "P|br!", &filename, &use_include_path, &zcontext) == FAILURE) {
goto skip_phar;
}

ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_QUIET, 1, 3)
Z_PARAM_PATH_STR(filename)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(use_include_path)
PHP_Z_PARAM_STREAM_CONTEXT_OR_NULL_AS_DEFAULT_CONTEXT(context)
ZEND_PARSE_PARAMETERS_END_EX(goto skip_phar;);

if (use_include_path || (!IS_ABSOLUTE_PATH(ZSTR_VAL(filename), ZSTR_LEN(filename)) && !strstr(ZSTR_VAL(filename), "://"))) {
zend_string *name = phar_get_name_for_relative_paths(filename, use_include_path);
if (!name) {
goto skip_phar;
}

php_stream *stream;
php_stream_context *context = php_stream_context_from_zval(zcontext, 0);

stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), "rb", 0 | REPORT_ERRORS, NULL, context);
php_stream *stream = php_stream_open_wrapper_ex(ZSTR_VAL(name), "rb", 0 | REPORT_ERRORS, NULL, context);

zend_string_release_ex(name, false);
if (stream == NULL) {
Expand Down
8 changes: 4 additions & 4 deletions ext/simplexml/tests/gh12929.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ try {
file_get_contents($scheme . "://x");
} catch (Error $e) {
echo $e->getMessage(), "\n";
echo $e->getPrevious()->getMessage(), "\n";
var_dump($e->getPrevious());
}

$scheme = "foo2";
Expand All @@ -19,11 +19,11 @@ try {
file_get_contents($scheme . "://x");
} catch (Error $e) {
echo $e->getMessage(), "\n";
echo $e->getPrevious()->getMessage(), "\n";
var_dump($e->getPrevious());
}
?>
--EXPECT--
It's not possible to assign a complex type to properties, resource given
SimpleXMLElement is not properly initialized
It's not possible to assign a complex type to properties, resource given
NULL
SimpleXMLElement is not properly initialized
NULL
2 changes: 1 addition & 1 deletion ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ PHP_METHOD(SoapServer, handle)
*/

sapi_add_header("Content-Type: text/xml; charset=utf-8", sizeof("Content-Type: text/xml; charset=utf-8")-1, 1);
php_stream_context *context = php_stream_context_from_zval(NULL, false);
php_stream_context *context = php_stream_context_get_default(false);
php_stream *stream = php_stream_open_wrapper_ex(service->sdl->source, "rb", REPORT_ERRORS, NULL, context);
if (stream) {
php_stream_passthru(stream);
Expand Down
12 changes: 2 additions & 10 deletions ext/standard/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,15 @@ static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
{
char *dirname;
size_t dir_len;
zval *zcontext = NULL;
php_stream_context *context = NULL;
php_stream *dirp;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_PATH(dirname, dir_len)
Z_PARAM_OPTIONAL
Z_PARAM_RESOURCE_OR_NULL(zcontext)
PHP_Z_PARAM_STREAM_CONTEXT_OR_NULL_AS_DEFAULT_CONTEXT(context)
ZEND_PARSE_PARAMETERS_END();

context = php_stream_context_from_zval(zcontext, 0);

dirp = php_stream_opendir(dirname, REPORT_ERRORS, context);

if (dirp == NULL) {
Expand Down Expand Up @@ -469,25 +466,20 @@ PHP_FUNCTION(scandir)
zend_long flags = PHP_SCANDIR_SORT_ASCENDING;
zend_string **namelist;
int n, i;
zval *zcontext = NULL;
php_stream_context *context = NULL;

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_PATH(dirn, dirn_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(flags)
Z_PARAM_RESOURCE_OR_NULL(zcontext)
PHP_Z_PARAM_STREAM_CONTEXT_OR_NULL_AS_DEFAULT_CONTEXT(context)
ZEND_PARSE_PARAMETERS_END();

if (dirn_len < 1) {
zend_argument_must_not_be_empty_error(1);
RETURN_THROWS();
}

if (zcontext) {
context = php_stream_context_from_zval(zcontext, 0);
}

if (flags == PHP_SCANDIR_SORT_ASCENDING) {
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
} else if (flags == PHP_SCANDIR_SORT_NONE) {
Expand Down
Loading
Loading