difftreelog
fix string index bounds check
in: master
2 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth363637 #[error("array out of bounds: {0} is not within [0,{1})")]37 #[error("array out of bounds: {0} is not within [0,{1})")]38 ArrayBoundsError(usize, usize),38 ArrayBoundsError(usize, usize),39 #[error("string out of bounds: {0} is not within [0,{1})")]40 StringBoundsError(usize, usize),394140 #[error("assert failed: {0}")]42 #[error("assert failed: {0}")]41 AssertionFailed(IStr),43 AssertionFailed(IStr),crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -589,13 +589,19 @@
n.value_type(),
)),
- (Val::Str(s), Val::Num(n)) => Val::Str(
- s.chars()
+ (Val::Str(s), Val::Num(n)) => Val::Str({
+ let v: IStr = s
+ .chars()
.skip(n as usize)
.take(1)
.collect::<String>()
- .into(),
- ),
+ .into();
+ if v.is_empty() {
+ let size = s.chars().count();
+ throw!(StringBoundsError(n as usize, size))
+ }
+ v
+ }),
(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
ValType::Str,
ValType::Num,