Skip to content

Commit

Permalink
Fix inclusive range bug
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jun 6, 2024
1 parent 307afe2 commit d35fdde
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Bug fixes
* More data-race conditions are caught and returned as errors instead of panicking.
* Missing `min` and `max` functions where both operands are floats or `Decimal` are added.
* Fixed stack overflow when calling closures recursively (thanks [`@MageWeiG`](https://github.com/MageWeiG) [880](https://github.com/rhaiscript/rhai/issues/880)).
* `Engine::call_fn` and `Engine::call_fn_with_options` now correctly use the `AST`'s `source` field.
* (Fuzzing) Fixed crash when using `..=` in strings.

New features
------------
Expand Down
4 changes: 2 additions & 2 deletions src/packages/string_more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ mod string_functions {
range: InclusiveRange,
) -> ImmutableString {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
let end = INT::min(INT::max(*range.end(), start), INT::MAX - 1);
sub_string(ctx, string, start, end - start + 1)
}
/// Copy a portion of the string and return it as a new string.
Expand Down Expand Up @@ -1042,7 +1042,7 @@ mod string_functions {
range: InclusiveRange,
) {
let start = INT::max(*range.start(), 0);
let end = INT::max(*range.end(), start);
let end = INT::min(INT::max(*range.end(), start), INT::MAX - 1);
crop(ctx, string, start, end - start + 1);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ fn test_string_index() {
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y[0..18]"#).unwrap(), "hello");
// overflow negative index
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y[2..-18]"#).unwrap(), "");
// overflow
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y[..=] = "x"; y"#).unwrap(), "x");
// overflow
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y[..] = "x"; y"#).unwrap(), "x");
// overflow
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y.crop(..); y"#).unwrap(), "hello");
// overflow
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y.crop(..=); y"#).unwrap(), "hello");

// mut slice index
assert_eq!(engine.eval::<String>(r#"let y = "hello"; y[1] = 'i'; y"#).unwrap(), "hillo");
Expand Down

0 comments on commit d35fdde

Please sign in to comment.