Skip to content

Reject a zero link status period at the tunable setter - #152

Open
zigpy-review-bot wants to merge 2 commits into
devfrom
zigpy-bot/zero-link-status-period
Open

zigpy-review-bot wants to merge 2 commits into
devfrom
zigpy-bot/zero-link-status-period

Conversation

@zigpy-review-bot

@zigpy-review-bot zigpy-review-bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Setting the link_status_period tunable to 0 crashes the stack since #145: take_decay_steps divides by the period, and activity decay runs on received link statuses, relayed unicasts, and accepted route requests and replies, among others. The tunable setter accepted any u32 microseconds, zero included.

Tunables now carry an optional valid range, and set rejects a value outside it, so a value the stack cannot run with is refused when it is set instead of crashing it later.

Changes

  • tunables! takes an optional ; valid <range> after a field's default. Tunables::set returns TunableError::InvalidValue ("value out of range") for a value outside that range and leaves the stored value unchanged, the same error a value that doesn't fit the type already gets, which the bridge answers with an InvalidRequest status.
  • link_status_period must be at least 1 µs.
  • The zero-period special case this PR first added to take_decay_steps is gone again: its code is as on dev, and its doc now says the period must be at least 1 µs.

Only link_status_period gets a range here: it is the one tunable I found where a value the setter accepts panics the stack. The broadcast token bucket treats a zero refill interval as "no rate limit", and the jitter pairs subtract saturating, so an inverted pair does not panic either. Tighter bounds on other tunables are left for a later change.

Testing

  • New unit test test_set_rejects_value_outside_valid_range: setting link_status_period to 0 is rejected and keeps the old value; 1 µs is accepted.
Checks run
  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets --all-features -- -D warnings -Dclippy::all -Dclippy::nursery
  • cargo test --workspace --all-features: all pass

`take_decay_steps` divides by `link_status_period`, which the tunable setter
accepts as zero. Activity decay now runs on received link statuses, relayed
unicasts, and accepted route requests and replies, so a zero period crashed
the stack on the receive path.

Treat a zero period as a count lasting no time: everything decays at once,
and the same instant again owes nothing.
@zigpy-review-bot zigpy-review-bot added bugfix This PR fixes a bug bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) labels Nov 23, 2026
@TheJulianJES
TheJulianJES requested a balanced review from Copilot November 23, 2026 03:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the change addressing zero-period Zigbee activity decay.

No open issues 🎉

What changed in this PR

Prevents a zero link_status_period from causing a division-by-zero panic during Zigbee network activity decay.

Changes:

  • Returns maximum decay steps when time has advanced with a zero period, and zero steps when no time has advanced.
  • Adds a regression test covering zero-period decay and repeated calls at the same instant.
File Description
crates/​ziggurat-zigbee/​src/​nwk/​activity.rs Handles zero periods safely in take_decay_steps and adds regression coverage.

Review effort: Balanced

@puddly

puddly commented Nov 23, 2026

Copy link
Copy Markdown
Member

Tunables are effectively undocumented stack constants and their purpose is to allow low-level tweaking of the stack without rebuilding firmwares. If you choose to tweak one, crashing the stack is a possibility.

I think a proper fix for this bug would be to have basic validation (e.g. greater than 0, less than 255) for tunables and to reject invalid values.

Tunables get an optional valid range in the `tunables!` list: `set` rejects a
value outside it with `InvalidValue`, leaving the stored value unchanged.
`link_status_period` must be non-zero, since activity decay divides by it.

This replaces the zero-period special case in `take_decay_steps`: a value the
stack cannot run with is refused when it is set instead of being worked around
where it is read.
@zigpy-review-bot zigpy-review-bot changed the title Do not panic on a zero link status period Reject a zero link status period at the tunable setter Nov 23, 2026
@zigpy-review-bot

Copy link
Copy Markdown
Collaborator Author

Agreed, validating at the setter is the better fix. Pushed 403ab27: tunables can now declare a valid range in the tunables! list, set rejects a value outside it with the existing InvalidValue error (the stored value stays as it was), and link_status_period must be non-zero. The special case in take_decay_steps is gone; its doc states the non-zero precondition instead.

I only gave link_status_period a range, since it is the only tunable I found where an accepted value panics the stack (a zero token refill interval means "no rate limit", and the jitter pairs subtract saturating). Adding bounds to the others is one ; valid <range> per field if you want them; I didn't pick limits for those myself.

Title and body updated to match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) bugfix This PR fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants