Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions ext/posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,15 @@ PHP_FUNCTION(posix_access)
RETURN_FALSE;
}

if (mode < 0 || (mode & ~(F_OK | R_OK | W_OK | X_OK))) {
zend_argument_value_error(
2,
"must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK"
Comment thread
devnexen marked this conversation as resolved.
Outdated
);
efree(path);
RETURN_THROWS();
}

ret = access(path, mode);
efree(path);

Expand Down
50 changes: 50 additions & 0 deletions ext/posix/tests/posix_access_flags.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
posix_access() flag (mode) validation
--FILE--
<?php

$dir = __DIR__;
$testfile = "$dir/testfile.txt";

// Create a temporary file for valid access tests
file_put_contents($testfile, "hello");

// Invalid: negative mode
try {
posix_access($testfile, -1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

// Invalid: mode with garbage bits
try {
posix_access($testfile, 01000); // S_ISVTX bit (sticky)
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

// Invalid: mode with unrelated high bits
try {
posix_access($testfile, 02000); // S_ISGID bit
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

// Valid: check read and write access
if (posix_access($testfile, POSIX_R_OK | POSIX_W_OK)) {
echo "Read/write access OK\n";
}

// Valid: check file existence
if (posix_access($testfile, POSIX_F_OK)) {
echo "File exists OK\n";
}

unlink($testfile);
Comment thread
devnexen marked this conversation as resolved.
Outdated
?>
--EXPECTF--
posix_access(): Argument #2 ($flags) must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK
posix_access(): Argument #2 ($flags) must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK
posix_access(): Argument #2 ($flags) must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK
Read/write access OK
File exists OK
Loading