Merge pull request #8182 from xXPBSXx/fix/flock-bad-descriptor-fb

fix(libkernel): fail flock on an invalid descriptor with EBADF and on a pipe with EOPNOTSUPP
This commit is contained in:
Dean authored and GitHub committed 2026-10-20 16:23:47 +13:00
commit 28a9be72a4
2 files changed
+16

No files matched your search

@@ -768,6 +768,15 @@ int APS5_VABI dup2_nid_postfix(int from, int to) {
int APS5_VABI flock_nid_postfix(int d, int operation) {
if (GuestSockets::IsOpen(d)) return PosixFailure(GUEST_EOPNOTSUPP);
File::RefuseArchivedFile(d, "flock");
#ifdef _WIN32
const auto handle = reinterpret_cast<HANDLE>(WithoutParameterHandler([d] { return ::_get_osfhandle(d); }));
if (handle == INVALID_HANDLE_VALUE) return PosixFailure(GUEST_EBADF);
if (::GetFileType(handle) == FILE_TYPE_PIPE) return PosixFailure(GUEST_EOPNOTSUPP);
#else
struct stat status;
if (::fstat(d, &status) != 0) return PosixFailure(GUEST_EBADF);
if (S_ISFIFO(status.st_mode)) return PosixFailure(GUEST_EOPNOTSUPP);
#endif
const int type = operation & 8 ? 8 : operation & 2 ? 2 : operation & 1 ? 1 : 0;
if (type == 0) return PosixFailure(GUEST_EBADF);
if (NativeFlock(d, type | (operation & 4)) != 0) {
+7
View File
@@ -981,6 +981,13 @@ int main() {
Require(flock_nid_postfix(other, 2 | 4) == 0);
Require(flock_nid_postfix(other, 8) == 0);
Require(flock_nid_postfix(locked, 2 | 4) == 0);
Require(flock_nid_postfix(-1, 2 | 4) == -1 && *__error_nid_postfix() == 9);
Require(flock_nid_postfix(0x7fffffff, 1) == -1 && *__error_nid_postfix() == 9);
int flockPipe[2] = {-1, -1};
Require(pipe_nid_postfix(flockPipe) == 0);
Require(flock_nid_postfix(flockPipe[0], 2 | 4) == -1 && *__error_nid_postfix() == 45);
Require(flock_nid_postfix(flockPipe[1], 8) == -1 && *__error_nid_postfix() == 45);
Require(close_nid_postfix(flockPipe[0]) == 0 && close_nid_postfix(flockPipe[1]) == 0);
Require(close_nid_postfix(locked) == 0);
const int recycled = open_nid_postfix(presentName.c_str(), 0, 0);
Require(recycled >= 0 && flock_nid_postfix(recycled, 8) == 0);