diff --git a/libraries/AP_Common/AP_Param.cpp b/libraries/AP_Common/AP_Param.cpp index 3beb585ee3..95e81a076a 100644 --- a/libraries/AP_Common/AP_Param.cpp +++ b/libraries/AP_Common/AP_Param.cpp @@ -153,6 +153,19 @@ bool AP_Param::check_group_info(const struct AP_Param::GroupInfo *group_info, return true; } +// check for duplicate key values +bool AP_Param::duplicate_key(uint8_t vindex, uint8_t key) +{ + for (uint8_t i=vindex+1; i<_num_vars; i++) { + uint8_t key2 = PGM_UINT8(&_var_info[i].key); + if (key2 == key) { + // no duplicate keys allowed + return true; + } + } + return false; +} + // validate the _var_info[] table bool AP_Param::check_var_info(void) { @@ -160,6 +173,7 @@ bool AP_Param::check_var_info(void) for (uint8_t i=0; i<_num_vars; i++) { uint8_t type = PGM_UINT8(&_var_info[i].type); + uint8_t key = PGM_UINT8(&_var_info[i].key); if (type == AP_PARAM_GROUP) { if (i == 0) { // first element can't be a group, for first() call @@ -179,6 +193,9 @@ bool AP_Param::check_var_info(void) } total_size += size + sizeof(struct Param_header); } + if (duplicate_key(i, key)) { + return false; + } } if (total_size > _eeprom_size) { serialDebug("total_size %u exceeds _eeprom_size %u", diff --git a/libraries/AP_Common/AP_Param.h b/libraries/AP_Common/AP_Param.h index 3fa0ca1196..596b07c29e 100644 --- a/libraries/AP_Common/AP_Param.h +++ b/libraries/AP_Common/AP_Param.h @@ -189,6 +189,7 @@ private: static const uint8_t _sentinal_group = 0xFF; static bool check_group_info(const struct GroupInfo *group_info, uint16_t *total_size, uint8_t max_bits); + static bool duplicate_key(uint8_t vindex, uint8_t key); static bool check_var_info(void); const struct Info *find_var_info_group(const struct GroupInfo *group_info, uint8_t vindex,