I was recently writing code for a custom serial communication protocol. What I did was, I used a part(8/16 bit) of the receiving data to denote how big the frame size is. Based on this data I expect that no of data to follow. I use Crc to accept or reject a frame. But I won't be able to include the frame length data in the Crc, since on the receiving end I should know how much data to expect, before processing a frame.
The issue that I faced is, occasionally this frame length data gets corrupted and it fools the receiver into receiving that many bytes, whereas the receiving array size is much lesser than that. This corrupts a lot of critical system variables that is present in the consecutive memory locations.
How do I prevent the buffer overflow from happening? My thoughts on this 1) Reject the framelength data if it goes beyond a certain value. 2) use a datatype that limits the max no. Like using a short which limits the scope of the array index to 256 memory locations, and create a buffer with 280 bytes. 3) allocate memory in a separate location, so that it doesn't affect the critical system variables.
One thing I used to prevent getting stuck in the receiving loop is by using a timeout. But I overlooked this aspect of the issue. It look me lot if time to confirm and reproduce the issue, since thus code is part of a larger system code, and I'm not an expert here.
Generally how to safely handle this type of issues?
Also: what are general considerations or standard practices to follow when using an array, to prevent it from overflowing?