git.delta.rocks / jrsonnet / refs/commits / 321e7ee3e21c

difftreelog

fix string index bounds check

Yaroslav Bolyukin2022-04-22parent: #cb29d29.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
3636
37 #[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),
3941
40 #[error("assert failed: {0}")]42 #[error("assert failed: {0}")]
41 AssertionFailed(IStr),43 AssertionFailed(IStr),
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
589 n.value_type(),589 n.value_type(),
590 )),590 )),
591591
592 (Val::Str(s), Val::Num(n)) => Val::Str(592 (Val::Str(s), Val::Num(n)) => Val::Str({
593 s.chars()593 let v: IStr = s
594 .chars()
594 .skip(n as usize)595 .skip(n as usize)
595 .take(1)596 .take(1)
596 .collect::<String>()597 .collect::<String>()
597 .into(),598 .into();
599 if v.is_empty() {
600 let size = s.chars().count();
601 throw!(StringBoundsError(n as usize, size))
602 }
603 v
598 ),604 }),
599 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(605 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
600 ValType::Str,606 ValType::Str,
601 ValType::Num,607 ValType::Num,