This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: ofstream crash with libstdc++
OK. I am able to reproduce the crash. Consider the following code:
#include <fstream>
#include <string>
using namespace std;
int main()
{
?? string filename = "test.txt";
?? ofstream fout (filename.c_str(), fstream::out);
?? if (fout.is_open())
????? fout << "hello\n";
?? return 0;
}
Now, please follow the commands and outputs:
[mahmood@localhost ~]$ /opt/gcc-4.1.2-built/bin/g++ -o test test.cpp
[mahmood@localhost ~]$ ./test
*** glibc detected *** ./test: double free or corruption (out): 0x00007fffeda600f0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x37d6875916]
/lib64/libc.so.6[0x37d6878443]
/usr/lib64/libstdc++.so.6(_ZNSsD1Ev+0x39)[0x37e249d4a9]
./test[0x400a21]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x37d681ecdd]
./test[0x4008a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:02 154387???????????????????????????? /home/mahmood/test
00600000-00601000 rw-p 00000000 08:02 154387???????????????????????????? /home/mahmood/test
016b6000-016d7000 rw-p 00000000 00:00 0????????????????????????????????? [heap]
37d6000000-37d6020000 r-xp 00000000 08:02 78729????????????????????????? /lib64/ld-2.12.so
37d621f000-37d6220000 r--p 0001f000 08:02 78729????????????????????????? /lib64/ld-2.12.so
37d6220000-37d6221000 rw-p 00020000 08:02 78729????????????????????????? /lib64/ld-2.12.so
37d6221000-37d6222000 rw-p 00000000 00:00 0
37d6800000-37d6989000 r-xp 00000000 08:02 78730????????????????????????? /lib64/libc-2.12.so
37d6989000-37d6b89000 ---p 00189000 08:02 78730????????????????????????? /lib64/libc-2.12.so
37d6b89000-37d6b8d000 r--p 00189000 08:02 78730????????????????????????? /lib64/libc-2.12.so
37d6b8d000-37d6b8e000 rw-p 0018d000 08:02 78730????????????????????????? /lib64/libc-2.12.so
37d6b8e000-37d6b93000 rw-p 00000000 00:00 0
37d7000000-37d7083000 r-xp 00000000 08:02 78732????????????????????????? /lib64/libm-2.12.so
37d7083000-37d7282000 ---p 00083000 08:02 78732????????????????????????? /lib64/libm-2.12.so
37d7282000-37d7283000 r--p 00082000 08:02 78732????????????????????????? /lib64/libm-2.12.so
37d7283000-37d7284000 rw-p 00083000 08:02 78732????????????????????????? /lib64/libm-2.12.so
37e1800000-37e1816000 r-xp 00000000 08:02 31151????????????????????????? /lib64/libgcc_s-4.4.6-20120305.so.1
37e1816000-37e1a15000 ---p 00016000 08:02 31151????????????????????????? /lib64/libgcc_s-4.4.6-20120305.so.1
37e1a15000-37e1a16000 rw-p 00015000 08:02 31151????????????????????????? /lib64/libgcc_s-4.4.6-20120305.so.1
37e2400000-37e24e8000 r-xp 00000000 08:02 78773????????????????????????? /usr/lib64/libstdc++.so.6.0.13
37e24e8000-37e26e8000 ---p 000e8000 08:02 78773????????????????????????? /usr/lib64/libstdc++.so.6.0.13
37e26e8000-37e26ef000 r--p 000e8000 08:02 78773????????????????????????? /usr/lib64/libstdc++.so.6.0.13
37e26ef000-37e26f1000 rw-p 000ef000 08:02 78773????????????????????????? /usr/lib64/libstdc++.so.6.0.13
37e26f1000-37e2706000 rw-p 00000000 00:00 0
7f4ac4561000-7f4ac4566000 rw-p 00000000 00:00 0
7f4ac4574000-7f4ac4576000 rw-p 00000000 00:00 0
7fffeda4d000-7fffeda62000 rw-p 00000000 00:00 0????????????????????????? [stack]
7fffedb89000-7fffedb8a000 r-xp 00000000 00:00 0????????????????????????? [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0????????????????? [vsyscall]
Aborted
Is there any idea about that?
?
Regards,
Mahmood
----- Original Message -----
From: Mahmood Naderan <nt_mahmood@yahoo.com>
To: Yann Droneaud <yann@droneaud.fr>
Cc: "libc-help@sourceware.org" <libc-help@sourceware.org>; Siddhesh Poyarekar <siddhesh.poyarekar@gmail.com>
Sent: Tuesday, February 19, 2013 3:09 PM
Subject: Re: ofstream crash with libstdc++
> It might be useful to enable exceptions[1]
> on the stream and see if the open actually failed.
?
> Why not checking if fout.open() return NULL ?
I tried these methods:
1)
??? std::string f="filter";
??? std::fstream fout;
??? fout.open(f.c_str(), std::fstream::app | std::fstream::out);
??? if (fout.is_open()) {
????? fout << "--------------------messages format--------------------------\n";? // STILL HAVE ERROR
??? }
2)
??? std::string f="filter";
??? fout.exceptions ( std::ofstream::failbit | std::ofstream::badbit );
??? try {
????? fout.open (f.c_str(), std::fstream::app | std::fstream::out);
????? fout << "--------------------messages format--------------------------\n";? // STILL HAVE ERROR
??? }
??? catch (std::ofstream::failure e) {
?? ? ? std::cout << "Exception opening/reading file";
??? }
In both cases the segmentation fault occurs at << operator.
>It's easier if you give code that actually demonstrates the crash than
>a snippet of code within some larger code.? At this point it's easier
>to assume that your code is at fault somehow.
The OS is scientific linux 6.3. To be honest, I run my code (without any change) on another scientific linux 6.0 and there is no problem. Also the source file is huge with other dependent libraries.
In my system, gcc/g++ 4.4 are installed by default. I manually compiled gcc/g++4.1 and this code has been compiled with gcc/g++4.1. On another system (that is SL6.0 and the code works perfectly), only gcc/g++4.1 is installed and again the code has been compiled with gcc/g++4.1
I think there is a conflict between the what is compiled and the shared libraries at runtime. From the backtrace, I think it is saying that the functionality of '<<' that has been compiled is now different from what is read from a shared library. Not sure about that however....
Regards,
Mahmood
----- Original Message -----
From: Yann Droneaud <yann@droneaud.fr>
To: Mahmood Naderan <nt_mahmood@yahoo.com>
Cc: "libc-help@sourceware.org" <libc-help@sourceware.org>
Sent: Tuesday, February 19, 2013 2:51 PM
Subject: Re: ofstream crash with libstdc++
Hi,
> ??? .....
> ???? std::string f;
> ???? std::fstream
> fout;
> ???? f = "filter";
> ???? fout.open(f.c_str(), std::fstream::app | std::fstream::out);
Why not checking if fout.open() return NULL ?
Regards.
--
Yann Droneaud