[gcc r16-9520] c++: ICE on on systems without mmap support [PR124806]

Torbjorn Svensson via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:d564253eb6c859e266d3cae18e82fb4db9a88316

commit r16-9520-gd564253eb6c859e266d3cae18e82fb4db9a88316
Author: Torbjörn SVENSSON <[email protected]>
Date:   Sun Jul 26 23:25:50 2026 +0200

    c++: ICE on on systems without mmap support [PR124806]
    
    On systems without mmap support, cc1plus can crash when finishing module
    output after earlier errors prevented elf_out::begin from running.  In
    that case elf_out::end attempts to fill in the ELF header even though
    hdr.buffer was never initialized.
    
    gcc/cp/ChangeLog:
    
            PR c++/124806
            * module.cc (elf_out::began): New data member.
            (elf_out::begin): Set it after successful initialization.
            (elf_out::end): Do not finalize output that never began.
    
    Signed-off-by: Torbjörn SVENSSON <[email protected]>
    (cherry picked from commit 94556938452d2bde01f5954207294906bbb53d8f)

Diff:
---
 gcc/cp/module.cc | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index dcd8e772416c..83d3929415cb 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -1506,6 +1506,7 @@ class elf_out : public elf, public data::allocator {
 private:
   ptr_int_hash_map identtab;	/* Map of IDENTIFIERS to strtab offsets. */
   unsigned pos;			/* Write position in file.  */
+  bool began;			/* True if begin initialized output state.  */
 #if MAPPED_WRITING
   unsigned offset;		/* Offset of the mapping.  */
   unsigned extent;		/* Length of mapping.  */
@@ -1514,7 +1515,7 @@ private:
 
 public:
   elf_out (int fd, int e)
-    :parent (fd, e), identtab (500), pos (0)
+    :parent (fd, e), identtab (500), pos (0), began (false)
   {
 #if MAPPED_WRITING
     offset = extent = 0;
@@ -2226,7 +2227,10 @@ elf_out::begin ()
   memset (h, 0, sizeof (header));
   hdr.pos = hdr.size;
   write (hdr);
-  return !get_error ();
+  if (get_error ())
+    return false;
+  began = true;
+  return true;
 }
 
 /* Finish writing the file.  Write out the string & section tables.
@@ -2235,7 +2239,7 @@ elf_out::begin ()
 bool
 elf_out::end ()
 {
-  if (fd >= 0)
+  if (fd >= 0 && began)
     {
       /* Write the string table.  */
       unsigned strnam = name (".strtab");
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.