Skip to content

Commit 5ed0602

Browse files
committed
Fix #76943: Inconsistent stream_wrapper_restore() errors
If restoring of any not registered built-in wrapper is requested, the function is supposed to fail with a warning, so we have to check this condition first. Furthermore, to be able to detect whether a built-in wrapper has been changed, it is not sufficient to check whether *any* userland wrapper has been registered, but rather whether the specific wrapper has been modified. Closes GH-6183.
1 parent 4000780 commit 5ed0602

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818

1919
- Standard:
2020
. Fixed bug #80114 (parse_url does not accept URLs with port 0). (cmb, twosee)
21+
. Fixed bug #76943 (Inconsistent stream_wrapper_restore() errors). (cmb)
2122

2223
01 Oct 2020, PHP 7.3.23
2324

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug #76943 (Inconsistent stream_wrapper_restore() errors)
3+
--SKIPIF--
4+
<?php
5+
if (!in_array('phar', stream_get_wrappers())) die('skip phar wrapper not registered');
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(stream_wrapper_restore('foo'));
10+
var_dump(stream_wrapper_restore('phar'));
11+
12+
stream_wrapper_register('bar', 'stdClass');
13+
14+
var_dump(stream_wrapper_restore('foo'));
15+
var_dump(stream_wrapper_restore('phar'));
16+
?>
17+
--EXPECTF--
18+
Warning: stream_wrapper_restore(): foo:// never existed, nothing to restore in %s on line %d
19+
bool(false)
20+
21+
Notice: stream_wrapper_restore(): phar:// was never changed, nothing to restore in %s on line %d
22+
bool(true)
23+
24+
Warning: stream_wrapper_restore(): foo:// never existed, nothing to restore in %s on line %d
25+
bool(false)
26+
27+
Notice: stream_wrapper_restore(): phar:// was never changed, nothing to restore in %s on line %d
28+
bool(true)

main/streams/userspace.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,23 +559,24 @@ PHP_FUNCTION(stream_wrapper_restore)
559559
{
560560
zend_string *protocol;
561561
php_stream_wrapper *wrapper;
562-
HashTable *global_wrapper_hash;
562+
HashTable *global_wrapper_hash, *wrapper_hash;
563563

564564
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &protocol) == FAILURE) {
565565
RETURN_FALSE;
566566
}
567567

568568
global_wrapper_hash = php_stream_get_url_stream_wrappers_hash_global();
569-
if (php_stream_get_url_stream_wrappers_hash() == global_wrapper_hash) {
570-
php_error_docref(NULL, E_NOTICE, "%s:// was never changed, nothing to restore", ZSTR_VAL(protocol));
571-
RETURN_TRUE;
572-
}
573-
574569
if ((wrapper = zend_hash_find_ptr(global_wrapper_hash, protocol)) == NULL) {
575570
php_error_docref(NULL, E_WARNING, "%s:// never existed, nothing to restore", ZSTR_VAL(protocol));
576571
RETURN_FALSE;
577572
}
578573

574+
wrapper_hash = php_stream_get_url_stream_wrappers_hash();
575+
if (wrapper_hash == global_wrapper_hash || zend_hash_find_ptr(wrapper_hash, protocol) == wrapper) {
576+
php_error_docref(NULL, E_NOTICE, "%s:// was never changed, nothing to restore", ZSTR_VAL(protocol));
577+
RETURN_TRUE;
578+
}
579+
579580
/* A failure here could be okay given that the protocol might have been merely unregistered */
580581
php_unregister_url_stream_wrapper_volatile(protocol);
581582

0 commit comments

Comments
 (0)