File tree Expand file tree Collapse file tree 2 files changed +35
-5
lines changed Expand file tree Collapse file tree 2 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -50,13 +50,24 @@ impl<const N: usize> FromBase32 for [u8; N] {
5050 type Err = Bolt11ParseError ;
5151
5252 fn from_base32 ( data : & [ Fe32 ] ) -> Result < Self , Self :: Err > {
53- data. iter ( ) . copied ( ) . fes_to_bytes ( ) . collect :: < Vec < _ > > ( ) . try_into ( ) . map_err ( |_| {
54- Bolt11ParseError :: InvalidSliceLength (
53+ let mut res_arr = [ 0 ; N ] ;
54+ // Do in a for loop to place in the array directly, not using `collect`
55+ let mut idx = 0 ;
56+ for elem in data. iter ( ) . copied ( ) . fes_to_bytes ( ) {
57+ if idx >= N {
58+ break ; // too many inputs, stop early, OK
59+ }
60+ res_arr[ idx] = elem;
61+ idx += 1 ;
62+ }
63+ if idx != N {
64+ return Err ( Bolt11ParseError :: InvalidSliceLength (
5565 data. len ( ) ,
5666 ( N * 8 + 4 ) / 5 ,
5767 "<[u8; N]>" ,
58- )
59- } )
68+ ) ) ;
69+ }
70+ Ok ( res_arr)
6071 }
6172}
6273
Original file line number Diff line number Diff line change @@ -59,15 +59,34 @@ fn array_u8() {
5959
6060#[ test]
6161fn array_u8_error_invalid_length ( ) {
62+ // correct len -- 5 fe32 -> 3 bytes
6263 assert_eq ! (
6364 <[ u8 ; 3 ] >:: from_base32(
64- & "qqqs" . to_string( ) . chars( ) . map( |c| Fe32 :: from_char( c) . unwrap( ) ) . collect:: <Vec <_>>( ) [ ..]
65+ & "pqqql" . to_string( ) . chars( ) . map( |c| Fe32 :: from_char( c) . unwrap( ) ) . collect:: <Vec <_>>( ) [ ..]
66+ )
67+ . unwrap( ) ,
68+ [ 8 , 0 , 15 ]
69+ ) ;
70+
71+ // input too short
72+ assert_eq ! (
73+ <[ u8 ; 3 ] >:: from_base32(
74+ & "pqql" . to_string( ) . chars( ) . map( |c| Fe32 :: from_char( c) . unwrap( ) ) . collect:: <Vec <_>>( ) [ ..]
6575 )
6676 . err( )
6777 . unwrap( )
6878 . to_string( ) ,
6979 "Slice had length 4 instead of 5 for element <[u8; N]>"
7080 ) ;
81+
82+ // input too long
83+ assert_eq ! (
84+ <[ u8 ; 3 ] >:: from_base32(
85+ & "pqqqql" . to_string( ) . chars( ) . map( |c| Fe32 :: from_char( c) . unwrap( ) ) . collect:: <Vec <_>>( ) [ ..]
86+ )
87+ . unwrap( ) ,
88+ [ 8 , 0 , 0 ]
89+ ) ;
7190}
7291
7392#[ test]
You can’t perform that action at this time.
0 commit comments