Segmentation fault with structs

Hello,

I have a program with some structs peerinfo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct peerinfo:obj {

  //uchar peerid[HASHLEN];  

  struct peerid stpeerid;
  struct unhashedid stunhashed;
  struct uptime stuptime;
  struct addressinfo staddr;
  struct resourcelist streslist;
  struct x509 stx509;

public:
  peerinfo();
  //peerinfo(const peerinfo& lstpeer);
  bool operator < (peerinfo& rhs) {
    BigInt rhs_bigint, my_bigint;
    uchar_to_bigint((uchar*)stpeerid.id, idlen, my_bigint);
    uchar_to_bigint((uchar*)rhs.stpeerid.id, idlen, rhs_bigint);
    return my_bigint < rhs_bigint;
  }
 
  bool operator == (peerinfo rhs) {
    BigInt rhs_bigint, my_bigint;
    uchar_to_bigint((uchar*)stpeerid.id, idlen, my_bigint);
    uchar_to_bigint((uchar*)rhs.stpeerid.id, idlen, rhs_bigint);
    return (my_bigint == rhs_bigint);
  }
  int parse(uchar* buf, int size, int& cur);
  int encode(uchar* buf, int size, int& cur);
  void Initialize();

  //peerinfo& operator=(const peerinfo& lstpeer);

};


If I try to assign a struct to a variable like:

1
2
3
4
struct peerinfo m_SUPER_INFO;

uint32 HPeer::getNextHop(uchar* peerid, uint32 len, struct peerinfo& stP) {
   stP = m_SUPER_INFO;


I have a segmentation fault, but it works if I do:

1
2
3
4
struct peerinfo m_SUPER_INFO;

uint32 HPeer::getNextHop(uchar* peerid, uint32 len, struct peerinfo& stP) {
    memcpy((void *) & stP, (void *) & m_SUPER_INFO, sizeof ( m_SUPER_INFO));


But later I need call another method:

1
2
       
m_pPeer->ComposeLookupObjectResp(respCode, stPSelf, stP, pchIP, port, tt, objLO);


And I have a segmentation fault in the call (the program pass for the line begin this call but don't pass for the first line in ComposeLookupObjectResp method.

I think that I have a problem in the generation of the first m_SUPER_INFO because all the rest works if I the program create the struct in other side.

I initialize like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        //PeerInfo
        memset(&m_SUPER_INFO, 0, sizeof(m_SUPER_INFO));
        m_SUPER_INFO.Initialize();
        m_SUPER_INFO.stobj.objtype = ePEERINFO;
        m_SUPER_INFO.isPresent = 1;


        //Peer-Id   
        m_SUPER_INFO.stpeerid.stobj.length = idlen;
        m_SUPER_INFO.stpeerid.isPresent = 1;
        memset(m_SUPER_INFO.stpeerid.id, 0, idlen);

        strncpy(m_SUPER_INFO.staddr.addr[0], m_SUPER_IP, 16);
        m_SUPER_INFO.staddr.port[0] = m_SUPER_PORT;
        m_SUPER_INFO.staddr.tt[0] = 0;
        m_SUPER_INFO.staddr.ht[0] = 0;
        m_SUPER_INFO.staddr.ipver[0] = 4;
        m_SUPER_INFO.staddr.R[0] = 0;
        m_SUPER_INFO.staddr.foundation[0] = 0;
        m_SUPER_INFO.staddr.compid[0] = 0;
        m_SUPER_INFO.staddr.priority[0] = 0;
        m_SUPER_INFO.staddr.num = 1;
        m_SUPER_INFO.staddr.isPresent = 1;

        m_SUPER_INFO.stobj.length = 43; //CORREGIR


If I comment the memset line I have a "St9bad_alloc" instead of a segmentation fault.

do you have son idea of what can stay happen??

Thanks a lot.
Last edited on
Isn't X509 an OpenSSL data structure that contains pointers to things and must be manipulated by related functions? Are the other structures of this ilk?

If this is the case, you can't just copy them using bitwise copies.
Topic archived. No new replies allowed.