[gcc r17-3819] libcpp: Use -1 for closed header-unit fd [PR c++/127097]
Jason Merrill
jason@gcc.gnu.org
Tue Sep 1 14:53:21 GMT 2026
https://gcc.gnu.org/g:e1c8dac7ca8dde4d99776685dee43e05cd3ea6d2
commit r17-3819-ge1c8dac7ca8dde4d99776685dee43e05cd3ea6d2
Author: Alexey Klimkin <alexey.klimkin@intel.com>
Date: Tue Aug 25 20:36:48 2026 -0700
libcpp: Use -1 for closed header-unit fd [PR c++/127097]
_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 therefore skips
reopening the source and reads stdin. Descriptor zero is also a valid
result from open when stdin was previously closed.
Close every valid descriptor and restore -1. Add a module regression
that imports a header unit and includes the same file through an
alternate spelling.
Assisted-by: Claude Opus 5 (Anthropic)
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.
Signed-off-by: Alexey Klimkin <alexey.klimkin@intel.com>
Diff:
---
gcc/testsuite/g++.dg/modules/pragma-once-hu-1_b.C | 12 ++++++++++++
libcpp/files.cc | 4 ++--
gcc/testsuite/g++.dg/modules/pragma-once-hu-1_a.H | 12 ++++++++++++
3 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/gcc/testsuite/g++.dg/modules/pragma-once-hu-1_b.C b/gcc/testsuite/g++.dg/modules/pragma-once-hu-1_b.C
new file mode 100644
index 000000000000..b2871d9030a0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pragma-once-hu-1_b.C
@@ -0,0 +1,12 @@
+// PR c++/127097
+// { dg-additional-options "-fmodules" }
+
+import "pragma-once-hu-1_a.H";
+// Reuse the tracked sys directory for an alternate spelling without symlinks.
+#include "sys/../pragma-once-hu-1_a.H"
+
+int
+main ()
+{
+ return pragma_once_hu_f () != 1;
+}
diff --git a/libcpp/files.cc b/libcpp/files.cc
index 917b7799be97..b0ecb00de24c 100644
--- a/libcpp/files.cc
+++ b/libcpp/files.cc
@@ -1227,11 +1227,11 @@ _cpp_find_header_unit (cpp_reader *pfile, const char *name, bool angle,
{
if (_cpp_file *file = test_header_unit (pfile, name, angle, loc))
{
- if (file->fd > 0)
+ if (file->fd != -1)
{
/* Don't leave it open. */
close (file->fd);
- file->fd = 0;
+ file->fd = -1;
}
file->header_unit = +1;
diff --git a/gcc/testsuite/g++.dg/modules/pragma-once-hu-1_a.H b/gcc/testsuite/g++.dg/modules/pragma-once-hu-1_a.H
new file mode 100644
index 000000000000..549f677a7753
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pragma-once-hu-1_a.H
@@ -0,0 +1,12 @@
+// PR c++/127097
+// { dg-module-do run }
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+#pragma once
+
+inline int
+pragma_once_hu_f ()
+{
+ return 1;
+}
More information about the Gcc-cvs
mailing list