diff options
author | Ninni Pipping <over999ships@gmail.com> | 2023-07-06 15:03:17 +0200 |
---|---|---|
committer | Ninni Pipping <over999ships@gmail.com> | 2023-07-07 23:19:42 +0200 |
commit | 7752a0d8d13e1052e6cb0f0199bd6cbb20e3abe8 (patch) | |
tree | 969225a0f5f9b66c4152ebd83d95682b6a08dde5 /core/variant/array.cpp | |
parent | c3b0a92c3cd9a219c1b1776b48c147f1d0602f07 (diff) | |
download | redot-engine-7752a0d8d13e1052e6cb0f0199bd6cbb20e3abe8.tar.gz |
Fix range error for `Array.slice`
Diffstat (limited to 'core/variant/array.cpp')
-rw-r--r-- | core/variant/array.cpp | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp index 5215142dd3..5a0ded6c01 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -454,17 +454,21 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { const int s = size(); - int begin = CLAMP(p_begin, -s, s); + if (s == 0 || (p_begin < -s && p_step < 0) || (p_begin >= s && p_step > 0)) { + return result; + } + + int begin = CLAMP(p_begin, -s, s - 1); if (begin < 0) { begin += s; } - int end = CLAMP(p_end, -s, s); + int end = CLAMP(p_end, -s - 1, s); if (end < 0) { end += s; } - ERR_FAIL_COND_V_MSG(p_step > 0 && begin > end, result, "Slice is positive, but bounds is decreasing."); - ERR_FAIL_COND_V_MSG(p_step < 0 && begin < end, result, "Slice is negative, but bounds is increasing."); + ERR_FAIL_COND_V_MSG(p_step > 0 && begin > end, result, "Slice step is positive, but bounds are decreasing."); + ERR_FAIL_COND_V_MSG(p_step < 0 && begin < end, result, "Slice step is negative, but bounds are increasing."); int result_size = (end - begin) / p_step + (((end - begin) % p_step != 0) ? 1 : 0); result.resize(result_size); |