Do not check for data oversize when unchecked.

This commit is contained in:
Stephen Chung 2020-06-15 22:04:14 +08:00
parent a417bdd8e3
commit fcb5059570
2 changed files with 37 additions and 31 deletions

View File

@ -29,24 +29,27 @@ fn pad<T: Variant + Clone>(engine: &Engine, args: &mut [&mut Dynamic]) -> FuncRe
let len = *args[1].downcast_ref::<INT>().unwrap(); let len = *args[1].downcast_ref::<INT>().unwrap();
// Check if array will be over max size limit // Check if array will be over max size limit
if engine.max_array_size > 0 && len > 0 && (len as usize) > engine.max_array_size { #[cfg(not(feature = "unchecked"))]
Err(Box::new(EvalAltResult::ErrorDataTooLarge( {
"Size of array".to_string(), if engine.max_array_size > 0 && len > 0 && (len as usize) > engine.max_array_size {
engine.max_array_size, return Err(Box::new(EvalAltResult::ErrorDataTooLarge(
len as usize, "Size of array".to_string(),
Position::none(), engine.max_array_size,
))) len as usize,
} else if len >= 0 { Position::none(),
)));
}
}
if len >= 0 {
let item = args[2].downcast_ref::<T>().unwrap().clone(); let item = args[2].downcast_ref::<T>().unwrap().clone();
let list = args[0].downcast_mut::<Array>().unwrap(); let list = args[0].downcast_mut::<Array>().unwrap();
while list.len() < len as usize { while list.len() < len as usize {
push(list, item.clone())?; push(list, item.clone())?;
} }
Ok(())
} else {
Ok(())
} }
Ok(())
} }
macro_rules! reg_op { macro_rules! reg_op {

View File

@ -222,32 +222,35 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
let len = *args[1].downcast_ref::< INT>().unwrap(); let len = *args[1].downcast_ref::< INT>().unwrap();
// Check if string will be over max size limit // Check if string will be over max size limit
if engine.max_string_size > 0 && len > 0 && (len as usize) > engine.max_string_size { #[cfg(not(feature = "unchecked"))]
{
if engine.max_string_size > 0 && len > 0 && (len as usize) > engine.max_string_size {
return Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
engine.max_string_size,
len as usize,
Position::none(),
)));
}
}
let ch = *args[2].downcast_ref::< char>().unwrap();
let s = args[0].downcast_mut::<ImmutableString>().unwrap();
let copy = s.make_mut();
for _ in 0..copy.chars().count() - len as usize {
copy.push(ch);
}
if engine.max_string_size > 0 && copy.len() > engine.max_string_size {
Err(Box::new(EvalAltResult::ErrorDataTooLarge( Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(), "Length of string".to_string(),
engine.max_string_size, engine.max_string_size,
len as usize, copy.len(),
Position::none(), Position::none(),
))) )))
} else { } else {
let ch = *args[2].downcast_ref::< char>().unwrap(); Ok(())
let s = args[0].downcast_mut::<ImmutableString>().unwrap();
let copy = s.make_mut();
for _ in 0..copy.chars().count() - len as usize {
copy.push(ch);
}
if engine.max_string_size > 0 && copy.len() > engine.max_string_size {
Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
engine.max_string_size,
copy.len(),
Position::none(),
)))
} else {
Ok(())
}
} }
}, },
); );