Skip to content

Commit c3b2849

Browse files
authored
[libc] Allow openat and creat to return fd 0. (#166466)
Previously, if the `open` or `openat` syscalls returned 0 as a (valid) file descriptor, the `creat` and `openat` wrappers would erroneously return -1.
1 parent 0502314 commit c3b2849

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

libc/src/fcntl/linux/creat.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ LLVM_LIBC_FUNCTION(int, creat, (const char *path, int mode_flags)) {
2727
SYS_openat, AT_FDCWD, path, O_CREAT | O_WRONLY | O_TRUNC, mode_flags);
2828
#endif
2929

30-
if (fd > 0)
31-
return fd;
32-
33-
libc_errno = -fd;
34-
return -1;
30+
if (fd < 0) {
31+
libc_errno = -fd;
32+
return -1;
33+
}
34+
return fd;
3535
}
3636

3737
} // namespace LIBC_NAMESPACE_DECL

libc/src/fcntl/linux/openat.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ LLVM_LIBC_FUNCTION(int, openat, (int dfd, const char *path, int flags, ...)) {
3232

3333
int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, dfd, path, flags,
3434
mode_flags);
35-
if (fd > 0)
36-
return fd;
37-
38-
libc_errno = -fd;
39-
return -1;
35+
if (fd < 0) {
36+
libc_errno = -fd;
37+
return -1;
38+
}
39+
return fd;
4040
}
4141

4242
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)