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()
|
.as_int()
|
||||||
.map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?;
|
.map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?;
|
||||||
|
|
||||||
let num_chars = s.chars().count();
|
|
||||||
|
|
||||||
if index >= 0 {
|
if index >= 0 {
|
||||||
let index = index as usize;
|
let ch = s.chars().nth(index as usize).ok_or_else(|| {
|
||||||
let ch = s.chars().nth(index).unwrap();
|
Box::new(EvalAltResult::ErrorStringBounds(
|
||||||
Ok(Target::StringChar(Box::new((val, index, ch.into()))))
|
s.chars().count(),
|
||||||
|
index,
|
||||||
|
idx_pos,
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(Target::StringChar(Box::new((
|
||||||
|
val,
|
||||||
|
index as usize,
|
||||||
|
ch.into(),
|
||||||
|
))))
|
||||||
} else {
|
} else {
|
||||||
Err(Box::new(EvalAltResult::ErrorStringBounds(
|
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))
|
.unwrap_or_else(|| Expr::Unit(pos))
|
||||||
}
|
}
|
||||||
// string[int]
|
// string[int]
|
||||||
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _))
|
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if i >= 0 && (i as usize) < s.chars().count() => {
|
||||||
if i >= 0 && (i as usize) < s.chars().count() =>
|
|
||||||
{
|
|
||||||
// String literal indexing - get the character
|
// String literal indexing - get the character
|
||||||
state.set_dirty();
|
state.set_dirty();
|
||||||
Expr::CharConstant(s.chars().nth(i as usize).expect("should get char"), pos)
|
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)
|
optimize_expr(lhs, state)
|
||||||
}
|
}
|
||||||
// lhs || rhs
|
// lhs || rhs
|
||||||
(lhs, rhs) => Expr::Or(
|
(lhs, rhs) => Expr::Or(Box::new(optimize_expr(lhs, state)), Box::new(optimize_expr(rhs, state)), pos),
|
||||||
Box::new(optimize_expr(lhs, state)),
|
|
||||||
Box::new(optimize_expr(rhs, state)),
|
|
||||||
pos
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Do not call some special keywords
|
// Do not call some special keywords
|
||||||
|
Loading…
Reference in New Issue
Block a user