@@ -8,8 +8,8 @@ use vmm_sys_util::fam::{FamStruct, FamStructWrapper};
88
99/// Maximum string len in bytes (16KB).
1010pub const MAX_STRING_LEN : usize = 16384 ;
11- /// Maximum vec len in bytes (10MB).
12- pub const MAX_VEC_LEN : usize = 10_485_760 ;
11+ /// Maximum vec size in bytes (10MB).
12+ pub const MAX_VEC_SIZE : usize = 10_485_760 ;
1313
1414/// Implements the Versionize trait for primitive types that also implement
1515/// serde's Serialize/Deserialize: use serde_bincode as a backend for
@@ -291,9 +291,8 @@ where
291291 version_map : & VersionMap ,
292292 app_version : u16 ,
293293 ) -> VersionizeResult < ( ) > {
294- let bytes_len = self . len ( ) * std:: mem:: size_of :: < T > ( ) ;
295- if bytes_len > MAX_VEC_LEN {
296- return Err ( VersionizeError :: VecLength ( bytes_len) ) ;
294+ if self . len ( ) > MAX_VEC_SIZE / std:: mem:: size_of :: < T > ( ) {
295+ return Err ( VersionizeError :: VecLength ( self . len ( ) ) ) ;
297296 }
298297 // Serialize in the same fashion as bincode:
299298 // Write len.
@@ -316,9 +315,8 @@ where
316315 let len: usize = bincode:: deserialize_from ( & mut reader)
317316 . map_err ( |ref err| VersionizeError :: Deserialize ( format ! ( "{:?}" , err) ) ) ?;
318317
319- let bytes_len = len * std:: mem:: size_of :: < T > ( ) ;
320- if bytes_len > MAX_VEC_LEN {
321- return Err ( VersionizeError :: VecLength ( bytes_len) ) ;
318+ if len > MAX_VEC_SIZE / std:: mem:: size_of :: < T > ( ) {
319+ return Err ( VersionizeError :: VecLength ( len) ) ;
322320 }
323321
324322 for _ in 0 ..len {
@@ -704,14 +702,14 @@ mod tests {
704702 #[ test]
705703 fn test_vec_limit ( ) {
706704 // We need extra 8 bytes for vector len.
707- let mut snapshot_mem = vec ! [ 0u8 ; MAX_VEC_LEN + 8 ] ;
708- let err = vec ! [ 123u8 ; MAX_VEC_LEN + 1 ]
705+ let mut snapshot_mem = vec ! [ 0u8 ; MAX_VEC_SIZE + 8 ] ;
706+ let err = vec ! [ 123u8 ; MAX_VEC_SIZE + 1 ]
709707 . serialize ( & mut snapshot_mem. as_mut_slice ( ) , & VersionMap :: new ( ) , 1 )
710708 . unwrap_err ( ) ;
711- assert_eq ! ( err, VersionizeError :: VecLength ( MAX_VEC_LEN + 1 ) ) ;
709+ assert_eq ! ( err, VersionizeError :: VecLength ( MAX_VEC_SIZE + 1 ) ) ;
712710 assert_eq ! (
713711 format!( "{}" , err) ,
714- "Vec length exceeded 10485761 > 10485760 bytes"
712+ "Vec of length 10485761 exceeded maximum size of 10485760 bytes"
715713 ) ;
716714 }
717715
0 commit comments