From 5bc29947ea04a21ef0c7cd2e6c857a7808ef6763 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Sat, 29 Jun 2024 12:25:25 -0400 Subject: [PATCH] GH-421 Fix 4.3 stack allocation crash issues with RefCounted --- src/common/guid.cpp | 10 +++++----- src/script/nodes/flow_control/chance.cpp | 7 +++++-- src/script/nodes/flow_control/random.cpp | 7 +++++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/common/guid.cpp b/src/common/guid.cpp index 8dc2c0e3..09ab7a63 100644 --- a/src/common/guid.cpp +++ b/src/common/guid.cpp @@ -66,12 +66,12 @@ String Guid::to_string() const Guid Guid::create_guid() { - RandomNumberGenerator rng; + Ref rng(memnew(RandomNumberGenerator)); - uint32_t a = rng.randi(); - uint32_t b = rng.randi(); - uint32_t c = rng.randi(); - uint32_t d = rng.randi(); + uint32_t a = rng->randi(); + uint32_t b = rng->randi(); + uint32_t c = rng->randi(); + uint32_t d = rng->randi(); // The 4 bits of digit M indicate the GUID version, and the 1-3 most significant bits // of digit N indicate the UUID variant. diff --git a/src/script/nodes/flow_control/chance.cpp b/src/script/nodes/flow_control/chance.cpp index d40ef94d..f04d84a7 100644 --- a/src/script/nodes/flow_control/chance.cpp +++ b/src/script/nodes/flow_control/chance.cpp @@ -19,13 +19,16 @@ class OScriptNodeChanceInstance : public OScriptNodeInstance { DECLARE_SCRIPT_NODE_INSTANCE(OScriptNodeChance); - RandomNumberGenerator _random; + Ref _random; int _chance{ 0 }; public: int step(OScriptExecutionContext& p_context) override { - const int _calculated_chance = _random.randi_range(0, 100); + if (!_random.is_valid()) + _random.instantiate(); + + const int _calculated_chance = _random->randi_range(0, 100); return _calculated_chance <= _chance ? 0 : 1; } }; diff --git a/src/script/nodes/flow_control/random.cpp b/src/script/nodes/flow_control/random.cpp index 5258efc9..94f6f9bb 100644 --- a/src/script/nodes/flow_control/random.cpp +++ b/src/script/nodes/flow_control/random.cpp @@ -20,7 +20,7 @@ class OScriptNodeRandomInstance : public OScriptNodeInstance { DECLARE_SCRIPT_NODE_INSTANCE(OScriptNodeRandom); - RandomNumberGenerator _random; + Ref _random; int _possibilities{ 0 }; public: @@ -29,7 +29,10 @@ class OScriptNodeRandomInstance : public OScriptNodeInstance if (_possibilities == 0) return -1; - return _random.randi_range(0, _possibilities - 1); + if (!_random.is_valid()) + _random.instantiate(); + + return _random->randi_range(0, _possibilities - 1); } };