llvm / llvm/llvm-project

[libc] `fileno` doesn't properly handle descriptorless streams.

Open
#223,550 4 comments 0 reactions 1 assignee Claimed by @linisha15 View on GitHub
libc
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Currently `fileno` doesn't properly handle `FILE` objects that aren't linux files. Specifically if you call `fileno` on a file from [`fopencookie`](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/fopencookie.cpp) or [`fmemopen`](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/fmemopen.cpp) it will blindly cast to [`LinuxFile`](https://github.com/llvm/llvm-project/blob/main/libc/src/__support/File/linux/file.cpp#L308) and try to read its file descriptor. POSIX says that this should return `EBADF`: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fileno.html.

Ideally the solution wouldn't involve adding fake file descriptors to `CookieFile` or `MemoryFile`. Consider if there's a way to positively identify a file as a `LinuxFile` instead.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.