@@ -273,13 +273,21 @@ impl<'a, M: RawMutex, B: I2c> Tps6699x<'a, M, B> {
273273 }
274274
275275 if done {
276- // Put back any unhandled interrupt flags for future processing
276+ // Panic safety: `unhandled`, `accumulated_flags`, and `mask` are all of size MAX_SUPPORTED_PORTS
277+ // so this will never index out of bounds
278+ #[ allow( clippy:: indexing_slicing) ]
277279 let unhandled = from_fn ( |i| accumulated_flags[ i] & !mask[ i] ) ;
280+
281+ // Put back any unhandled interrupt flags for future processing
278282 if unhandled. iter ( ) . any ( |& f| f != IntEventBus1 :: new_zero ( ) ) {
279283 // If there are unhandled flags, signal them for future processing
280284 trace ! ( "Signaling unhandled interrupt flags: {:?}" , unhandled) ;
281285 self . controller . interrupt_waker . signal ( unhandled) ;
282286 }
287+
288+ // Panic safety: the return type, `accumulated_flags`, and `mask` are all of size MAX_SUPPORTED_PORTS
289+ // so this will never index out of bounds
290+ #[ allow( clippy:: indexing_slicing) ]
283291 return from_fn ( |i| accumulated_flags[ i] & mask[ i] ) ;
284292 }
285293 }
@@ -329,18 +337,18 @@ impl<'a, M: RawMutex, B: I2c> Tps6699x<'a, M, B> {
329337 ) -> Result < ReturnValue , Error < B :: Error > > {
330338 let timeout = cmd. timeout ( ) ;
331339 let result = with_timeout ( timeout, self . execute_command_no_timeout ( port, cmd, indata, outdata) ) . await ;
332- if result. is_err ( ) {
340+ if let Ok ( result) = result {
341+ result
342+ } else {
333343 error ! ( "Command {:#?} timed out" , cmd) ;
334344 // See if there's a definite error we can read
335345 let mut inner = self . lock_inner ( ) . await ;
336- return match inner. read_command_result ( port, None , cmd. has_return_value ( ) ) . await ? {
346+ match inner. read_command_result ( port, None , cmd. has_return_value ( ) ) . await ? {
337347 ReturnValue :: Rejected => PdError :: Rejected ,
338348 _ => PdError :: Timeout ,
339349 }
340- . into ( ) ;
350+ . into ( )
341351 }
342-
343- result. unwrap ( )
344352 }
345353
346354 async fn execute_srdy ( & mut self , port : LocalPortId , switch : SrdySwitch ) -> Result < ReturnValue , Error < B :: Error > > {
@@ -390,7 +398,8 @@ impl<'a, M: RawMutex, B: I2c> Tps6699x<'a, M, B> {
390398 let args = trig:: Args { edge, cmd } ;
391399 let mut args_buf = [ 0 ; trig:: ARGS_LEN ] ;
392400
393- bincode:: encode_into_slice ( args, & mut args_buf, config:: standard ( ) . with_fixed_int_encoding ( ) ) . unwrap ( ) ;
401+ bincode:: encode_into_slice ( args, & mut args_buf, config:: standard ( ) . with_fixed_int_encoding ( ) )
402+ . map_err ( |_| Error :: Pd ( PdError :: InvalidParams ) ) ?;
394403
395404 self . execute_command ( port, Command :: Trig , Some ( & args_buf) , None ) . await
396405 }
@@ -639,10 +648,9 @@ impl<'a, M: RawMutex, B: I2c> Tps6699x<'a, M, B> {
639648 . get_rx_caps ( port, register, & mut out_spr_pdos, & mut out_epr_pdos)
640649 . await ?;
641650
642- // These unwraps are safe because we know the sizes of the arrays
643651 Ok ( rx_caps:: RxCaps {
644- spr : heapless:: Vec :: from_slice ( & out_spr_pdos[ ..num_valid_spr ] ) . unwrap ( ) ,
645- epr : heapless:: Vec :: from_slice ( & out_epr_pdos[ ..num_valid_epr ] ) . unwrap ( ) ,
652+ spr : heapless:: Vec :: from_iter ( out_spr_pdos. into_iter ( ) . take ( num_valid_spr ) ) ,
653+ epr : heapless:: Vec :: from_iter ( out_epr_pdos. into_iter ( ) . take ( num_valid_epr ) ) ,
646654 } )
647655 }
648656
@@ -807,10 +815,19 @@ impl<'a, M: RawMutex, B: I2c> Interrupt<'a, M, B> {
807815 {
808816 let interrupts_enabled = self . controller . interrupts_enabled ( ) ;
809817 let mut inner = self . lock_inner ( ) . await ;
810- for port in 0 ..inner. num_ports ( ) {
818+
819+ // Note: `interrupts_enabled` and `flags` are both of size MAX_SUPPORTED_PORTS and so
820+ // will always have a 1:1 mapping. If `num_ports` ever returns a value larger than
821+ // MAX_SUPPORTED_PORTS, `port` will simply be capped at MAX_SUPPORTED_PORTS.
822+ for ( port, ( interrupt_enabled, flag) ) in interrupts_enabled
823+ . iter ( )
824+ . zip ( flags. iter_mut ( ) )
825+ . take ( inner. num_ports ( ) )
826+ . enumerate ( )
827+ {
811828 let port_id = LocalPortId ( port as u8 ) ;
812829
813- if !interrupts_enabled [ port ] {
830+ if !interrupt_enabled {
814831 trace ! ( "{:?}: Interrupt for disabled" , port_id) ;
815832 continue ;
816833 }
@@ -830,7 +847,7 @@ impl<'a, M: RawMutex, B: I2c> Interrupt<'a, M, B> {
830847
831848 match with_timeout ( Duration :: from_millis ( 100 ) , inner. clear_interrupt ( port_id) ) . await {
832849 Ok ( res) => match res {
833- Ok ( event) => flags [ port ] |= event,
850+ Ok ( event) => * flag |= event,
834851 Err ( _e) => {
835852 continue ;
836853 }
0 commit comments