Web lists-archives.com

[GCC] C++ program calling std::getline() crashes with certain compilation flags




The following C++ program crashes when compiled with GCC (both 5.4 and 6.3) under Cygwin, when compiled with both an optimization level higher than -O0 (i.e. -O1, -O2 or -O3) and the C++ standard set to -std=c++nn (for any supported nn, i.e. 98, 03, 11, 14 or 17):
```
#include <iostream>
#include <string>
int main()
{
    std::string s;
    std::getline(std::cin, s);
    std::cout << "You entered \"" << s << "\".\n";
    return 0;
}
```
On the other hand, when compiled with -std=gnu++nn or -O0, the program executes normally. For details, run the attached Bash script test_getline.sh, which produces the output contained in the attached file test_getline_Cygwin.txt . No similar problem shows with GCC under Linux.
It looks similar to the problem reported in https://cygwin.com/ml/cygwin/2017-07/msg00088.html which appears to be circumvented by passing -std=gnu++11 instead of -std=c++11.
Under GDB, a faulty executable produces a message like:
```
Thread 1 "test_getline" received signal SIGABRT, Aborted.
0x00000003bf12576b in cygstdc++-6!_ZNSs7reserveEm () from /usr/bin/cygstdc++-6.dll
```
Un-mangling _ZNSs7reserveEm through c++filt gives
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve(unsigned long)

Falk
CYGWIN_NT-6.1 guam 2.8.1(0.312/5/3) 2017-07-03 14:11 x86_64 Cygwin
g++ (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

### -std=c++98 -O0 ###: You entered "// Let's see if it works...".
### -std=c++98 -O1 ###: ./test_getline.sh: line 18:  3744 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++98 -O2 ###: ./test_getline.sh: line 18:  2516 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++98 -O3 ###: ./test_getline.sh: line 18: 10004 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++03 -O0 ###: You entered "// Let's see if it works...".
### -std=c++03 -O1 ###: ./test_getline.sh: line 18: 10228 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++03 -O2 ###: ./test_getline.sh: line 18:  6552 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++03 -O3 ###: ./test_getline.sh: line 18:  9348 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++11 -O0 ###: You entered "// Let's see if it works...".
### -std=c++11 -O1 ###: ./test_getline.sh: line 18:  9808 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++11 -O2 ###: ./test_getline.sh: line 18:  9912 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++11 -O3 ###: ./test_getline.sh: line 18:  9904 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++14 -O0 ###: You entered "// Let's see if it works...".
### -std=c++14 -O1 ###: ./test_getline.sh: line 18: 10040 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++14 -O2 ###: ./test_getline.sh: line 18:  9380 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++14 -O3 ###: ./test_getline.sh: line 18:  6224 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++17 -O0 ###: You entered "// Let's see if it works...".
### -std=c++17 -O1 ###: ./test_getline.sh: line 18:  1476 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++17 -O2 ###: ./test_getline.sh: line 18:  9572 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=c++17 -O3 ###: ./test_getline.sh: line 18: 10152 Aborted (core dumped) ./test_getline < test_getline.cxx
### -std=gnu++98 -O0 ###: You entered "// Let's see if it works...".
### -std=gnu++98 -O1 ###: You entered "// Let's see if it works...".
### -std=gnu++98 -O2 ###: You entered "// Let's see if it works...".
### -std=gnu++98 -O3 ###: You entered "// Let's see if it works...".
### -std=gnu++03 -O0 ###: You entered "// Let's see if it works...".
### -std=gnu++03 -O1 ###: You entered "// Let's see if it works...".
### -std=gnu++03 -O2 ###: You entered "// Let's see if it works...".
### -std=gnu++03 -O3 ###: You entered "// Let's see if it works...".
### -std=gnu++11 -O0 ###: You entered "// Let's see if it works...".
### -std=gnu++11 -O1 ###: You entered "// Let's see if it works...".
### -std=gnu++11 -O2 ###: You entered "// Let's see if it works...".
### -std=gnu++11 -O3 ###: You entered "// Let's see if it works...".
### -std=gnu++14 -O0 ###: You entered "// Let's see if it works...".
### -std=gnu++14 -O1 ###: You entered "// Let's see if it works...".
### -std=gnu++14 -O2 ###: You entered "// Let's see if it works...".
### -std=gnu++14 -O3 ###: You entered "// Let's see if it works...".
### -std=gnu++17 -O0 ###: You entered "// Let's see if it works...".
### -std=gnu++17 -O1 ###: You entered "// Let's see if it works...".
### -std=gnu++17 -O2 ###: You entered "// Let's see if it works...".
### -std=gnu++17 -O3 ###: You entered "// Let's see if it works...".

Attachment: test_getline.sh
Description: test_getline.sh

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple