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
--- 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,