{"record":{"id":"7686bd0989291cd0","repo":"embassy-rs/embassy","slug":"invalid-watchdog-scratch-index","errorCode":null,"errorMessage":"Invalid watchdog scratch index","messagePattern":"Invalid watchdog scratch index","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-rp/src/watchdog.rs","lineNumber":143,"sourceCode":"        let watchdog = pac::WATCHDOG;\n        watchdog.ctrl().write(|w| {\n            w.set_trigger(true);\n        })\n    }\n\n    /// Store data in scratch register\n    pub fn set_scratch(&mut self, index: usize, value: u32) {\n        let watchdog = pac::WATCHDOG;\n        match index {\n            0 => watchdog.scratch0().write(|w| *w = value),\n            1 => watchdog.scratch1().write(|w| *w = value),\n            2 => watchdog.scratch2().write(|w| *w = value),\n            3 => watchdog.scratch3().write(|w| *w = value),\n            4 => watchdog.scratch4().write(|w| *w = value),\n            5 => watchdog.scratch5().write(|w| *w = value),\n            6 => watchdog.scratch6().write(|w| *w = value),\n            7 => watchdog.scratch7().write(|w| *w = value),\n            _ => panic!(\"Invalid watchdog scratch index\"),\n        }\n    }\n\n    /// Read data from scratch register\n    pub fn scratch(&mut self, index: usize) -> u32 {\n        let watchdog = pac::WATCHDOG;\n        match index {\n            0 => watchdog.scratch0().read(),\n            1 => watchdog.scratch1().read(),\n            2 => watchdog.scratch2().read(),\n            3 => watchdog.scratch3().read(),\n            4 => watchdog.scratch4().read(),\n            5 => watchdog.scratch5().read(),\n            6 => watchdog.scratch6().read(),\n            7 => watchdog.scratch7().read(),\n            _ => panic!(\"Invalid watchdog scratch index\"),\n        }\n    }","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-rp/src/watchdog.rs#L125-L161","documentation":"`Watchdog::set_scratch` writes to one of the watchdog's 8 scratch registers (indices 0-7); the hardware has no register beyond scratch7, so any other index panics. Scratch registers persist across soft resets and are meant for passing data to the bootloader or across resets.","triggerScenarios":"Calling `watchdog.set_scratch(index, value)` with an index outside 0..=7, e.g. set_scratch(8, x) or an index computed from a constant that was off-by-one or 1-based by mistake.","commonSituations":"Off-by-one when mapping logical slots to scratch indices; 1-based numbering in user config applied directly as the hardware index; loops over 1..=8 instead of 0..=8.","solutions":["Pass an index in the range 0..=7; verify the call site value","Add a debug_assert or clamp/log for the index in your own wrapper before calling set_scratch","Audit loops/constants generating the index for off-by-one errors"],"exampleFix":"// before\nwatchdog.set_scratch(slot_number, data); // slot_number is 1-based\n// after\nassert!(slot_number >= 1 && slot_number <= 8);\nwatchdog.set_scratch(slot_number - 1, data); // hardware index 0..=7","handlingStrategy":"validation","validationCode":"fn set_scratch_safe(w: &mut embassy_rp::watchdog::Watchdog<'_>, i: usize, v: u32) {\n    assert!(i <= 7, \"scratch index must be 0..=7\");\n    w.set_scratch(i, v);\n}","typeGuard":null,"tryCatchPattern":"// panic in no_std: bounds-check before the call\nif index <= 7 { watchdog.set_scratch(index, value); }","preventionTips":["Standardize on 0-based scratch indices across your codebase","Bounds-check index constants and loop ranges (0..8, not 1..8)","Wrap set_scratch/scratch in a checked helper used everywhere"],"tags":["embedded","rp2040","watchdog","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}