Skip to content

Commit ab36540

Browse files
committed
Explicitly validate popen mode
To avoid behavior differences due to libc.
1 parent 0dda242 commit ab36540

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

ext/standard/file.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,18 @@ PHP_FUNCTION(popen)
930930
char *z = memchr(posix_mode, 'b', mode_len);
931931
if (z) {
932932
memmove(z, z + 1, mode_len - (z - posix_mode));
933+
mode_len--;
933934
}
934935
}
935936
#endif
936937

938+
/* Musl only partially validates the mode. Manually check it to ensure consistent behavior. */
939+
if (mode_len != 1 || (*posix_mode != 'r' && *posix_mode != 'w')) {
940+
php_error_docref2(NULL, command, posix_mode, E_WARNING, "Invalid mode");
941+
efree(posix_mode);
942+
RETURN_FALSE;
943+
}
944+
937945
fp = VCWD_POPEN(command, posix_mode);
938946
if (!fp) {
939947
php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));

ext/standard/tests/file/popen_pclose_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ unlink($file_path."/popen.tmp");
2222
--EXPECTF--
2323
*** Testing for error conditions ***
2424

25-
Warning: popen(abc.txt,rw): %s on line %d
25+
Warning: popen(abc.txt,rw): Invalid mode in %s on line %d
2626
bool(false)
2727

2828
--- Done ---

0 commit comments

Comments
 (0)