SubGHz: subghz_setting_get_inx_preset_by_name furi_crashes on a miss while claiming to return -1
Found while reviewing #1104. Lower priority than #1159 / #1160 — I could not find a path that reaches this today, so this is hardening, not a live crash. Filing it because it is the same "a lookup miss aborts the firmware" shape #1104 was about, and because the function's own signature says it does not crash.
What
subghz_setting_get_inx_preset_by_name() in lib/subghz/subghz_setting.c returns int and ends with return -1; — i.e. it advertises a "not found" result — but it can never return it:
int subghz_setting_get_inx_preset_by_name(SubGhzSetting* instance, const char* preset_name) {
furi_check(instance);
size_t idx = 0;
for M_EACH(item, instance->preset->data, SubGhzSettingCustomPresetItemArray_t) {
if(strcmp(furi_string_get_cstr(item->custom_preset_name), preset_name) == 0) {
return idx;
}
idx++;
}
furi_crash("SubGhz: No name preset.");
return -1; // dead
}subghz_setting_get_preset_data_by_name() then feeds that result straight into SubGhzSettingCustomPresetItemArray_get() without checking it, so neither caller could handle -1 even if it were returned.
Callers: subghz_txrx.c (subghz_txrx_tx_start, subghz_txrx_rx_start), subghz_i.c (subghz_key_load).
Why it is not reachable today
A preset name coming from a .sub file is canonicalised by subghz_txrx_get_preset_name() before it ever reaches the lookup — it maps the six known FuriHalSubGhzPreset* strings to AM270 / AM650 / FM238 / FM12K / FM476 / CUSTOM, and anything else to "", which fails the load earlier with "Cannot parse file". All five non-custom names are pushed unconditionally by subghz_setting_load_default(), which always runs before setting_user is read, and setting_user can only remove frequencies (Add_standard_frequencies), never presets. CUSTOM is inserted on demand from the file itself, and a failed insert breaks out of the load.
So every name that reaches the lookup is guaranteed present. The crash is defence against a caller bug, not against user input.
Why still worth fixing
- It is a
furi_crashin sharedlib/reached from a path fed by SD-card data. The canonicalisation that makes it safe lives in a different file (applications/main/subghz/helpers/subghz_txrx.c) — that coupling is invisible fromsubghz_setting.cand one refactor away from being wrong. - The
intreturn andreturn -1actively mislead: a reader adding a caller will reasonably assume a miss is representable.
Suggested fix
Either make the miss real — return -1, and have subghz_setting_get_preset_data_by_name() return NULL on it, with the two callers treating that as the existing "unknown preset" error — or, if crashing is the deliberate contract, drop the int/-1 fiction and document it.
@xMasterX your call on which way, or whether it is worth touching at all — assigning to you to decide. Happy to close as by-design.
Source: DarkFlippers/unleashed-firmware