From 63be4de418d96dcef35b13caa5fb6451042a3ead Mon Sep 17 00:00:00 2001 From: Kyle Reed <3761006+kallanreed@users.noreply.github.com> Date: Sun, 9 Jul 2023 11:15:14 -0700 Subject: [PATCH] Add some basic validation to freqman parsing (#1256) Co-authored-by: kallanreed --- firmware/application/freqman_db.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/firmware/application/freqman_db.cpp b/firmware/application/freqman_db.cpp index b1c75547..e57731a5 100644 --- a/firmware/application/freqman_db.cpp +++ b/firmware/application/freqman_db.cpp @@ -146,15 +146,12 @@ uint8_t find_by_name(const options_t& options, std::string_view name) { *} */ -// TODO: How much format validation should this do? -// It's very permissive right now, but entries can be invalid. -// TODO: parse_int seems to hang on invalid input. bool parse_freqman_entry(std::string_view str, freqman_entry& entry) { if (str.empty() || str[0] == '#') return false; - auto cols = split_string(str, ','); entry = freqman_entry{}; + auto cols = split_string(str, ','); for (auto col : cols) { if (col.empty()) @@ -215,6 +212,24 @@ bool parse_freqman_entry(std::string_view str, freqman_entry& entry) { } } + // No valid frequency combination was set. + if (entry.type == freqman_type::Unknown) + return false; + + // Ranges should have both frequencies set and A <= B. + if (entry.type == freqman_type::Range || entry.type == freqman_type::HamRadio) { + if (entry.frequency_a == 0 || entry.frequency_b == 0) + return false; + + if (entry.frequency_a > entry.frequency_b) + return false; + } + + // TODO: Consider additional validation: + // - Tone only on HamRadio. + // - Fail on failed parse_int. + // - Fail if bandwidth set before modulation. + return true; }