From 728ae0aab8cf5eae7769a886f50923e6efbc7464 Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Mon, 23 Nov 2026 02:58:22 +0100 Subject: [PATCH 1/2] Do not panic on a zero link status period `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. --- crates/ziggurat-zigbee/src/nwk/activity.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/ziggurat-zigbee/src/nwk/activity.rs b/crates/ziggurat-zigbee/src/nwk/activity.rs index 4bf77424..0b41387d 100644 --- a/crates/ziggurat-zigbee/src/nwk/activity.rs +++ b/crates/ziggurat-zigbee/src/nwk/activity.rs @@ -55,6 +55,14 @@ pub struct ActivityParams { pub fn take_decay_steps(decayed_at: &mut Instant, now: Instant, period: Duration) -> u16 { let steps_per_count = u64::from(STEPS_PER_COUNT); let period = u64::try_from(period.as_micros()).unwrap(); + + // The `link_status_period` tunable accepts zero: a count then lasts no time at all + if period == 0 { + let owed = now > *decayed_at; + *decayed_at = (*decayed_at).max(now); + return if owed { u16::MAX } else { 0 }; + } + let elapsed = now.as_micros().saturating_sub(decayed_at.as_micros()); let steps = elapsed * steps_per_count / period; @@ -114,6 +122,20 @@ mod test { assert_eq!(take_decay_steps(&mut decayed_at, now, period), 0); } + #[test] + fn test_zero_period_decays_everything_without_panicking() { + let mut decayed_at = at(0); + + assert_eq!( + take_decay_steps(&mut decayed_at, at(1), Duration::ZERO), + u16::MAX + ); + assert_eq!(decayed_at, at(1)); + + // The same instant again owes nothing + assert_eq!(take_decay_steps(&mut decayed_at, at(1), Duration::ZERO), 0); + } + #[test] fn test_long_gap_is_taken_in_one_call() { let mut decayed_at = at(0); From 403ab2783b553d27c8bc7448da486ed6fade407e Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:28:13 +0100 Subject: [PATCH 2/2] Reject a zero link status period at the tunable setter 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. --- crates/ziggurat-zigbee/src/constants.rs | 32 ++++++++++++++++++++-- crates/ziggurat-zigbee/src/nwk/activity.rs | 25 ++--------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/crates/ziggurat-zigbee/src/constants.rs b/crates/ziggurat-zigbee/src/constants.rs index 3f8aa143..672e1b56 100644 --- a/crates/ziggurat-zigbee/src/constants.rs +++ b/crates/ziggurat-zigbee/src/constants.rs @@ -1,5 +1,6 @@ use core::fmt; use core::marker::PhantomData; +use core::ops::RangeBounds; use core::sync::atomic::Ordering::Relaxed; use core::sync::atomic::{AtomicBool, AtomicU8, AtomicU32, AtomicUsize}; use core::time::Duration; @@ -218,8 +219,12 @@ impl TunableValue for EndDeviceTimeout { /// Defines [`Tunables`] once: the struct, its defaults, the typed getters, and the /// by-field-name `set` used by the `SetTunable` protocol command all come from this /// single field list. +/// +/// A field of an ordered type may end in `; valid `: `set` then rejects a value +/// outside that range, for values the stack cannot run with at all (not every unwise +/// one). macro_rules! tunables { - ($($(#[$attr:meta])* $name:ident: $ty:ty = $default:expr,)*) => { + ($($(#[$attr:meta])* $name:ident: $ty:ty = $default:expr $(; valid $valid:expr)?,)*) => { /// Protocol parameters and policy knobs, initialized to spec-derived defaults. /// /// Unlike the module-level constants above, every field here is tweakable at @@ -253,6 +258,11 @@ macro_rules! tunables { $(stringify!($name) => { let value = <$ty as TunableValue>::decode(raw) .ok_or(TunableError::InvalidValue)?; + $( + if !RangeBounds::<$ty>::contains(&$valid, &value) { + return Err(TunableError::InvalidValue); + } + )? <$ty as TunableValue>::store(value, &self.$name); })* _ => return Err(TunableError::UnknownName), @@ -312,8 +322,9 @@ tunables! { /// many-to-one route request. mtorr_delivery_failure_threshold: u8 = 1, - /// The time between link status command frames. - link_status_period: Duration = Duration::from_secs(15), + /// The time between link status command frames. Activity decay divides by it, so + /// it cannot be zero. + link_status_period: Duration = Duration::from_secs(15); valid Duration::from_micros(1).., /// `nwkcMinRouterBootstrapJitter`: the shortest delay before a gratuitous link /// status. @@ -452,4 +463,19 @@ mod test { ); assert!(tunables.hack_beacon_spam_duration().is_zero()); } + + #[test] + fn test_set_rejects_value_outside_valid_range() { + let tunables = Tunables::default(); + + // A zero link status period: rejected, value unchanged. + assert_eq!( + tunables.set("link_status_period", 0), + Err(TunableError::InvalidValue) + ); + assert_eq!(tunables.link_status_period(), Duration::from_secs(15)); + + tunables.set("link_status_period", 1).unwrap(); + assert_eq!(tunables.link_status_period(), Duration::from_micros(1)); + } } diff --git a/crates/ziggurat-zigbee/src/nwk/activity.rs b/crates/ziggurat-zigbee/src/nwk/activity.rs index 0b41387d..4c16c868 100644 --- a/crates/ziggurat-zigbee/src/nwk/activity.rs +++ b/crates/ziggurat-zigbee/src/nwk/activity.rs @@ -52,17 +52,12 @@ pub struct ActivityParams { /// The decay steps owed for the time from `*decayed_at` to `now`, at one count per /// `period`. `*decayed_at` moves forward by the steps returned, so the rest of the time /// counts toward the next call. +/// +/// `period` must be at least one microsecond: the `link_status_period` tunable it +/// comes from rejects anything shorter. pub fn take_decay_steps(decayed_at: &mut Instant, now: Instant, period: Duration) -> u16 { let steps_per_count = u64::from(STEPS_PER_COUNT); let period = u64::try_from(period.as_micros()).unwrap(); - - // The `link_status_period` tunable accepts zero: a count then lasts no time at all - if period == 0 { - let owed = now > *decayed_at; - *decayed_at = (*decayed_at).max(now); - return if owed { u16::MAX } else { 0 }; - } - let elapsed = now.as_micros().saturating_sub(decayed_at.as_micros()); let steps = elapsed * steps_per_count / period; @@ -122,20 +117,6 @@ mod test { assert_eq!(take_decay_steps(&mut decayed_at, now, period), 0); } - #[test] - fn test_zero_period_decays_everything_without_panicking() { - let mut decayed_at = at(0); - - assert_eq!( - take_decay_steps(&mut decayed_at, at(1), Duration::ZERO), - u16::MAX - ); - assert_eq!(decayed_at, at(1)); - - // The same instant again owes nothing - assert_eq!(take_decay_steps(&mut decayed_at, at(1), Duration::ZERO), 0); - } - #[test] fn test_long_gap_is_taken_in_one_call() { let mut decayed_at = at(0);