<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/113036>113036</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            std::nested_exception::nested_ptr() is null or possibly indeterminate with Homebrew g++ on the Mac
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          alf-p-steinbach
      </td>
    </tr>
</table>

<pre>
    The following code works fine, as expected, with clang++, but reports nullpointer with Homebrew g++.

    #include <exception>
    #include <iostream>
    #include <stdexcept>
 using namespace std;

    #include <stdlib.h>         // exit, EXIT_...

    void throw_nested()
    {
        try {
 throw runtime_error( "First exception" );
        } catch( ... ) {
            throw_with_nested( runtime_error( "Second exception" ) );
        }
    }

    void check( const exception& x )
 {
        cout << "Checking...\n";
        try {
            //! rethrow_if_nested( x );     -- "libc++abi: terminating"
            if( const auto p_nest = dynamic_cast<const nested_exception*>( &x ) ) {
 cout << "Internal rethrowing...\n";
                //! p_nest->rethrow_nested();       -- "libc++abi: terminating"
 const auto x_ptr = p_nest->nested_ptr();
                if( not x_ptr ) {
                    cout << "!Nested exception pointer is null.\n";
                    exit( EXIT_FAILURE );           //! libc++
                }
                rethrow_exception( x_ptr );
            }
            cout << "Not nested.\n";
        } catch( ... ) {
            cout << "Nested!\n";
        }
 cout << "Done.\n";
    }

    auto main() -> int
    {
 try {
            throw_nested();
        } catch( const exception& x ) {
            check( x );
        }
 }

---

Results:

    $ echo $OPT
    -std=c++17 -pedantic-errors -Wall -Wextra

 [/Users/alf/f/source/compiler-bugs]
    $ clang++ ${=OPT} throw_if_nested.cpp -oa

    [/Users/alf/f/source/compiler-bugs]
 $ ./a
    Checking...
    Internal rethrowing...
    Nested!
 Done.

    [/Users/alf/f/source/compiler-bugs]
    $ g++ ${=OPT} throw_if_nested.cpp -ob

    [/Users/alf/f/source/compiler-bugs]
 $ ./b
    Checking...
    Internal rethrowing...
    !Nested exception pointer is null.

---

Compiler versions:

* Apple clang version 16.0.0 (clang-1600.0.26.3) / Target: arm64-apple-darwin24.0.0
* g++-14 (Homebrew GCC 14.2.0) 14.2.0

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVtuO2zYQ_Rr6ZSBBonx90IPXXrcB2rRIN0jfFhQ1tthQpEBSa-_fF9RlJd82SRtiLxZNzpxz5mhIZq04KMSUzB7IbDthtSu0SZncB1VgHQqVMV5MMp2_pk8Fwl5LqY9CHYDrHOGozVcLe6GQ0A0wC3iqkDvM_eNRuAK4ZOpA6EPzs4GsdmCw0sZZULWUlRbKoWnX_qpLzAweodsQkmhLonX7FwCA0EQoLuscgSQbPHGsnNCKJI_31ghtnUFWvrPEuryNNKyprWeoWIm2YhzBupwkD--jsS6XIgsLkjxCPwjdEboDPAnnyT_-_eHpOQyvaL1okYMrjD4-K7SNektCV6NMi4fhwQ9nXkeTzVYwtXKixGc0RhtCl0Ao3QljHQxCUQo-cHIRjiy2wJnjhd8WhqFfdJ20Sdyg9NUaoN7M_BdyrfKr1PfSj7lub-rDC-RffXCu1TmnOZxgkOsKNte18wUiycYD2_g4Qh18HWYbj-sKz7m6o9HWk9AYDLZKiP1Ih1PHrlkbBD6bFBlvzcwyQZI1ODSlUMwJ_1bQ6xRiP3BktdNQNQmAJFvIXxUrBX_mzDqSbNpFbfrnkR5rb-SmCvNTr_mIz4UeH_z7p5jsKb0vzbUSLb6AJI-9Jmce7tT4MT1G9E_PlTMN-yFRR7ly5i3JbZStmEq7Psw9W9_xCqHxxybXYDfoO5ZoG9g3lfKjbQDLtgHs1h9--_zpEc7UOdd0UOmO-uMXZjz6EozcsBy438R4M9aFDh9177O7dL-_hVzG7twSvxP5tnO3WuFNPDdaSOOlkgnVOga8jUAod6vH3n39b9r7HRnudao7uvQN7vSNJnlBLwiC8eMntLV0liTr6_NqCsgL7T_88efT8EXQnG_bznLx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