Skip to content

Commit cb62dcb

Browse files
committed
CAN: fix length calculation in message constructor
The two types of the CANMessage constructor accepting a data buffer have two issues. First, they limit the input buffer size to the 4 least significant bits of the passed length even though a CAN message cannot have more than 8 bytes of payload. Second, the used data length in the following memcpy() uses the initially passed data length which may exceed the internal data buffer size. Both will lead into hard to find bugs if the passed data buffer size is outside the limits according to the CAN standard. This fix intends to solve this by limiting the input data size to 8 bytes.
1 parent 3d038e5 commit cb62dcb

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

drivers/CAN.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ class CANMessage : public CAN_Message {
6565
*/
6666
CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
6767
{
68-
len = _len & 0xF;
68+
len = (_len > 8) ? 8 : _len;
6969
type = _type;
7070
format = _format;
7171
id = _id;
72-
memcpy(data, _data, _len);
72+
memcpy(data, _data, len);
7373
}
7474

7575

@@ -83,11 +83,11 @@ class CANMessage : public CAN_Message {
8383
*/
8484
CANMessage(unsigned int _id, const char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
8585
{
86-
len = _len & 0xF;
86+
len = (_len > 8) ? 8 : _len;
8787
type = _type;
8888
format = _format;
8989
id = _id;
90-
memcpy(data, _data, _len);
90+
memcpy(data, _data, len);
9191
}
9292

9393
/** Creates CAN remote message.

0 commit comments

Comments
 (0)