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
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -36,6 +36,8 @@
 
 	#[error("array out of bounds: {0} is not within [0,{1})")]
 	ArrayBoundsError(usize, usize),
+	#[error("string out of bounds: {0} is not within [0,{1})")]
+	StringBoundsError(usize, usize),
 
 	#[error("assert failed: {0}")]
 	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,