简体   繁体   English

验证动态字节数组的 CRC 时崩溃 | c

[英]Crash when validating the CRC of a dynamic byte array | c

For an embedded system I am writing code in c to validate a received byte array based on the provided CRC.对于嵌入式系统,我正在 c 中编写代码,以根据提供的 CRC 验证接收到的字节数组。 The system is active in an RTU Modbus.系统在 RTU Modbus 中处于活动状态。

In my unit test I have the following (correct) byte array:在我的单元测试中,我有以下(正确的)字节数组:

unsigned char frame[7] = { 0x01, 0x04, 0x02, 0x03, 0xFF, 0x80, 0xF9 }

The last two bytes are the provided CRC code that I want to validate.最后两个字节是我要验证的提供的 CRC 代码。

My approach is to split the received array into two arrays.我的方法是将接收到的数组拆分为两个 arrays。 The first array being n-2 in length and the second array being 2 in length.第一个数组的长度为 n-2,第二个数组的长度为 2。 Afterwards, create my own CRC code based on the first array, and finally I want to validating if the second array and my own CRC code are the same.然后,根据第一个数组创建我自己的 CRC 码,最后我想验证第二个数组和我自己的 CRC 码是否相同。

This is the code I have right now:这是我现在拥有的代码:

bool validateCrc16ModbusFrame(unsigned char frame[])
{
   // A valid response frame consists of at least 6 bytes.
   size_t size = sizeof frame;  
   if (size < 6) {
       return false;
   }

   // Split the frame into the 'bytes to check' and the 'provided CRC.'
   int newSize = size - 2;
   unsigned char* bytesToCheck = (unsigned char*)_malloca(newSize + 1); // Not sure about this line.
   char providedCrc[2];
   memcpy(bytesToCheck, frame, newSize * sizeof(int));
   memcpy(providedCrc, &frame[newSize], 2 * sizeof(int));

   // Calculate the CRC with the bytes to check.
   uint16_t calculatedCrc = calculateCrc16Modbus(bytesToCheck, newSize); // This function calculates the correct CRC code.
   _freea(bytesToCheck); // Not sure about this line.

   // The CRC is provided as two uint8_t bytes. Convered the two uint8_t to one uint16_t.
   uint8_t firstByteProvidedCrc = providedCrc[0];
   uint8_t secondByteProvidedCrc = providedCrc[1];
   uint16_t uint16ProvidedCrc = ((uint16_t)firstByteProvidedCrc << 8) | secondByteProvidedCrc;

   // Compare the provided CRC and the calculated CRC.
   bool result = uint16ProvidedCrc == calculatedCrc;
   return result;
}

But when I run the test code it crashes with the message '.. This test has probaly CRASHED.!'但是当我运行测试代码时,它会崩溃并显示消息“.. This test has probaly CRASHED.!” When I debug the test code I get an exception with the message 'TestProjectName.exe has triggered a breakpoint.'当我调试测试代码时,我收到一条异常消息“TestProjectName.exe 已触发断点”。 I think the problem arises from creating and/or freeing the memory for the dynamic byte array.我认为问题来自为动态字节数组创建和/或释放 memory。

Anyone know what I'm doing wrong?有人知道我在做什么错吗?

Thanks in advance.提前致谢。

Kind regards, Frenk亲切的问候,弗兰克

The problem is the memcpy calls are multiplying newsize by sizeof(int), when only newsize+1 characters are allocated.问题是当只分配 newsize+1 个字符时,memcpy 调用将 newsize 乘以 sizeof(int)。 They should probably be:他们可能应该是:

   memcpy(bytesToCheck, frame, newSize);       /* no sizeof(int) */
   memcpy(providedCrc, &frame[newSize], 2);    /* no sizeof(int) */

Also you don't need to copy or split the array.此外,您不需要复制或拆分数组。 You can calculate the CRC on the original array including the appended CRC, and the resulting CRC will be zero if the CRC is not post complemented, or some non-zero constant value if the CRC is post complemented.您可以在包含附加 CRC 的原始数组上计算 CRC,如果 CRC 未后补,则生成的 CRC 将为零,或者如果 CRC 后补,则为某个非零常数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM