I foudn that the following program compiles okay with gcc-4.0. But it report "bad_alloc" when executed with common account. However, when I tried to run it with root, it becomes okay again.
// .hh file #ifndef _MC_STATE_H_ #define _MC_STATE_H_ #include <sys/types.h> #include <cstring> #include <cstdlib> #include <string> #include "global.h" using namespace std; class Signature { public: Signature(unsigned char * str, size_t len); Signature(const Signature& nsig); ~Signature(); Signature operator =(Signature osig); string to_string(); private: unsigned char * sig; size_t len; }; class State { public: State(); State(const void*, int len); State(const State&); ~State() throw (); State& operator = (State&); bool operator == (State&); bool operator == (State); bool operator != (State&); Signature get_signature(); State operator + (State&); private: unsigned char * data; size_t sz; bool valid_sig; unsigned char sig[16]; }; #endif // the .cc file #include <sys/types.h> #include <cstring> #include <cstdlib> #include <openssl/md4.h> #include "state.hh" #include <cassert> using namespace std; Signature::Signature(unsigned char* str, size_t vlen) { sig = new unsigned char[len]; len = vlen; memcpy(sig, str, len); } Signature::Signature(const Signature& nsig) : len(nsig.len) { sig = new unsigned char [len]; memcpy(sig, nsig.sig, len); } Signature::~Signature() { delete[] sig; } Signature Signature::operator = (Signature osig) { delete[] sig; len = osig.len; sig = new unsigned char [len]; memcpy(sig, osig.sig, len); return *this; } string Signature::to_string() { char* buf; int i; buf = new char[len * 2+1]; for (i = 0; i < len; i++) sprintf(buf+2*i, "%x", sig[i]); buf[len*2] = 0; string retval = buf; delete [] buf; return retval; } /*****************************************************************************/ State::State() : data(0), sz(0), valid_sig(false) {} State::State(const void * dt, int len) : valid_sig(false), sz(len) { data = new unsigned char [len]; memcpy(data, dt, sz); } State::State(const State& nst) : valid_sig(nst.valid_sig), sz(nst.sz) { data = new unsigned char[sz]; memcpy(data, nst.data, sz); memcpy(sig, nst.sig, sizeof(sig)); } State::~State() throw() { delete [] data; } State& State::operator = (State& nst) { if (data != 0) delete reinterpret_cast<char*>(data); sz = nst.sz; valid_sig = nst.valid_sig; data = new unsigned char[sz]; memcpy(data, nst.data, sz); if (valid_sig) memcpy(sig, nst.sig, sizeof(sig)); return *this; } bool State::operator == (State & nst) { if (sz != nst.sz) return false; return (memcmp(data, nst.data, sz) == 0); } bool State::operator == (State nst) { if (sz != nst.sz) return false; return (memcmp(data, nst.data, sz) == 0); } bool State::operator != (State & nst) { if (sz != nst.sz) return true; return (memcmp(data, nst.data, sz) != 0); } Signature State::get_signature() { if (sz <= 16) return Signature(data,sz); MD4(data, sz, sig); return Signature(sig, sizeof(sig)); } State State::operator + (State& nst) { State retval; retval.data = new unsigned char [this->sz + nst.sz]; retval.sz = this->sz + nst.sz; retval.valid_sig = false; memcpy(retval.data, data, sz); memcpy(retval.data + sz, nst.data, nst.sz); assert(retval.sz == this->sz + nst.sz); return retval; } #if 1 #include <iostream> using namespace std; int main() { char data[] = "fdfdsafdsafhasdkhfksladhfklsdahfklsdahflksdahfdsahkfkasd"; State st(data, sizeof(data)); cout<<st.get_signature().to_string()<<endl; char data2[] = "abcd"; State st2(data2, sizeof(data2)); cout<<st2.get_signature().to_string()<<endl; State st3 = st + st2; cout<<st3.get_signature().to_string() <<endl; } #endif -- Summary: wierd behavior Product: gcc Version: 4.0.0 Status: UNCONFIRMED Severity: critical Priority: P1 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: gnu04 at yahoo dot com CC: gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189