Skip to content

Commit 2576c57

Browse files
committed
Fix #76735: Incorrect message in fopen on invalid mode
We have to log errors in `stream_opener` callbacks to the wrapper's error log, because otherwise we may pick up an unrelated `errno` or a most generic message. Closes GH-6187.
1 parent 9439d65 commit 2576c57

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PHP NEWS
1919
- Standard:
2020
. Fixed bug #80114 (parse_url does not accept URLs with port 0). (cmb, twosee)
2121
. Fixed bug #76943 (Inconsistent stream_wrapper_restore() errors). (cmb)
22+
. Fixed bug #76735 (Incorrect message in fopen on invalid mode). (cmb)
2223

2324
01 Oct 2020, PHP 7.3.23
2425

ext/bz2/tests/002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ bool(false)
8383
resource(%d) of type (stream)
8484
resource(%d) of type (stream)
8585

86-
Warning: fopen(bz_open_002.txt): failed to open stream: Bad file %s in %s on line %d
86+
Warning: fopen(bz_open_002.txt): failed to open stream: `br' is not a valid mode for fopen in %s on line %d
8787

8888
Warning: bzopen(): first parameter has to be string or file-resource in %s on line %d
8989
bool(false)
9090

91-
Warning: fopen(bz_open_002.txt): failed to open stream: Bad file %s in %s on line %d
91+
Warning: fopen(bz_open_002.txt): failed to open stream: `br' is not a valid mode for fopen in %s on line %d
9292

9393
Warning: bzopen(): first parameter has to be string or file-resource in %s on line %d
9494
bool(false)

ext/standard/tests/file/bug76735.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Bug #76735 (Incorrect message in fopen on invalid mode)
3+
--FILE--
4+
<?php
5+
fopen(__FILE__, 'Q');
6+
?>
7+
--EXPECTF--
8+
Warning: fopen(%s): failed to open stream: `Q' is not a valid mode for fopen in %s on line %d

main/streams/plain_wrapper.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,9 +1021,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zen
10211021
char *persistent_id = NULL;
10221022

10231023
if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
1024-
if (options & REPORT_ERRORS) {
1025-
php_error_docref(NULL, E_WARNING, "`%s' is not a valid mode for fopen", mode);
1026-
}
1024+
php_stream_wrapper_log_error(&php_plain_files_wrapper, options, "`%s' is not a valid mode for fopen", mode);
10271025
return NULL;
10281026
}
10291027

0 commit comments

Comments
 (0)