Explicitly validate popen mode

To avoid behavior differences due to libc.
This commit is contained in:
Nikita Popov 2020-08-05 11:27:13 +02:00
parent 0dda242bde
commit ab36540bdd
2 changed files with 9 additions and 1 deletions

View file

@ -930,10 +930,18 @@ PHP_FUNCTION(popen)
char *z = memchr(posix_mode, 'b', mode_len);
if (z) {
memmove(z, z + 1, mode_len - (z - posix_mode));
mode_len--;
}
}
#endif
/* Musl only partially validates the mode. Manually check it to ensure consistent behavior. */
if (mode_len != 1 || (*posix_mode != 'r' && *posix_mode != 'w')) {
php_error_docref2(NULL, command, posix_mode, E_WARNING, "Invalid mode");
efree(posix_mode);
RETURN_FALSE;
}
fp = VCWD_POPEN(command, posix_mode);
if (!fp) {
php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));

View file

@ -22,7 +22,7 @@ unlink($file_path."/popen.tmp");
--EXPECTF--
*** Testing for error conditions ***
Warning: popen(abc.txt,rw): %s on line %d
Warning: popen(abc.txt,rw): Invalid mode in %s on line %d
bool(false)
--- Done ---