Fix panic when string character index is OOB.
This commit is contained in:
parent
de4120b3bc
commit
fc99b981a1
@ -1057,15 +1057,25 @@ impl Engine {
|
||||
.as_int()
|
||||
.map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?;
|
||||
|
||||
let num_chars = s.chars().count();
|
||||
|
||||
if index >= 0 {
|
||||
let index = index as usize;
|
||||
let ch = s.chars().nth(index).unwrap();
|
||||
Ok(Target::StringChar(Box::new((val, index, ch.into()))))
|
||||
let ch = s.chars().nth(index as usize).ok_or_else(|| {
|
||||
Box::new(EvalAltResult::ErrorStringBounds(
|
||||
s.chars().count(),
|
||||
index,
|
||||
idx_pos,
|
||||
))
|
||||
})?;
|
||||
|
||||
Ok(Target::StringChar(Box::new((
|
||||
val,
|
||||
index as usize,
|
||||
ch.into(),
|
||||
))))
|
||||
} else {
|
||||
Err(Box::new(EvalAltResult::ErrorStringBounds(
|
||||
num_chars, index, idx_pos,
|
||||
s.chars().count(),
|
||||
index,
|
||||
idx_pos,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
@ -435,9 +435,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
|
||||
.unwrap_or_else(|| Expr::Unit(pos))
|
||||
}
|
||||
// string[int]
|
||||
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _))
|
||||
if i >= 0 && (i as usize) < s.chars().count() =>
|
||||
{
|
||||
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if i >= 0 && (i as usize) < s.chars().count() => {
|
||||
// String literal indexing - get the character
|
||||
state.set_dirty();
|
||||
Expr::CharConstant(s.chars().nth(i as usize).expect("should get char"), pos)
|
||||
@ -550,11 +548,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
|
||||
optimize_expr(lhs, state)
|
||||
}
|
||||
// lhs || rhs
|
||||
(lhs, rhs) => Expr::Or(
|
||||
Box::new(optimize_expr(lhs, state)),
|
||||
Box::new(optimize_expr(rhs, state)),
|
||||
pos
|
||||
),
|
||||
(lhs, rhs) => Expr::Or(Box::new(optimize_expr(lhs, state)), Box::new(optimize_expr(rhs, state)), pos),
|
||||
},
|
||||
|
||||
// Do not call some special keywords
|
||||
|
Loading…
Reference in New Issue
Block a user