ThomasBarth / ESP32-CAN-Driver

CAN driver project for the Espressif ESP32
284 stars 78 forks source link

Stack mash problem #26

Open ttlappalainen opened 4 years ago

ttlappalainen commented 4 years ago

Hi,

I have your original ESP CAN on my NMEA2000_CAN unit. I found that e.g. on reading frame from can controller, stack may be mashed due to writing over buffer index.

In some cases, when you read FIR info from controller, it may be corrupted so that length is >8. That causes later buffer to be written over index and stack crash. So in every place you point to buffer, max index should be set to max. 8. e.g. if ( __frame.FIR.B.DLC>8 ) __frame.FIR.B.DLC=8; Naturally, if length is >8, frame is possibly totally corrupted anyway and the best would be not to queue it at all.

//get FIR
**__frame.FIR.U=MODULE_CAN->MBX_CTRL.FCTRL.FIR.U;**

//check if this is a standard or extended CAN frame
//standard frame
if(__frame.FIR.B.FF==CAN_frame_std){

    //Get Message ID
    __frame.MsgID = _CAN_GET_STD_ID;

    //deep copy data bytes
    for(__byte_i=0;__byte_i<**__frame.FIR.B.DLC**;__byte_i++)
        __frame.data.u8[__byte_i]=MODULE_CAN->MBX_CTRL.FCTRL.TX_RX.STD.data[__byte_i];

}