typedef unsigned char uint8;
typedef struct dat
{
MSG_HDR stHdr;
uint8 data1[128];
uint8 data2[128];
uint8 data3[128];
} DATA;
int func(CString String1, CString String2, CString String3)
{
DATA *Cfg;
//Memory allocation done and checked for NULL
// Scenario 1:
memcpy(Cfg->data1, String1.GetBuffer(String1.GetLength()), sizeof(Cfg->data1));
memcpy(Cfg->data2, String2.GetBuffer(String2.GetLength()), sizeof(Cfg->data2));
memcpy(Cfg->data3, String3.GetBuffer(String3.GetLength()), sizeof(Cfg->data3));
// Scenario 2:
memcpy (Cfg->data1, String1.GetBuffer(String1.GetLength()), String1.GetLength());
memcpy (Cfg->data2, String2.GetBuffer(String2.GetLength()), String2.GetLength());
memcpy (Cfg->data3, String3.GetBuffer(String3.GetLength()), String3.GetLength());
Cfg->data1[String1.GetLength()+1] = '\0';
Cfg->data2[String2.GetLength()+1] = '\0';
Cfg->data3[String3.GetLength()+1] = '\0';
}
I am getting "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
There is a structure with uint8 variables which is essentially char. There is a function which is being called to which 3 CString Variables are passed. The structure pointer is created and memory allocated and checked for.
Then memcpy is performed. The scenario 1 is the actual code that existed. Since the number of bytes to copy is 128, which is more than the data to be copied, this is supposed to cause trouble and changed to scenario 2. Here the CString
GetLength()
is used to get the data length. Since it doesn't include Null terminated string, Null termination is added as a separate step.
When i run these two separately as a program, I don't get any error. Whereas when run in project, I get the error at occasions. If this installer is the last one to be installed in the system, this error is not be seen. If some other components are installed, then this error props up.
I would like to understand if there is anything wrong in this piece of code which could cause this error? This is the part of code which we are suspecting could result in this error. Kindly help
Edit 1 : Error trace is as follows
Init Step 1: Error - Failed to Configure the Network. eXX.API.E_FAIL_Exception: (HRESULT:0x80004005) eXX func failed ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at AALib.AAPIClass.func(String string1, String string2, String string3) at eXX.API.eXXN.func(String string1, String string2, String string3) --- End of inner exception stack trace --- at eXX.API.eXXN.func(String string1, String string2, String string3) at SS.U.N.func.Commit() --- End of inner exception stack trace --- at SS.U.N.func.Commit() at BaseConfigurationStep.Commit() at SS.U.N.N.Commit() at SS.U.U.Commit() at SS.SS.Commit() at DLC.CCompositeStep.Execute()
My application exposed func through AALib to eXX (which is in C#), which again encapsulates it in eXX.API.eXXN.func, which is called by DLC 9written in c#) So the sequence is DLC(App1) -> eXX -> AALib (Which is the exposed API from my application)
User contributions licensed under CC BY-SA 3.0