Skip to content

Commit a7982db

Browse files
committed
byte stuffing bug fix
1 parent b6baa1f commit a7982db

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/dxl_c/protocol.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,14 @@ static DXLLibErrorCode_t parse_dxl1_0_packet(InfoToParseDXLPacket_t* p_parse_pac
493493
static DXLLibErrorCode_t parse_dxl2_0_packet(InfoToParseDXLPacket_t* p_parse_packet, uint8_t recv_data)
494494
{
495495
DXLLibErrorCode_t ret = DXL_LIB_PROCEEDING;
496-
uint16_t byte_stuffing_cnt = 0;
496+
static uint16_t byte_stuffing_cnt = 0; // static variable
497497

498498
switch(p_parse_packet->parse_state)
499499
{
500500
case DXL2_0_PACKET_PARSING_STATE_IDLE:
501501
if(p_parse_packet->header_cnt >= 3){
502502
p_parse_packet->header_cnt = 0;
503+
byte_stuffing_cnt = 0; // static variable initialization
503504
}
504505
p_parse_packet->header[p_parse_packet->header_cnt++] = recv_data;
505506
if(p_parse_packet->header_cnt == 3){
@@ -568,17 +569,11 @@ static DXLLibErrorCode_t parse_dxl2_0_packet(InfoToParseDXLPacket_t* p_parse_pac
568569
if(p_parse_packet->packet_len < 4){ // 4 = Instruction(1)+Error(1)+CRC(2)
569570
ret = DXL_LIB_ERROR_LENGTH;
570571
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_IDLE;
571-
}else if(p_parse_packet->packet_len > p_parse_packet->param_buf_capacity+4){ // 4 = Instruction(1)+Error(1)+CRC(2)
572-
ret = DXL_LIB_ERROR_BUFFER_OVERFLOW;
573-
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_IDLE;
574572
}else{
575573
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_ERROR;
576574
}
577575
}else{
578-
if(p_parse_packet->packet_len > p_parse_packet->param_buf_capacity+3){ // 3 = Instruction(1)+CRC(2)
579-
ret = DXL_LIB_ERROR_BUFFER_OVERFLOW;
580-
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_IDLE;
581-
}else if(p_parse_packet->packet_len == 3){ // 3 = Instruction(1)+CRC(2)
576+
if(p_parse_packet->packet_len == 3){ // 3 = Instruction(1)+CRC(2)
582577
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_CRC_L;
583578
}else{
584579
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_PARAM;
@@ -600,8 +595,13 @@ static DXLLibErrorCode_t parse_dxl2_0_packet(InfoToParseDXLPacket_t* p_parse_pac
600595
if(p_parse_packet->p_param_buf == NULL){
601596
ret = DXL_LIB_ERROR_NULLPTR;
602597
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_IDLE;
598+
break;
599+
}
600+
if(p_parse_packet->param_buf_capacity < p_parse_packet->recv_param_len) {
601+
ret = DXL_LIB_ERROR_BUFFER_OVERFLOW;
602+
p_parse_packet->parse_state = DXL2_0_PACKET_PARSING_STATE_IDLE;
603+
break;
603604
}
604-
605605
p_parse_packet->p_param_buf[p_parse_packet->recv_param_len++] = recv_data;
606606
update_dxl_crc(&p_parse_packet->calculated_crc, recv_data);
607607

0 commit comments

Comments
 (0)