Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am quite new to C++, could anyone help me to fix memory copy issue (invalid pointer of destination):

cut out code looks like this:
C++
int cWrapper::EncryptRun(const unsigned char *data, NewCamDData *cd, int len, bool UseMsgId, comm_type_t commType)
{
	memcpy(desKey , cd->desKey, 16);	
	int sz = sizeof(DES_key_schedule);
	memcpy(&ks1, &cd->ks1, sz);
	memcpy(&ks2, &cd->ks2, sz);

unsigned char netbuf[CWS_NETMSGSIZE];
memset(&netbuf[2],0,cdLen+2);
memcpy(&netbuf[cdLen+4],data,len);
netbuf[cdLen+4+1]=(data[1]&0xf0)|(((len-3)>>8)&0x0f);
netbuf[cdLen+4+2]=(len-3)&0xff;
//...assign values

//this is where the problem is for the data pointer
memcpy(&data,&netbuf,len);
}


data points to an array of 240 bytes, and netbuf is new byte array to be copied to data.

To complete calls, I am adding the method which calls this:

C++
DllExport int CALLBACK EncryptA(NewCamDData *cd, unsigned char *pBuffer, int len, bool UseMsgId, comm_type_t commType)
{
    //cw assigned in class initialisation:
    cw = new cWrapper(525);
    int ret = cw->EncryptRun(pBuffer, cd, len, UseMsgId, commType);
    return ret; //*pBuffer;
}


This method called from VB.NET.

debugging shows me this:
?netbuf
0x0038ebec ""
    [0]: 0 <-first byte of array
    [1]: 48 '0'
    [2]: 169 '©'
?data
0x0280aee4 "€	¤+ØÔ­&§Hœõ­Røî°¶l-áþ«…"
?sizeof(data)
4
?*data
128 '€' <-first byte of array
Posted
Updated 12-Jan-12 7:57am
v4

Use
memcpy(data, netbuf, len);

because data and netbuf are already pointers.
However, this will produce a compiler error, because the destination data is declared as const. Due to the fact that cd is not used, I assume that
memcpy(cd, netbuf, len)

is what you want.

To be sure that no buffer overflows occur, you may also add some checks:
C++
ASSERT(cdLen + 4 + len <= CWS_NETMSGSIZE);
ASSERT(cdLen + 7 <= CWS_NETMSGSIZE);
// This only when copying to cd
ASSERT(len <= sizeof(NewCamDData));
 
Share this answer
 
Comments
Stefan_Lang 12-Jan-12 12:13pm    
Perfect, my 5.
Tomazas77 12-Jan-12 13:26pm    
not, true, cd is used its just strip out.

using first option compilation is not possible with error:
"Error 1 error C2664: 'memcpy' : cannot convert parameter 1 from 'const unsigned char *' to 'void *' c:\PROJECTS\C++\NewCamdLibrary\NewCamdLibrary\CardServer\cWrapper.cpp 88"
Jochen Arndt 12-Jan-12 13:31pm    
That's what I wrote ('this will produce a compiler error'). 'data' is declared as const. When you want to copy to data, you must remove the const specifier from the function declaration and implementation.
Tomazas77 12-Jan-12 13:42pm    
netbuf data has to be copied to data value, not to cd.

CWS_NETMSGSIZE is constant and its value 240.
Jochen Arndt 12-Jan-12 13:50pm    
Yes, I understand. Use my first memcpy example with data as destination, but you must remove the const specifier. When declaring a const pointer (here: 'const unsigned char *data'), you are not allowed to change the content at the memory location pointed to. The first argument to the memcpy function must not be const (see declaration / help for memcpy). Therefore, you must change the first parameter type of your EncryptRun function to 'unsigned char * data'.
C#
memcpy(data,netbuf,len);

i think, this is how you should call. To call like this the data parameter declaration should be "unsigned char *data" should not be a constant pointer!.

Also make sure -
C#
CWS_NETMSGSIZE>=len.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900