AP_Logger: add message validation check against duplicate labels

This commit is contained in:
Peter Barker 2020-04-07 15:16:51 +10:00 committed by Peter Barker
parent 04b0a8a62a
commit acba2a78f6
2 changed files with 43 additions and 0 deletions

View File

@ -298,6 +298,44 @@ void AP_Logger::dump_structures(const struct LogStructure *logstructures, const
#endif
}
bool AP_Logger::labels_string_is_good(const char *labels) const
{
// This goes through and slices labels up into substrings by
// changing commas to nulls - keeping references to each string in
// label_offsets.
bool passed = true;
char *label_offsets[LS_LABELS_SIZE];
uint8_t label_offsets_offset = 0;
char labels_copy[LS_LABELS_SIZE];
strncpy(labels_copy, labels, ARRAY_SIZE(labels_copy));
if (labels_copy[0] == ',') {
Debug("Leading comma in (%s)", labels);
passed = false;
}
label_offsets[label_offsets_offset++] = labels_copy;
const uint8_t len = strnlen(labels_copy, ARRAY_SIZE(labels_copy));
for (uint8_t i=0; i<len; i++) {
if (labels_copy[i] == ',') {
if (labels_copy[i+1] == '\0') {
Debug("Trailing comma in (%s)", labels);
passed = false;
continue;
}
labels_copy[i] = '\0';
label_offsets[label_offsets_offset++] = &labels_copy[i+1];
}
}
for (uint8_t i=0; i<label_offsets_offset-1; i++) {
for (uint8_t j=i+1; j<label_offsets_offset; j++) {
if (!strcmp(label_offsets[i], label_offsets[j])) {
Debug("Duplicate label (%s) in (%s)", label_offsets[i], labels);
passed = false;
}
}
}
return passed;
}
bool AP_Logger::validate_structure(const struct LogStructure *logstructure, const int16_t offset)
{
bool passed = true;
@ -337,6 +375,10 @@ bool AP_Logger::validate_structure(const struct LogStructure *logstructure, cons
passed = false;
}
if (!labels_string_is_good(logstructure->labels)) {
passed = false;
}
// check that the structure is of an appropriate length to take fields
const int16_t msg_len = Write_calc_msg_len(logstructure->format);
if (msg_len != logstructure->msg_len) {

View File

@ -470,6 +470,7 @@ private:
const char* unit_name(const uint8_t unit_id);
double multiplier_name(const uint8_t multiplier_id);
bool seen_ids[256] = { };
bool labels_string_is_good(const char *labels) const;
#endif
// possibly expensive calls to start log system: