Add some basic validation to freqman parsing (#1256)

Co-authored-by: kallanreed <kallanreed@noreply.github.com>
This commit is contained in:
Kyle Reed 2023-07-09 11:15:14 -07:00 committed by GitHub
parent 497ca3f934
commit 63be4de418
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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) { bool parse_freqman_entry(std::string_view str, freqman_entry& entry) {
if (str.empty() || str[0] == '#') if (str.empty() || str[0] == '#')
return false; return false;
auto cols = split_string(str, ',');
entry = freqman_entry{}; entry = freqman_entry{};
auto cols = split_string(str, ',');
for (auto col : cols) { for (auto col : cols) {
if (col.empty()) 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; return true;
} }