public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/21189] New: wierd behavior
@ 2005-04-24 14:30 gnu04 at yahoo dot com
  2005-04-24 14:31 ` [Bug c++/21189] " gnu04 at yahoo dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: gnu04 at yahoo dot com @ 2005-04-24 14:30 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/21189] wierd behavior
  2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
@ 2005-04-24 14:31 ` gnu04 at yahoo dot com
  2005-04-24 14:32 ` gnu04 at yahoo dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: gnu04 at yahoo dot com @ 2005-04-24 14:31 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gnu04 at yahoo dot com  2005-04-24 14:31 -------
Created an attachment (id=8725)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=8725&action=view)
The .cc file


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/21189] wierd behavior
  2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
  2005-04-24 14:31 ` [Bug c++/21189] " gnu04 at yahoo dot com
@ 2005-04-24 14:32 ` gnu04 at yahoo dot com
  2005-04-24 14:34 ` [Bug c++/21189] weird behavior pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: gnu04 at yahoo dot com @ 2005-04-24 14:32 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gnu04 at yahoo dot com  2005-04-24 14:32 -------
Created an attachment (id=8726)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=8726&action=view)
the .hh file


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/21189] wierd behavior
  2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
                   ` (2 preceding siblings ...)
  2005-04-24 14:34 ` [Bug c++/21189] weird behavior pinskia at gcc dot gnu dot org
@ 2005-04-24 14:34 ` pinskia at gcc dot gnu dot org
  2005-04-24 14:46 ` [Bug c++/21189] weird behavior pinskia at gcc dot gnu dot org
  2005-04-25 16:28 ` gnu04 at yahoo dot com
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-04-24 14:34 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-04-24 14:34 -------
If it is throwing bad_alloc, that means it is running out of memory.  I would check you memory limits?

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|wierd behavior              |wierd behavior


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/21189] weird behavior
  2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
  2005-04-24 14:31 ` [Bug c++/21189] " gnu04 at yahoo dot com
  2005-04-24 14:32 ` gnu04 at yahoo dot com
@ 2005-04-24 14:34 ` pinskia at gcc dot gnu dot org
  2005-04-24 14:34 ` [Bug c++/21189] wierd behavior pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-04-24 14:34 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|wierd behavior              |weird behavior


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/21189] weird behavior
  2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
                   ` (3 preceding siblings ...)
  2005-04-24 14:34 ` [Bug c++/21189] wierd behavior pinskia at gcc dot gnu dot org
@ 2005-04-24 14:46 ` pinskia at gcc dot gnu dot org
  2005-04-25 16:28 ` gnu04 at yahoo dot com
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-04-24 14:46 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-04-24 14:46 -------
Actually this is a bug in your code:
Signature::Signature(unsigned char* str, size_t vlen)
{
  sig = new unsigned char[len];
  len = vlen;
...

len is not assigned so you are using an uninitialized variable.  Switching the order of the above 
statements around fixes the problem.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/21189] weird behavior
  2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
                   ` (4 preceding siblings ...)
  2005-04-24 14:46 ` [Bug c++/21189] weird behavior pinskia at gcc dot gnu dot org
@ 2005-04-25 16:28 ` gnu04 at yahoo dot com
  5 siblings, 0 replies; 7+ messages in thread
From: gnu04 at yahoo dot com @ 2005-04-25 16:28 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gnu04 at yahoo dot com  2005-04-25 16:27 -------
Subject: Re:  weird behavior

Thanks a lot! And sorry for the spamming. I should be
careful.

Andy

--- pinskia at gcc dot gnu dot org
<gcc-bugzilla@gcc.gnu.org> wrote:
> 
> ------- Additional Comments From pinskia at gcc dot
> gnu dot org  2005-04-24 14:46 -------
> Actually this is a bug in your code:
> Signature::Signature(unsigned char* str, size_t
> vlen)
> {
>   sig = new unsigned char[len];
>   len = vlen;
> ...
> 
> len is not assigned so you are using an
> uninitialized variable.  Switching the order of the
> above 
> statements around fixes the problem.
> 
> -- 
>            What    |Removed                    
> |Added
>
----------------------------------------------------------------------------
>              Status|UNCONFIRMED                
> |RESOLVED
>          Resolution|                           
> |INVALID
> 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189
> 
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
> 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21189


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-04-25 16:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-24 14:30 [Bug c++/21189] New: wierd behavior gnu04 at yahoo dot com
2005-04-24 14:31 ` [Bug c++/21189] " gnu04 at yahoo dot com
2005-04-24 14:32 ` gnu04 at yahoo dot com
2005-04-24 14:34 ` [Bug c++/21189] weird behavior pinskia at gcc dot gnu dot org
2005-04-24 14:34 ` [Bug c++/21189] wierd behavior pinskia at gcc dot gnu dot org
2005-04-24 14:46 ` [Bug c++/21189] weird behavior pinskia at gcc dot gnu dot org
2005-04-25 16:28 ` gnu04 at yahoo dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).