SubGHz: Add Manually key generation frees the shared transmitter unguarded and leaves it dangling (13 sites)

Author: mishamyteCreated Sep 21, 2026Updated Sep 24, 2026
Labelstype/bugarea/subghz

Found while reviewing #1104. That PR fixes this exact pattern in subghz_txrx_tx_start(); the same shape is still present 13 times in subghz_txrx_create_protocol_key.c.

1. Unguarded free after a guarded use

Every subghz_txrx_gen_*_protocol() looks like this (keeloq shown, lines ~108-125):

c
    txrx->transmitter = subghz_transmitter_alloc_init(txrx->environment, SUBGHZ_PROTOCOL_..._NAME);
    ...
    if(txrx->transmitter && subghz_protocol_..._create_data(...)) {   // <- NULL is expected here
        res = true;
    }
    subghz_transmitter_free(txrx->transmitter);                       // <- but freed unconditionally

subghz_transmitter_free() opens with furi_check(instance) (lib/subghz/transmitter.c), so a NULL here aborts the firmware — the crash #1104 fixes on the TX path. The if(txrx->transmitter && ...) guard on the line above says the author expected NULL to be possible.

It does not fire today only because these protocol names are compile-time constants that are always in the registry with a non-NULL encoder->alloc, unlike the file-supplied name in tx_start. It becomes live the moment a protocol is built out, reordered, or given a NULL encoder — exactly what iDo already is.

Affected lines in applications/main/subghz/helpers/subghz_txrx_create_protocol_key.c: 124, 168, 200, 245, 275, 303, 333, 363, 393, 423, 453, 483, 560.

2. The field is left dangling

None of those 13 frees clears txrx->transmitter, so after any Add Manually key generation the shared struct field holds a freed pointer. #1104 establishes "NULL means no live transmission" for tx_start/tx_stop; this file breaks that invariant everywhere else, which makes the new if(instance->transmitter) guard weaker than it reads.

Harmless today only because subghz_txrx_tx_start() unconditionally overwrites the field before any guard reads it.

Suggested fix (deeper than adding NULL assignments)

SubGhzTxRx::transmitter is carrying two incompatible meanings: "the live transmission" (owned by tx_start..tx_stop, read by subghz_devices_start_async_tx and subghz_txrx_set_raw_file_encoder_worker_callback_end) and "scratch variable" (these 14 functions, which alloc, use and free it entirely within one call).

Give the gen_* functions a plain local:

c
    SubGhzTransmitter* transmitter = subghz_transmitter_alloc_init(...);
    ...
    if(transmitter) {
        ...
        subghz_transmitter_free(transmitter);
    }

That removes the dangling-pointer question and the NULL-free at once, costs no flash (it removes a store per site), needs no header change, and leaves the struct field meaning only "the live transmission". Roughly 45 lines across one file, no API surface — the gen_* prototypes and the private subghz_txrx_i.h field stay as they are.

There are exactly four readers of the field and none observes it between a gen_* returning and tx_start overwriting it, so the change is low risk; it does want an Add Manually smoke test across the 14 protocols.

Related: #1159 (the 14th function leaks instead of freeing).

Not hardware-tested; found by code inspection.

@xMasterX does the local-variable direction look right to you, or would you rather just add the NULL assignments? Assigning to you to pick.

Source: DarkFlippers/unleashed-firmware