Browse Source

AP_Logger: add message validation check against duplicate labels

c415-sdk
Peter Barker 5 years ago committed by Peter Barker
parent
commit
acba2a78f6
  1. 42
      libraries/AP_Logger/AP_Logger.cpp
  2. 1
      libraries/AP_Logger/AP_Logger.h

42
libraries/AP_Logger/AP_Logger.cpp

@ -298,6 +298,44 @@ void AP_Logger::dump_structures(const struct LogStructure *logstructures, const @@ -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 @@ -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) {

1
libraries/AP_Logger/AP_Logger.h

@ -470,6 +470,7 @@ private: @@ -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:

Loading…
Cancel
Save