_cpp_find_header_unit closes the source descriptor after resolving a
header unit, but records zero instead of the established -1 sentinel.
A later textual include through an alternate pathname consequently
skips reopening the source and reads stdin.  This gives a misleading
"file is shorter than expected" warning followed by redefinition
errors.  The existing fd > 0 check also misses a real header descriptor
when open returns zero because stdin was closed.

Close every valid descriptor and restore -1 after closing it.

The regression imports a header unit and then includes the same
physical file through a sys/../ spelling.  It fails on unpatched trunk
and compiles, links, and runs after the fix.

Bootstrapped and regression-tested on x86_64-pc-linux-gnu, configured with
--disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,lto.
Compared against an unpatched build of the same parent commit: identical
FAIL/XPASS sets, and 15 new PASSes from the added test.

Per https://gcc.gnu.org/ai-policy.html: this change was LLM-assisted.
I have reviewed and understand the change and can
answer questions about it.

libcpp/
        PR c++/127097
        * files.cc (_cpp_find_header_unit): Close descriptor zero and set
        the closed descriptor sentinel to -1.

gcc/testsuite/
        PR c++/127097
        * g++.dg/modules/pragma-once-hu-1_a.H: New test.
        * g++.dg/modules/pragma-once-hu-1_b.C: New test.

Regards,
Alexey

Attachment: 0001-libcpp-Use-1-for-closed-header-unit-fd-PR-c-127097.patch
Description: 0001-libcpp-Use-1-for-closed-header-unit-fd-PR-c-127097.patch

Reply via email to