@@ -341,8 +341,6 @@ PacketShared::STATUS PacketCommand::lookupCommandByName(const char* name){
341
341
DEBUG_PORT.println (i);
342
342
DEBUG_PORT.print (F (" #\t &_commandList[i]=" ));
343
343
DEBUG_PORT.println ((int ) &_commandList[i], HEX);
344
- DEBUG_PORT.print (F (" #\t type_id[" ));DEBUG_PORT.print (_input_index);DEBUG_PORT.print (F (" ]=" ));
345
- DEBUG_PORT.println (_commandList[i].type_id [_input_index]);
346
344
DEBUG_PORT.print (F (" #\t name=" ));
347
345
DEBUG_PORT.println (_commandList[i].name );
348
346
#endif
@@ -417,28 +415,38 @@ PacketShared::STATUS PacketCommand::set_recvTimestamp(uint32_t timestamp_micros)
417
415
PacketShared::STATUS PacketCommand::matchCommand (){
418
416
byte cur_byte = 0x00 ;
419
417
_current_command = _default_command;
418
+ int type_id_index = 0 ;
420
419
// parse out type_id from header
421
420
#ifdef PACKETCOMMAND_DEBUG
422
421
DEBUG_PORT.println (F (" # In PacketCommand::matchCommand" ));
423
422
DEBUG_PORT.print (F (" #\t _input_index=" ));DEBUG_PORT.println (_input_index);
424
423
DEBUG_PORT.print (F (" #\t _input_len=" ));DEBUG_PORT.println (_input_len);
425
424
#endif
425
+ if (_input_index >= _input_len){
426
+ #ifdef PACKETCOMMAND_DEBUG
427
+ DEBUG_PORT.println (F (" ### Error: invalid 'type ID' detected, no packet data left" ));
428
+ #endif
429
+ return PacketShared::ERROR_INVALID_TYPE_ID;
430
+ }
426
431
while (_input_index < _input_len){
427
432
cur_byte = _input_buffer[_input_index];
428
433
#ifdef PACKETCOMMAND_DEBUG
429
434
DEBUG_PORT.print (F (" #\t cur_byte=" ));DEBUG_PORT.println (cur_byte,HEX);
435
+ DEBUG_PORT.print (F (" #\t _type_id_index=" ));DEBUG_PORT.println (type_id_index);
430
436
#endif
431
437
if (cur_byte != 0xFF and cur_byte != 0x00 ){ // valid type ID completed
432
438
#ifdef PACKETCOMMAND_DEBUG
433
439
DEBUG_PORT.println (F (" #\t valid 'type ID' format detected" ));
434
440
#endif
435
- _current_command.type_id [_input_index] = cur_byte;
441
+ _current_command.type_id [type_id_index] = cur_byte;
442
+ // CAUTION do not increment type_id_index or _input_index here!
436
443
break ;
437
444
}
438
445
else if (cur_byte == 0xFF ){ // extended type ID, need to check the next byte
439
- _current_command.type_id [_input_index] = 0xFF ;
446
+ _current_command.type_id [type_id_index] = 0xFF ;
447
+ type_id_index++;
440
448
_input_index++;
441
- if (_input_index >= MAX_TYPE_ID_LEN){
449
+ if (type_id_index >= MAX_TYPE_ID_LEN){
442
450
#ifdef PACKETCOMMAND_DEBUG
443
451
DEBUG_PORT.println (F (" ### Error: invalid 'type ID' detected, exceeded maximum length" ));
444
452
#endif
@@ -458,7 +466,7 @@ PacketShared::STATUS PacketCommand::matchCommand(){
458
466
return PacketShared::ERROR_INVALID_TYPE_ID;
459
467
}
460
468
}
461
- // For a valid type ID 'cur_byte' will be euqal to its last byte and all previous
469
+ // For a valid type ID 'cur_byte' will be equal to its last byte and all previous
462
470
// bytes, if they exist, must have been 0xFF (or nothing), so we only need to check the
463
471
// corresponding byte for a match in the registered command list. Also,
464
472
// since pkt_index must be < MAX_TYPE_ID_LEN at this point, it should be within
@@ -468,10 +476,10 @@ PacketShared::STATUS PacketCommand::matchCommand(){
468
476
#ifdef PACKETCOMMAND_DEBUG
469
477
DEBUG_PORT.print (F (" # Searching command at index=" ));
470
478
DEBUG_PORT.println (i);
471
- DEBUG_PORT.print (F (" #\t type_id[" ));DEBUG_PORT.print (_input_index );DEBUG_PORT.print (F (" ]=" ));
472
- DEBUG_PORT.println (_commandList[i].type_id [_input_index ]);
479
+ DEBUG_PORT.print (F (" #\t type_id[" ));DEBUG_PORT.print (type_id_index );DEBUG_PORT.print (F (" ]=" ));
480
+ DEBUG_PORT.println (_commandList[i].type_id [type_id_index ]);
473
481
#endif
474
- if (_commandList[i].type_id [_input_index ] == cur_byte){
482
+ if (_commandList[i].type_id [type_id_index ] == cur_byte){
475
483
// a match has been found, so save it and stop
476
484
#ifdef PACKETCOMMAND_DEBUG
477
485
DEBUG_PORT.println (F (" #match found" ));
0 commit comments