Skip to content

Commit 11ba78a

Browse files
committed
Merge pull request #109376 from bruvzg/range_range
[GDScript] Fix `range` helper method using 32-bit ints for arguments.
2 parents 80a219a + b21e652 commit 11ba78a

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

modules/gdscript/gdscript_utility_functions.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,19 @@ struct GDScriptUtilityFunctionsDefinitions {
139139
case 1: {
140140
DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
141141

142-
int count = *p_args[0];
142+
int64_t count = *p_args[0];
143143

144144
Array arr;
145145
if (count <= 0) {
146146
*r_ret = arr;
147147
return;
148148
}
149149

150+
GDFUNC_FAIL_COND_MSG(count > INT32_MAX, RTR("Range too big."));
150151
Error err = arr.resize(count);
151152
GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
152153

153-
for (int i = 0; i < count; i++) {
154+
for (int64_t i = 0; i < count; i++) {
154155
arr[i] = i;
155156
}
156157

@@ -160,19 +161,20 @@ struct GDScriptUtilityFunctionsDefinitions {
160161
DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
161162
DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
162163

163-
int from = *p_args[0];
164-
int to = *p_args[1];
164+
int64_t from = *p_args[0];
165+
int64_t to = *p_args[1];
165166

166167
Array arr;
167168
if (from >= to) {
168169
*r_ret = arr;
169170
return;
170171
}
171172

173+
GDFUNC_FAIL_COND_MSG(to - from > INT32_MAX, RTR("Range too big."));
172174
Error err = arr.resize(to - from);
173175
GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
174176

175-
for (int i = from; i < to; i++) {
177+
for (int64_t i = from; i < to; i++) {
176178
arr[i - from] = i;
177179
}
178180

@@ -183,9 +185,9 @@ struct GDScriptUtilityFunctionsDefinitions {
183185
DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
184186
DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT);
185187

186-
int from = *p_args[0];
187-
int to = *p_args[1];
188-
int incr = *p_args[2];
188+
int64_t from = *p_args[0];
189+
int64_t to = *p_args[1];
190+
int64_t incr = *p_args[2];
189191

190192
VALIDATE_ARG_CUSTOM(2, Variant::INT, incr == 0, RTR("Step argument is zero!"));
191193

@@ -200,24 +202,25 @@ struct GDScriptUtilityFunctionsDefinitions {
200202
}
201203

202204
// Calculate how many.
203-
int count = 0;
205+
int64_t count = 0;
204206
if (incr > 0) {
205207
count = Math::division_round_up(to - from, incr);
206208
} else {
207209
count = Math::division_round_up(from - to, -incr);
208210
}
209211

212+
GDFUNC_FAIL_COND_MSG(count > INT32_MAX, RTR("Range too big."));
210213
Error err = arr.resize(count);
211214
GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
212215

213216
if (incr > 0) {
214-
int idx = 0;
215-
for (int i = from; i < to; i += incr) {
217+
int64_t idx = 0;
218+
for (int64_t i = from; i < to; i += incr) {
216219
arr[idx++] = i;
217220
}
218221
} else {
219-
int idx = 0;
220-
for (int i = from; i > to; i += incr) {
222+
int64_t idx = 0;
223+
for (int64_t i = from; i > to; i += incr) {
221224
arr[idx++] = i;
222225
}
223226
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func test():
2+
# GH-109376
3+
# Note: unlike "for range", which is iterated in place, this helper function generates an array of all values in range.
4+
var result
5+
6+
result = range(2147483640, 2147483647) # Range below 32-bit size limit.
7+
print(result)
8+
9+
result = range(2147483640, 2147483647 + 1) # Range 1 over 32-bit size limit.
10+
print(result)
11+
12+
result = range(9922147483640, 9922147483647) # Range significantly over 32-bit size limit.
13+
print(result)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GDTEST_OK
2+
[2147483640, 2147483641, 2147483642, 2147483643, 2147483644, 2147483645, 2147483646]
3+
[2147483640, 2147483641, 2147483642, 2147483643, 2147483644, 2147483645, 2147483646, 2147483647]
4+
[9922147483640, 9922147483641, 9922147483642, 9922147483643, 9922147483644, 9922147483645, 9922147483646]

0 commit comments

Comments
 (0)