You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Fix RandomPCG::random(int, int) overflow bug
- Use int64_t for subtraction before converting to uint32_t - Don't add one to uint32_t max value for rand() bounds
This commit is contained in:
@@ -80,5 +80,15 @@ int RandomPCG::random(int p_from, int p_to) {
|
||||
if (p_from == p_to) {
|
||||
return p_from;
|
||||
}
|
||||
return int(rand(uint32_t(Math::abs(p_from - p_to)) + 1U)) + MIN(p_from, p_to);
|
||||
|
||||
int64_t min = MIN(p_from, p_to);
|
||||
int64_t max = MAX(p_from, p_to);
|
||||
uint32_t diff = static_cast<uint32_t>(max - min);
|
||||
|
||||
if (diff == UINT32_MAX) {
|
||||
// Can't add 1 to max uint32_t value for inclusive range, so call rand without passing bounds.
|
||||
return static_cast<int64_t>(rand()) + min;
|
||||
}
|
||||
|
||||
return static_cast<int64_t>(rand(diff + 1U)) + min;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user