1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-06 12:20:30 +00:00

Fix range error for Array.slice

(cherry picked from commit 7752a0d8d1)
This commit is contained in:
Ninni Pipping
2023-07-06 15:03:17 +02:00
committed by Yuri Sizov
parent b0ceeb2341
commit a31b866793
3 changed files with 32 additions and 9 deletions

View File

@@ -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);