pixartist
pixartist

Reputation: 1155

Legacy code seems to have an overflow, I'm not sure though

I'm working at some legacy code right now (converting some of it to C#), and I've stumbled upon a problem:

A byte array is created (length is ulcLen):

CSLAutoArray<BYTE> pMem(new BYTE[ulcLen]);

Now some stuff is put into the byte array, after which a CRC / Hash value is supposed to be written to the first four bytes (ULONG / UInt32):

__CfgCRC(pMem + sizeof(ULONG), ulcLen - sizeof(ULONG))

->

inline ULONG __CfgCRC(const void* const cpcMem, const ULONG ulcMemSize)
{
  ULONG ulRes = 0;

  const BYTE* const cpcUseMem = reinterpret_cast<const BYTE*>(cpcMem);
  for(const BYTE* pcLook = cpcUseMem; cpcUseMem + ulcMemSize > pcLook; pcLook++)
  {
    ulRes ^= static_cast<ULONG>(*pcLook);
    //[...]
  };

  return ulRes;
};

Now, is it just me, or is the static_cast reading 1/2/3 bytes over the end of the byte array, at the end of the for loop? Since pcLook (the memory pointer) is increased until it reaches the full length of the data, (ulclen + sizeof(ULONG)) ? Or am I wrong? Or does static_cast somehow not read over the end of an array ? (CSLAutoArray is some kind of managed pointer class, but as far as I see it does not interfere with this code)

Upvotes: 0

Views: 48

Answers (1)

mark
mark

Reputation: 5469

*pcLook is just a BYTE so no, it's only reading 1 octet at a time. the cast just casts the BYTE and not what pcLock is pointing to.

Upvotes: 3

Related Questions