fix(runtime): Prevent use-after-free in ScriptedDataConverter type changes (#13107) d7280b6962
Fixes a use-after-free crash in ScriptedDataConverter::storeData<T> that occurs when a two-way scripted data bind converts between different DataValue types (e.g. a Number↔String converter).

Co-authored-by: Philip Chung <philterdesign@gmail.com>
diff --git a/.rive_head b/.rive_head
index bdf7921..510832f 100644
--- a/.rive_head
+++ b/.rive_head
@@ -1 +1 @@
-a39edb59ab0a19bd9cfad66bbd956be625dd5273
+d7280b696290c3b9ae9f1ec58803a337fe6a6ee8
diff --git a/include/rive/scripted/scripted_data_converter.hpp b/include/rive/scripted/scripted_data_converter.hpp
index 9959703..da10c0b 100644
--- a/include/rive/scripted/scripted_data_converter.hpp
+++ b/include/rive/scripted/scripted_data_converter.hpp
@@ -22,6 +22,7 @@
         if (m_dataValue && !m_dataValue->is<T>())
         {
             delete m_dataValue;
+            m_dataValue = nullptr;
         }
         if (!m_dataValue)
         {
diff --git a/tests/unit_tests/runtime/scripting/scripting_converter_test.cpp b/tests/unit_tests/runtime/scripting/scripting_converter_test.cpp
index abc2c27..afcc0a0 100644
--- a/tests/unit_tests/runtime/scripting/scripting_converter_test.cpp
+++ b/tests/unit_tests/runtime/scripting/scripting_converter_test.cpp
@@ -3,6 +3,9 @@
 #include "scripting_test_utilities.hpp"
 #include "rive/animation/state_machine_instance.hpp"
 #include "rive/lua/rive_lua_libs.hpp"
+#include "rive/scripted/scripted_data_converter.hpp"
+#include "rive/data_bind/data_values/data_value_number.hpp"
+#include "rive/data_bind/data_values/data_value_string.hpp"
 #include "rive/viewmodel/viewmodel_instance_string.hpp"
 #include "rive_file_reader.hpp"
 
@@ -346,6 +349,61 @@
     }
 }
 
+TEST_CASE("scripted converter survives Number<->String direction flips",
+          "[scripting]")
+{
+    ScriptingTest vm(
+        R"(type NumberToStringConverter = {}
+
+function convert(self: NumberToStringConverter, input: DataValueNumber): DataValueString
+  local result = DataValue.string()
+  result.value = tostring(math.round(input.value))
+  return result
+end
+
+function reverseConvert(self: NumberToStringConverter, input: DataValueString): DataValueNumber
+  local result = DataValue.number()
+  result.value = tonumber(input.value) or 0
+  return result
+end
+
+return function(): Converter<NumberToStringConverter, DataValueNumber, DataValueString>
+  return {
+    convert = convert,
+    reverseConvert = reverseConvert,
+  }
+end
+)");
+
+    // ScriptingTest leaves the factory function on top of the stack;
+    // ensureScriptInitialized consumes it to build m_self.
+    ScriptedDataConverter converter;
+    // Enable convert() (bit 10) and reverseConvert() (bit 11).
+    converter.implementedMethods((1 << 10) | (1 << 11));
+    REQUIRE(converter.dataConverts());
+    REQUIRE(converter.dataReverseConverts());
+    REQUIRE(converter.ensureScriptInitialized(vm.vm()));
+
+    // Flip directions repeatedly on the same converter instance. Each flip
+    // changes the type storeData<T> must produce; the buggy version crashed
+    // on the second call.
+    for (int i = 0; i < 3; i++)
+    {
+        DataValueNumber number(20000 + i);
+        auto forward = converter.convert(&number, nullptr);
+        REQUIRE(forward != nullptr);
+        REQUIRE(forward->is<DataValueString>());
+        CHECK(forward->as<DataValueString>()->value() ==
+              std::to_string(20000 + i));
+
+        DataValueString text(std::to_string(12345 + i));
+        auto reverse = converter.reverseConvert(&text, nullptr);
+        REQUIRE(reverse != nullptr);
+        REQUIRE(reverse->is<DataValueNumber>());
+        CHECK(reverse->as<DataValueNumber>()->value() == (float)(12345 + i));
+    }
+}
+
 TEST_CASE("scripted string converter", "[silver]")
 {
     rive::SerializingFactory silver;