diff options
author | Yuri Sizov <11782833+YuriSizov@users.noreply.github.com> | 2023-07-17 16:48:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-17 16:48:48 +0200 |
commit | 3d04a22d7cb8b5756ddb314cbccd799630e18d47 (patch) | |
tree | 1ea23350692f95c075deb6e1871b7b1747e57ef6 /core/variant/array.cpp | |
parent | b7c3998669bc58c8a4a561986e6b99f99cbaf713 (diff) | |
parent | 7752a0d8d13e1052e6cb0f0199bd6cbb20e3abe8 (diff) | |
download | redot-engine-3d04a22d7cb8b5756ddb314cbccd799630e18d47.tar.gz |
Merge pull request #79103 from AThousandShips/array_slice_range
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); |