7

Code:

#include <iostream>
#include <string>
#include <fstream>
int main(int argc, char *argv[]) {
    std::string filename = "dummyfile";
    std::ifstream infile(filename);
    std::string line;
    std::getline(infile, line);
    return 0;
}

compiled and linked with:

g++-7 -std=c++17 -flto mycode.cpp

results in:

* Error in `./a.out': free(): invalid pointer: 0x0000000000602200 * Aborted (core dumped)

It compiles and runs fine without the -flto flag. Keeping -flto but changing -std=c++17 to -std=c++14 or -std=c++11 would make it work too. So it seems that there is something about the combination of flto and c++17.

gdb result:

#0  0x00007ffff7a4acc9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007ffff7a4e0d8 in __GI_abort () at abort.c:89
#2  0x00007ffff7a87f24 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff7b966c8 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff7a941fe in malloc_printerr (ptr=<optimized out>, str=0x7ffff7b927b9 "free(): invalid pointer", action=1) at malloc.c:4996
#4  _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:3840
#5  0x00007ffff77c6e25 in std::string::reserve(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff77c706a in std::string::append(char const*, unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff7784aba in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x000000000040130b in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) ()
#9  0x00000000004010cd in main () 

Compiler and OS: g++-7 (Ubuntu 7.2.0-1ubuntu1~14.04) 7.2.0

Praetorian
  • 106,671
  • 19
  • 240
  • 328
narengi
  • 1,345
  • 3
  • 17
  • 38
  • I am unable to reproduce this with `GCC 7.2.1` on `Linux`. What OS? – Galik Jan 29 '18 at 16:29
  • @Galik oops, sorry I forgot to mention that. I just edited the question – narengi Jan 29 '18 at 16:31
  • "7.2.0-1ubuntu1~14.04" -- On Ubuntu 16.04, I'm seeing GCC 5.4 as the latest GCC available. Did you install your GCC 7 from some unofficial repository? –  Jan 29 '18 at 16:38
  • @hvd: If I remember correctly, I installed with `apt-get g++-7`. this is the result of `dpkg -s g++-7`: Package: g++-7 Status: install ok installed Priority: optional Section: devel Installed-Size: 162548 Maintainer: Ubuntu Core developers Architecture: amd64 Source: gcc-7 Version: 7.2.0-1ubuntu1~14.04 Provides: c++-compiler, c++abi2-dev Description: GNU C++ compiler This is the GNU C++ compiler, a fairly portable optimizing compiler for C++. Homepage: http://gcc.gnu.org/ Original-Maintainer: Debian GCC Maintainers – narengi Jan 29 '18 at 16:49
  • @narengi A Google search suggests you may have obtained this from https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test. Does that look familiar to you? –  Jan 29 '18 at 16:53
  • @hvd I am almost certain that I didn't obtain it from that source. But how can I make sure? – narengi Jan 29 '18 at 16:58
  • @narengi With `apt-cache showpkg`, see https://askubuntu.com/questions/8560/how-do-i-find-out-which-repository-a-package-comes-from –  Jan 29 '18 at 18:43
  • @hvd `apt-cache showpkg g++-7` gives `Package: g++-7 Versions: 7.2.0-1ubuntu1~14.04 (/var/lib/apt/lists/ppa.launchpad.net_ubuntu-toolchain-r_test_ubuntu_dists_trusty_main_binary-amd64_Packages)`. So after all, it seems that the package does come from that ubuntu toolchain that you had speculated – narengi Jan 29 '18 at 21:56

0 Answers0