Fix header-unit file descriptor handling to prevent reading stdin
libcpp now correctly uses -1 as a sentinel for closed file descriptors related to header units, preventing accidental reads from stdin.
A bug in libcpp’s header unit handling caused a closed file descriptor to be recorded as zero instead of the intended -1 sentinel. This led to incorrect behavior where subsequent textual includes could mistakenly reopen and read from stdin. This fix ensures that valid descriptors are closed and the sentinel is correctly set to -1, resolving the issue and preventing incorrect file reading.
In Details
The _cpp_find_header_unit function in libcpp/files.cc incorrectly set a closed file descriptor to 0 instead of -1. This commit corrects this by closing descriptor 0 if it's valid and ensuring the sentinel for a closed descriptor is consistently -1. A regression test is added for modules that import a header unit and then include the same file via an alternate spelling.
- libcpp
- The C++ preprocessor library used by GCC. It handles tasks like macro expansion, include file processing, and conditional compilation.
- header unit
- A feature in C++20 that allows header files to be compiled into a binary representation, enabling faster and more reliable inclusion.
- file descriptor
- A handle used by the operating system to access an open file or other I/O resource. Typically an integer.
- sentinel value
- A special value used to indicate a specific condition, such as the end of a data structure or, in this case, a closed file descriptor.
- stdin
- Standard input, the default source of input for a program, typically the keyboard or a redirected file.