Skip to content

Fix GH-18281: making flock flags mutually exclusive. #18315

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 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,20 @@ PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */
}
/* }}} */

static inline bool php_is_valid_flock_flag(zend_long s) {
const zend_long sb = s & ~PHP_LOCK_NB;
return (sb == PHP_LOCK_UN || sb == PHP_LOCK_SH ||
sb == PHP_LOCK_EX);
}

PHPAPI void php_flock_common(php_stream *stream, zend_long operation,
uint32_t operation_arg_num, zval *wouldblock, zval *return_value)
{
int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN };
int act;

act = operation & PHP_LOCK_UN;
if (act < 1 || act > 3) {
if (!php_is_valid_flock_flag(operation)) {
zend_argument_value_error(operation_arg_num, "must be one of LOCK_SH, LOCK_EX, or LOCK_UN");
RETURN_THROWS();
}
Expand Down
8 changes: 6 additions & 2 deletions ext/standard/tests/file/flock.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ var_dump($would);
var_dump(flock($fp, LOCK_UN, $would));
var_dump($would);

var_dump(flock($fp, -1));
try {
var_dump(flock($fp, -1));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
var_dump(flock($fp, 0));
Expand Down Expand Up @@ -59,5 +63,5 @@ bool(true)
int(0)
bool(true)
int(0)
bool(true)
flock(): Argument #2 ($operation) must be one of LOCK_SH, LOCK_EX, or LOCK_UN
flock(): Argument #2 ($operation) must be one of LOCK_SH, LOCK_EX, or LOCK_UN
Loading