Bug 103153 - The recursive call in C++ leads to crashes of GCC.
Summary: The recursive call in C++ leads to crashes of GCC.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-09 10:19 UTC by Xinmeng Xia
Modified: 2021-11-09 10:39 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-11-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Xinmeng Xia 2021-11-09 10:19:20 UTC
The following code can be well compiled, however when execute it, GCC crashes with segmentation fault.
 
========================
#include <iostream>
#include <exception>
int main(){
   try{
       throw -1;
   }
   catch(int e){
       main();
   }
   return 0;
}
========================

Segmentation fault (core dumped)

gcc version 9.3.0 on Ubuntu 20.04
Comment 1 Martin Liška 2021-11-09 10:23:09 UTC
Well, the code causes a stack overflow, it's invalid if I see correctly.
Comment 2 Martin Liška 2021-11-09 10:24:09 UTC
g++ pr103153.C && valgrind ./a.out
==1687== Memcheck, a memory error detector
==1687== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1687== Using Valgrind-3.17.0.GIT and LibVEX; rerun with -h for copyright info
==1687== Command: ./a.out
==1687== 
==1687== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==1687== 
==1687== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==1687==  Access not within mapped region at address 0x1FFE801FE8
==1687== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==1687==    at 0x4B9D520: get_cie_encoding (unwind-dw2-fde.c:300)
==1687==  If you believe this happened as a result of a stack
==1687==  overflow in your program's main thread (unlikely but
==1687==  possible), you can try to increase the size of the
==1687==  main thread stack using the --main-stacksize= flag.
==1687==  The main thread stack size used in this run was 8388608.
==1687== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
Comment 3 Andrew Pinski 2021-11-09 10:27:33 UTC
"however when execute it"

Yes because the code has an infinite stack usage.
Comment 4 Jakub Jelinek 2021-11-09 10:30:10 UTC
The code is invalid both because of the endless recursion into which it runs and because in C++ main can't be called.  See
https://eel.is/c++draft/basic.start.main#3
Comment 5 Jonathan Wakely 2021-11-09 10:39:00 UTC
(In reply to Xinmeng Xia from comment #0)
> The following code can be well compiled, however when execute it, GCC
> crashes with segmentation fault.

No, your program crashes, GCC doesn't.

Any use of main is undefined in C++, especially calling it. This program is just broken and of course it crashes.