11use crate :: coder:: { Buffer , Decoder , Encoder , Result , View } ;
2+ use crate :: error:: err;
23use crate :: fast:: { CowSlice , NextUnchecked , PushUnchecked , VecImpl } ;
34use crate :: pack:: { pack_bytes_less_than, unpack_bytes_less_than} ;
45use crate :: pack_ints:: { pack_ints, unpack_ints, Int } ;
@@ -54,13 +55,19 @@ impl<'a, T: Int, const N: usize> VariantDecoder<'a, T, N, false> {
5455 }
5556}
5657
57- impl < ' a , T : Int , const N : usize , const C_STYLE : bool > View < ' a >
58+ impl < ' a , T : Int + Into < usize > , const N : usize , const C_STYLE : bool > View < ' a >
5859 for VariantDecoder < ' a , T , N , C_STYLE >
5960{
6061 fn populate ( & mut self , input : & mut & ' a [ u8 ] , length : usize ) -> Result < ( ) > {
6162 assert ! ( N >= 2 ) ;
6263 if TypeId :: of :: < T > ( ) != TypeId :: of :: < u8 > ( ) {
6364 unpack_ints :: < T > ( input, length, & mut self . variants ) ?;
65+ // TOOD: this uses extra memory bandwith to rescan.
66+ for int in unsafe { self . variants . as_slice ( length) } {
67+ if T :: from_unaligned ( * int) . into ( ) >= N {
68+ return err ( "invalid enum variant index" ) ;
69+ }
70+ }
6471 } else {
6572 // SAFETY: Checked the type above and [u8; 1] has the
6673 // same memory layout as `u8`.
@@ -79,7 +86,7 @@ impl<'a, T: Int, const N: usize, const C_STYLE: bool> View<'a>
7986 }
8087}
8188
82- impl < ' a , T : Int , const N : usize , const C_STYLE : bool > Decoder < ' a , T >
89+ impl < ' a , T : Int + Into < usize > , const N : usize , const C_STYLE : bool > Decoder < ' a , T >
8390 for VariantDecoder < ' a , T , N , C_STYLE >
8491{
8592 // Guaranteed to output numbers less than N.
0 commit comments