Menu

#44 MonoControlUsbSetup does not correctly copy data

v2.2.8
open
nobody
None
5
2014-06-19
2014-06-19
Alex Earl
No

In MonoControlUsbSetup.SetData, if the data parameter is already an IntPtr, the code will copy the data of the IntPtr instead of the buffer that the IntPtr points to. An updated method like below could be used:

public void SetData(object data, int offset, int length)
{
byte[] temp;
if (data is byte[]) {
temp = data as byte[];
} else if(data is IntPtr) {
temp = new byte[length];
Marshal.Copy((IntPtr)data, temp, offset, length);
} else {
PinnedHandle p = new PinnedHandle(data);
temp = new byte[length];
Marshal.Copy(p.Handle, temp, offset, length);
p.Dispose();
}
Marshal.Copy(temp, 0, PtrData, length);
}

Discussion

  • Alex Earl

    Alex Earl - 2014-06-19

    The code did not get posted well, this might look better:

    public void SetData(object data, int offset, int length)
    {
        byte[] temp;
        if (data is byte[]) {
            temp = data as byte[];
        } else if(data is IntPtr) {
            temp = new byte[length];
            Marshal.Copy((IntPtr)data, temp, offset, length);
        } else {
            PinnedHandle p = new PinnedHandle(data);
            temp = new byte[length];
            Marshal.Copy(p.Handle, temp, offset, length);
            p.Dispose();
        }
        Marshal.Copy(temp, 0, PtrData, length);
    }
    
     
  • Alex Earl

    Alex Earl - 2014-06-19

    Looks like this is fixed in the latest trunk.

     

Log in to post a comment.