问题描述
我在下面的代码文件中遇到了getline崩溃。
我建立了gcc7.2,因为系统更新不可用。
最小例子:
#include< iostream>
$ b $ int main(int argc,char * argv [])
{
std :: string line;
while(std :: getline(std :: cin,line))
{
}
return 0;
$ b $ p
$ b 在下面几行中,GCC_INSTALL_DIR表示我自己的gcc被安装的目录
输出:
./ a.out
a
***`./中的错误a.out':free():无效指针:0x0000000000602200 ***
======= Backtrace:=========
/lib64/libc.so.6 (+ 0x7cfe1)[0x7f392a8f3fe1]
[GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6(_ZNSs7reserveEm + 0x85)[0x7f392b221cd5]
[GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6 _ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RSbIS4_S5_T1_ES4_ + 0x175)[0x7f392b1fa675]
./a.out[0x40120d]
./a.out[0x4010a9]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f392a898b15 ]
./a.out[0x400f29]
=======内存映射:========
00400000-00402000 r-xp 00000000 08:05 3312183238 /a.out
00601000-00602000 r - p 00001000 08:05 3312183238 /a.out
00602000-00603000 rw-p 00002000 0 8:05 3312183238 /a.out
008a6000-008d8000 rw -p 00000000 00:00 0 [堆]
7f3924000000-7f3924021000 rw -p 00000000 00:00 0
7f3924021000- 7f3928000000 --- p 00000000 00:00 0
7f392a877000-7f392aa2d000 r-xp 00000000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392aa2d000-7f392ac2d000 --- p 001b6000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392ac2d000-7f392ac31000 r - p 001b6000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392ac31000-7f392ac33000 rw-p 001ba000 08: 03 201329280 /usr/lib64/libc-2.17.so
7f392ac33000-7f392ac38000 rw-p 00000000 00:00 0
7f392ac38000-7f392ac4e000 r-xp 00000000 08:05 93316545 [GCC_INSTALL_DIR] / generated / lib64 / libgcc_s.so.1
7f392ac4e000-7f392ae4d000 --- p 00016000 08:05 93316545 [GCC_INSTALL_DIR] /generated/lib64/libgcc_s.so.1
7f392ae4d000-7f392ae4e000 r - p 00015000 08:05 93316545 [GCC_INSTALL_DIR] /generated/lib64/libgcc_s.so.1
7f392ae4e000-7f392ae4f000 rw-p 00016000 08:05 93316545 [GCC_INSTALL_DIR] /generated/lib64/libgcc_s.so.1
7f392ae4f000-7f392af50000 r-xp 00000000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392af50000-7f392b14f000 --- p 00101000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392b14f000-7f392b150000 r - p 00100000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392b150000-7f392b151000 rw-p 00101000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392b151000-7f392b2c3000 r-xp 00000000 08:05 93812046 [GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6.0.24
7f392b2c3000-7f392b4c2000 --- p 00172000 08:05 93812046 [GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6.0.24
7f392b4c2000-7f392b4cc000 r - p 00171000 08:05 93812046 [GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6.0.24
7f392b4cc000-7f392b4ce000 rw -p 0017b000 08:05 93812046 [GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6.0.24
7f392b4ce000-7f392b4d2000 rw -p 00000000 00: 00 0
7f392b4d2000-7f392b4f3000 r-xp 00000000 08:03 201329241 /usr/lib64/ld-2.17.so
7f392b6d4000-7f392b6d9000 rw-p 00000000 00:00 0
7f392b6f0000-7f392b6f3000 rw -p 00000000 00:00 0
7f392b6f3000-7f392b6f4000 r - p 00021000 08:03 201329241 /usr/lib64/ld-2.17.so
7f392b6f4000-7f392b6f5000 rw-p 00022000 08:03 201329241 / usr /lib64/ld-2.17.so
7f392b6f5000-7f392b6f6000 rw-p 00000000 00:00 0
7fff7363e000-7fff7365f000 rw -p 00000000 00:00 0 [stack]
7fff7374b000-7fff7374d000 r- xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
放弃(核心转储)
[GCC_INSTALL_DIR ] /生成/ bin / g ++ -c -g -I [GCC_INSTALL_DIR] / generated / include / c ++ / 7.2.0 -std = c ++ 17 -flto -o main.o main.cpp -D_GLIBCXX_USE_CXX11_ABI = 0&& [GCC_INSTALL_DIR] / generated / bin / g ++ -flto main.o
链接库:
ldd ./a.out
linux-vdso.so.1 => (0x00007ffcf9fe6000)
libstdc ++。so.6 => [GCC_INSTALL_DIR] / generated / lib64 / libstdc ++。so.6(0x00007f04a565e000)
libm.so.6 => /lib64/libm.so.6(0x00007f04a5342000)
libgcc_s.so.1 => [GCC_INSTALL_DIR] /generated/lib64/libgcc_s.so.1(0x00007f04a512b000)
libc.so.6 => /lib64/libc.so.6(0x00007f04a4d6a000)
/lib64/ld-linux-x86-64.so.2(0x00007f04a59e00
libc版本:
ldd --version
ldd(GNU libc)配置选项gcc7.2
$ OLD_PWD / gcc-7.2.0 / configure --prefix = $ OLD_PWD / generated --disable-multilib
编译命令行:
[GCC_INSTALL_DIR] / generated / bin / g ++ -c -g -I [GCC_INSTALL_DIR] / generated / include / c ++ / 7.2.0 -std = c ++ 17 -flto -o main.o main.cpp -D_GLIBCXX_USE_CXX11_ABI = 0&& [GCC_INSTALL_DIR] / generated / bin / g ++ -flto main.o
Valgrind输出:
== 28919 == Memcheck,一个内存错误检测器
== 28919 == Copyright(C)2002-2013,and GNU由Julian Seward等人撰写的GPL。
== 28919 ==使用Valgrind-3.10.0和LibVEX;使用-h获取版权信息
== 28919 ==命令:./a.out
== 28919 ==
a
== 28919 ==无效的free()/删除/删除[] / realloc()
== 28919 == at 0x4C2B131:operator delete(void *)(in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
== 28919 == by 0x4F05CD4:_M_dispose(basic_string.h:3155)
== 28919 == by 0x4F05CD4:std :: string :: reserve(unsigned long)(basic_string.tcc:961)
== 28919 == by 0x4EDE674:push_back(basic_string.h:4109)
== 28919 == by 0x4EDE674:operator + =(basic_string.h:3966)
== 28919 == by 0x4EDE674: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>&; )(istream-string.cc:168)
== 28919 == by 0x40120C: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>&) in /home/elie/dev/a.out)
== 28919 == by 0x4010A8:main(在/home/elie/dev/a.out)
== 28919 ==地址0x602200是数据符号中的0字节_ZNSs4_Rep20_S_empty_rep_storageE
== 28919 ==
a
a
== 28919 ==
== 28919 == HEAP SUMMARY:
== 28919 ==在退出时使用:72,704字节在1块
== 28919 ==总堆使用情况:2个分配,2个释放,72,730字节分配
== 28919 ==
== 28919 == LEAK SUMMARY:
== 28919 ==绝对丢失:0字节0字节
== 28919 ==间接丢失:0字节0字节
== 28919 = =可能丢失:0块中的0字节
== 28919 ==仍然可达:1块中72,704字节
== 28919 ==被抑制:0字节0块
== 28919 = =重新运行--leak-check = full查看泄漏内存的详细信息
== 28919 ==
== 28919 == F或计数检测到并抑制错误,重新运行:-v
== 28919 ==错误总结:1错误来自1个上下文(被抑制:1从1)
我们必须使用 _GLIBCXX_USE_CXX11_ABI = 0
,因为我们与旧库进行交互。
删除 flto
, _GLIBCXX_USE_CXX11_ABI = 0
或更改 - std = c ++ 17
for -std = c ++ 14
解决了崩溃问题。
这是gcc-7.2的错误还是我错过了什么?我应该向gcc提交错误报告吗?
我宁愿避免解决禁用 flto
或 c ++ 17
编辑:
在gcc7.2中出现的错误:
解决方案旧版binutils存在一个错误。
binutils-2.28解决了这些问题。
相关gcc bugzilla:
I have a crash with getline in the following code file.I built gcc7.2 because system updates are not available.
Minimal example :
#include <iostream>
int main(int argc, char *argv[])
{
std::string line;
while (std::getline(std::cin, line))
{
}
return 0;
}
On the following lines, GCC_INSTALL_DIR represents the directory where my own gcc is installedOutput :
./a.out
a
*** Error in `./a.out': free(): invalid pointer: 0x0000000000602200 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7cfe1)[0x7f392a8f3fe1]
[GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6(_ZNSs7reserveEm+0x85)[0x7f392b221cd5]
[GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6(_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RSbIS4_S5_T1_ES4_+0x175)[0x7f392b1fa675]
./a.out[0x40120d]
./a.out[0x4010a9]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f392a898b15]
./a.out[0x400f29]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:05 3312183238 [redacted]/a.out
00601000-00602000 r--p 00001000 08:05 3312183238 [redacted]/a.out
00602000-00603000 rw-p 00002000 08:05 3312183238 [redacted]/a.out
008a6000-008d8000 rw-p 00000000 00:00 0 [heap]
7f3924000000-7f3924021000 rw-p 00000000 00:00 0
7f3924021000-7f3928000000 ---p 00000000 00:00 0
7f392a877000-7f392aa2d000 r-xp 00000000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392aa2d000-7f392ac2d000 ---p 001b6000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392ac2d000-7f392ac31000 r--p 001b6000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392ac31000-7f392ac33000 rw-p 001ba000 08:03 201329280 /usr/lib64/libc-2.17.so
7f392ac33000-7f392ac38000 rw-p 00000000 00:00 0
7f392ac38000-7f392ac4e000 r-xp 00000000 08:05 93316545 [GCC_INSTALL_DIR]/generated/lib64/libgcc_s.so.1
7f392ac4e000-7f392ae4d000 ---p 00016000 08:05 93316545 [GCC_INSTALL_DIR]/generated/lib64/libgcc_s.so.1
7f392ae4d000-7f392ae4e000 r--p 00015000 08:05 93316545 [GCC_INSTALL_DIR]/generated/lib64/libgcc_s.so.1
7f392ae4e000-7f392ae4f000 rw-p 00016000 08:05 93316545 [GCC_INSTALL_DIR]/generated/lib64/libgcc_s.so.1
7f392ae4f000-7f392af50000 r-xp 00000000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392af50000-7f392b14f000 ---p 00101000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392b14f000-7f392b150000 r--p 00100000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392b150000-7f392b151000 rw-p 00101000 08:03 201329288 /usr/lib64/libm-2.17.so
7f392b151000-7f392b2c3000 r-xp 00000000 08:05 93812046 [GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6.0.24
7f392b2c3000-7f392b4c2000 ---p 00172000 08:05 93812046 [GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6.0.24
7f392b4c2000-7f392b4cc000 r--p 00171000 08:05 93812046 [GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6.0.24
7f392b4cc000-7f392b4ce000 rw-p 0017b000 08:05 93812046 [GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6.0.24
7f392b4ce000-7f392b4d2000 rw-p 00000000 00:00 0
7f392b4d2000-7f392b4f3000 r-xp 00000000 08:03 201329241 /usr/lib64/ld-2.17.so
7f392b6d4000-7f392b6d9000 rw-p 00000000 00:00 0
7f392b6f0000-7f392b6f3000 rw-p 00000000 00:00 0
7f392b6f3000-7f392b6f4000 r--p 00021000 08:03 201329241 /usr/lib64/ld-2.17.so
7f392b6f4000-7f392b6f5000 rw-p 00022000 08:03 201329241 /usr/lib64/ld-2.17.so
7f392b6f5000-7f392b6f6000 rw-p 00000000 00:00 0
7fff7363e000-7fff7365f000 rw-p 00000000 00:00 0 [stack]
7fff7374b000-7fff7374d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abandon (core dumped)
[GCC_INSTALL_DIR]/generated/bin/g++ -c -g -I[GCC_INSTALL_DIR]/generated/include/c++/7.2.0 -std=c++17 -flto -o main.o main.cpp -D_GLIBCXX_USE_CXX11_ABI=0 && [GCC_INSTALL_DIR]/generated/bin/g++ -flto main.o
Linked library:
ldd ./a.out
linux-vdso.so.1 => (0x00007ffcf9fe6000)
libstdc++.so.6 => [GCC_INSTALL_DIR]/generated/lib64/libstdc++.so.6 (0x00007f04a565e000)
libm.so.6 => /lib64/libm.so.6 (0x00007f04a5342000)
libgcc_s.so.1 => [GCC_INSTALL_DIR]/generated/lib64/libgcc_s.so.1 (0x00007f04a512b000)
libc.so.6 => /lib64/libc.so.6 (0x00007f04a4d6a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f04a59e00
libc version:
ldd --version
ldd (GNU libc) 2.1700)
Configure option for gcc7.2
$OLD_PWD/gcc-7.2.0/configure --prefix=$OLD_PWD/generated --disable-multilib
Compile command line:
[GCC_INSTALL_DIR]/generated/bin/g++ -c -g -I[GCC_INSTALL_DIR]/generated/include/c++/7.2.0 -std=c++17 -flto -o main.o main.cpp -D_GLIBCXX_USE_CXX11_ABI=0 && [GCC_INSTALL_DIR]/generated/bin/g++ -flto main.o
Valgrind output:
==28919== Memcheck, a memory error detector
==28919== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==28919== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==28919== Command: ./a.out
==28919==
a
==28919== Invalid free() / delete / delete[] / realloc()
==28919== at 0x4C2B131: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==28919== by 0x4F05CD4: _M_dispose (basic_string.h:3155)
==28919== by 0x4F05CD4: std::string::reserve(unsigned long) (basic_string.tcc:961)
==28919== by 0x4EDE674: push_back (basic_string.h:4109)
==28919== by 0x4EDE674: operator+= (basic_string.h:3966)
==28919== by 0x4EDE674: 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) (istream-string.cc:168)
==28919== by 0x40120C: 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> >&) (in /home/elie/dev/a.out)
==28919== by 0x4010A8: main (in /home/elie/dev/a.out)
==28919== Address 0x602200 is 0 bytes inside data symbol "_ZNSs4_Rep20_S_empty_rep_storageE"
==28919==
a
a
==28919==
==28919== HEAP SUMMARY:
==28919== in use at exit: 72,704 bytes in 1 blocks
==28919== total heap usage: 2 allocs, 2 frees, 72,730 bytes allocated
==28919==
==28919== LEAK SUMMARY:
==28919== definitely lost: 0 bytes in 0 blocks
==28919== indirectly lost: 0 bytes in 0 blocks
==28919== possibly lost: 0 bytes in 0 blocks
==28919== still reachable: 72,704 bytes in 1 blocks
==28919== suppressed: 0 bytes in 0 blocks
==28919== Rerun with --leak-check=full to see details of leaked memory
==28919==
==28919== For counts of detected and suppressed errors, rerun with: -v
==28919== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)
We have to use _GLIBCXX_USE_CXX11_ABI=0
because we interact with an old library.
Removing flto
, _GLIBCXX_USE_CXX11_ABI=0
or changing -std=c++17
for -std=c++14
resolves the crash.
Is it a bug with gcc-7.2 or did I miss something ? Should I file a bug-report to gcc ?
I would prefer to avoid the workaround of disabling flto
or c++17
Edit :bug present in gcc7.2 : https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82172
解决方案 There is a bug on old version of binutils.
Building with binutils-2.28 resolved those problems.
Relevent gcc bugzilla : https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82172
这篇关于flto与gcc7.2崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!