A piece of C++ code that uses std::stringstream
fails when I change the runtime libraries to older versions.
I have two environments that I call patched and obsolete
The Patched environment is SLES 11 SP3
- GCC 4.3
- GNU LIBC 2.11
- GNU LIBSTDC++ 4.7.2
The Obsolete environment is SLES 11
- GCC 4.3
- GNU LIBC 2.9
- GNU LIBSTDC++ 4.3.3
When I compile and run the following code in the patched environment it compiles and terminates correctly, however the binary compiled in the patched environment dumps core when it is being run in the obsolete environment.
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
template<class T> inline string toS(const T &t){
stringstream ss;
ss<<t;
return ss.str();}
int main(){
string s = "And the answer is: " + toS(42) ;
cout << s << endl;
return 0;
}
The code is compiled with the following options -Wall -O3 -g
, however the result does not seem to depend on the optimization level.
In the obsolete environment the code fails with
*** glibc detected *** ./a.out: double free or corruption (out): 0x0000000000603440 ***
Analyzing the core file I can extract the following stack trace:
#4 0x00002b9b1f86ac26 in free () from /lib64/libc.so.6
#5 0x00002b9b1f119c66 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow(int) () from /usr/lib64/libstdc++.so.6
#6 0x00002b9b1f11deb8 in std::basic_streambuf<char, std::char_traits<char> >::xsputn(char const*, long) () from /usr/lib64/libstdc++.so.6
#7 0x00002b9b1f103b6d in std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const () from /usr/lib64/libstdc++.so.6
#8 0x00002b9b1f103c87 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const () from /usr/lib64/libstdc++.so.6
#9 0x00002b9b1f1156cc in std::ostream& std::ostream::_M_insert<long>(long) ()
from /usr/lib64/libstdc++.so.6
#10 0x0000000000401877 in toS<int> (t=@0x7fff8bc4d6d8) at strstr.cxx:7
#11 0x0000000000401468 in main () at strstr.cxx:1
This core dump suggests that the code fails while converting the number into string.
I'd like to understand what is going on in order to modify the code so that if compiled in the patched environment it would run in the obsolete.