Repository: godot
Issue: Crash when manipulating Gradient inside NoiseTexture2D
Labels: bug,needs testing,crash,topic:2d

### Godot version

4.0.beta.custom_build. f3e6750a7

### System information

Ubuntu 22.04 - Radeon RX 570, Gnome shell 42 X11

### Issue description

```
=================================================================
==79796==ERROR: AddressSanitizer: heap-use-after-free on address 0x60d0029f5c04 at pc 0x000003ed3896 bp 0x7fe795bc93d0 sp 0x7fe795bc93c0
READ of size 4 at 0x60d0029f5c04 thread T56
    #0 0x3ed3895 in Gradient::get_color_at_offset(float) scene/resources/gradient.h:116
    #1 0x846169f in NoiseTexture2D::_modulate_with_gradient(Ref<Image>, Ref<Gradient>) modules/noise/noise_texture_2d.cpp:184
    #2 0x8460679 in NoiseTexture2D::_generate_texture() modules/noise/noise_texture_2d.cpp:163
    #3 0x845e9b4 in NoiseTexture2D::_thread_function(void*) modules/noise/noise_texture_2d.cpp:135
    #4 0x1d3b071d in Thread::callback(Thread*, Thread::Settings const&, void (*)(void*), void*) core/os/thread.cpp:65
    #5 0x1d3b4711 in void std::__invoke_impl<void, void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*>(std::__invoke_other, void (*&&)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*&&, Thread::Settings&&, void (*&&)(void*), void*&&) /usr/include/c++/11/bits/invoke.h:61
    #6 0x1d3b4323 in std::__invoke_result<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*>::type std::__invoke<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*>(void (*&&)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*&&, Thread::Settings&&, void (*&&)(void*), void*&&) /usr/include/c++/11/bits/invoke.h:96
    #7 0x1d3b3d93 in void std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> >::_M_invoke<0ul, 1ul, 2ul, 3ul, 4ul>(std::_Index_tuple<0ul, 1ul, 2ul, 3ul, 4ul>) /usr/include/c++/11/bits/std_thread.h:253
    #8 0x1d3b39ed in std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> >::operator()() /usr/include/c++/11/bits/std_thread.h:260
    #9 0x1d3b39a1 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> > >::_M_run() /usr/include/c++/11/bits/std_thread.h:211
    #10 0x205e60e3 in execute_native_thread_routine (/usr/bin/godot4s+0x205e60e3)
    #11 0x7fe808f95b42 in start_thread nptl/pthread_create.c:442
    #12 0x7fe8090279ff  (/lib/x86_64-linux-gnu/libc.so.6+0x1269ff)

0x60d0029f5c04 is located 116 bytes inside of 144-byte region [0x60d0029f5b90,0x60d0029f5c20)
freed by thread T0 here:
    #0 0x7fe8098f9c18 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x1d39eaa9 in Memory::realloc_static(void*, unsigned long, bool) core/os/memory.cpp:129
    #2 0x40f3b54 in Error CowData<Gradient::Point>::resize<false>(int) core/templates/cowdata.h:296
    #3 0x40a5fe1 in Vector<Gradient::Point>::resize(int) core/templates/vector.h:91
    #4 0x174fa9b7 in Gradient::set_offsets(Vector<float> const&) scene/resources/gradient.cpp:111
    #5 0x17514bff in void call_with_variant_args_helper<__UnexistingClass, Vector<float> const&, 0ul>(__UnexistingClass*, void (__UnexistingClass::*)(Vector<float> const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) core/variant/binder_common.h:262
    #6 0x1751166a in void call_with_variant_args_dv<__UnexistingClass, Vector<float> const&>(__UnexistingClass*, void (__UnexistingClass::*)(Vector<float> const&), Variant const**, int, Callable::CallError&, Vector<Variant> const&) core/variant/binder_common.h:409
    #7 0x1750e2a8 in MethodBindT<Vector<float> const&>::call(Object*, Variant const**, int, Callable::CallError&) const core/object/method_bind.h:320
    #8 0x1eba130a in Object::callp(StringName const&, Variant const**, int, Callable::CallError&) core/object/object.cpp:733
    #9 0x1dfaa195 in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:62
    #10 0x1dfba75c in CallableCustomBind::call(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable_bind.cpp:99
    #11 0x1dfa975c in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:50
    #12 0x1eca5c1b in UndoRedo::_process_operation_list(List<UndoRedo::Operation, DefaultAllocator>::Element*) core/object/undo_redo.cpp:318
    #13 0x1ec9ad5f in UndoRedo::_redo(bool) core/object/undo_redo.cpp:75
    #14 0x1eca4aee in UndoRedo::commit_action(bool) core/object/undo_redo.cpp:297
    #15 0xe0bbb18 in EditorUndoRedoManager::commit_action(bool) editor/editor_undo_redo_manager.cpp:234
    #16 0x12b5efd3 in GradientEditor::_ramp_changed() editor/plugins/gradient_editor.cpp:110
    #17 0x12b85c50 in void call_with_variant_args_helper<GradientEditor>(GradientEditor*, void (GradientEditor::*)(), Variant const**, Callable::CallError&, IndexSequence<>) core/variant/binder_common.h:262
    #18 0x12b851f5 in void call_with_variant_args<GradientEditor>(GradientEditor*, void (GradientEditor::*)(), Variant const**, int, Callable::CallError&) core/variant/binder_common.h:376
    #19 0x12b84af2 in CallableCustomMethodPointer<GradientEditor>::call(Variant const**, int, Variant&, Callable::CallError&) const core/object/callable_method_pointer.h:104
    #20 0x1dfa975c in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:50
    #21 0x1ebacf1c in Object::emit_signalp(StringName const&, Variant const**, int) core/object/object.cpp:1046
    #22 0x7f86a0e in Error Object::emit_signal<>(StringName const&) core/object/object.h:868
    #23 0x12b69128 in GradientEditor::gui_input(Ref<InputEvent> const&) editor/plugins/gradient_editor.cpp:310
    #24 0x140bbe3d in Control::_call_gui_input(Ref<InputEvent> const&) scene/gui/control.cpp:1713
    #25 0x13a95d3c in Viewport::_gui_call_input(Control*, Ref<InputEvent> const&) scene/main/viewport.cpp:1326
    #26 0x13a9f598 in Viewport::_gui_input_event(Ref<InputEvent>) scene/main/viewport.cpp:1559
    #27 0x13adc38a in Viewport::push_input(Ref<InputEvent> const&, bool) scene/main/viewport.cpp:2796
    #28 0x13c5d46a in Window::_window_input(Ref<InputEvent> const&) scene/main/window.cpp:1089
    #29 0x13d3afda in void call_with_variant_args_helper<Window, Ref<InputEvent> const&, 0ul>(Window*, void (Window::*)(Ref<InputEvent> const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) core/variant/binder_common.h:262

previously allocated by thread T0 here:
    #0 0x7fe8098f9c18 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x1d39eaa9 in Memory::realloc_static(void*, unsigned long, bool) core/os/memory.cpp:129
    #2 0x40f412f in Error CowData<Gradient::Point>::resize<false>(int) core/templates/cowdata.h:326
    #3 0x40a5fe1 in Vector<Gradient::Point>::resize(int) core/templates/vector.h:91
    #4 0x174fa9b7 in Gradient::set_offsets(Vector<float> const&) scene/resources/gradient.cpp:111
    #5 0x17514bff in void call_with_variant_args_helper<__UnexistingClass, Vector<float> const&, 0ul>(__UnexistingClass*, void (__UnexistingClass::*)(Vector<float> const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) core/variant/binder_common.h:262
    #6 0x1751166a in void call_with_variant_args_dv<__UnexistingClass, Vector<float> const&>(__UnexistingClass*, void (__UnexistingClass::*)(Vector<float> const&), Variant const**, int, Callable::CallError&, Vector<Variant> const&) core/variant/binder_common.h:409
    #7 0x1750e2a8 in MethodBindT<Vector<float> const&>::call(Object*, Variant const**, int, Callable::CallError&) const core/object/method_bind.h:320
    #8 0x1eba130a in Object::callp(StringName const&, Variant const**, int, Callable::CallError&) core/object/object.cpp:733
    #9 0x1dfaa195 in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:62
    #10 0x1dfba75c in CallableCustomBind::call(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable_bind.cpp:99
    #11 0x1dfa975c in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:50
    #12 0x1eca5c1b in UndoRedo::_process_operation_list(List<UndoRedo::Operation, DefaultAllocator>::Element*) core/object/undo_redo.cpp:318
    #13 0x1ec9ad5f in UndoRedo::_redo(bool) core/object/undo_redo.cpp:75
    #14 0x1eca4aee in UndoRedo::commit_action(bool) core/object/undo_redo.cpp:297
    #15 0xe0bbb18 in EditorUndoRedoManager::commit_action(bool) editor/editor_undo_redo_manager.cpp:234
    #16 0x12b5efd3 in GradientEditor::_ramp_changed() editor/plugins/gradient_editor.cpp:110
    #17 0x12b85c50 in void call_with_variant_args_helper<GradientEditor>(GradientEditor*, void (GradientEditor::*)(), Variant const**, Callable::CallError&, IndexSequence<>) core/variant/binder_common.h:262
    #18 0x12b851f5 in void call_with_variant_args<GradientEditor>(GradientEditor*, void (GradientEditor::*)(), Variant const**, int, Callable::CallError&) core/variant/binder_common.h:376
    #19 0x12b84af2 in CallableCustomMethodPointer<GradientEditor>::call(Variant const**, int, Variant&, Callable::CallError&) const core/object/callable_method_pointer.h:104
    #20 0x1dfa975c in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:50
    #21 0x1ebacf1c in Object::emit_signalp(StringName const&, Variant const**, int) core/object/object.cpp:1046
    #22 0x7f86a0e in Error Object::emit_signal<>(StringName const&) core/object/object.h:868
    #23 0x12b6491f in GradientEditor::gui_input(Ref<InputEvent> const&) editor/plugins/gradient_editor.cpp:217
    #24 0x140bbe3d in Control::_call_gui_input(Ref<InputEvent> const&) scene/gui/control.cpp:1713
    #25 0x13a95d3c in Viewport::_gui_call_input(Control*, Ref<InputEvent> const&) scene/main/viewport.cpp:1326
    #26 0x13a9f598 in Viewport::_gui_input_event(Ref<InputEvent>) scene/main/viewport.cpp:1559
    #27 0x13adc38a in Viewport::push_input(Ref<InputEvent> const&, bool) scene/main/viewport.cpp:2796
    #28 0x13c5d46a in Window::_window_input(Ref<InputEvent> const&) scene/main/window.cpp:1089
    #29 0x13d3afda in void call_with_variant_args_helper<Window, Ref<InputEvent> const&, 0ul>(Window*, void (Window::*)(Ref<InputEvent> const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) core/variant/binder_common.h:262

Thread T56 created by T0 here:
    #0 0x7fe80989d685 in __interceptor_pthread_create ../../../../src/libsanitizer/asan/asan_interceptors.cpp:216
    #1 0x205e61b8 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) (/usr/bin/godot4s+0x205e61b8)
    #2 0x1d3b0c39 in Thread::start(void (*)(void*), void*, Thread::Settings const&) core/os/thread.cpp:82
    #3 0x845e4b7 in NoiseTexture2D::_thread_done(Ref<Image> const&) modules/noise/noise_texture_2d.cpp:128
    #4 0x848d17c in void call_with_variant_args_helper<__UnexistingClass, Ref<Image> const&, 0ul>(__UnexistingClass*, void (__UnexistingClass::*)(Ref<Image> const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) core/variant/binder_common.h:262
    #5 0x8488e63 in void call_with_variant_args_dv<__UnexistingClass, Ref<Image> const&>(__UnexistingClass*, void (__UnexistingClass::*)(Ref<Image> const&), Variant const**, int, Callable::CallError&, Vector<Variant> const&) core/variant/binder_common.h:409
    #6 0x848300a in MethodBindT<Ref<Image> const&>::call(Object*, Variant const**, int, Callable::CallError&) const core/object/method_bind.h:320
    #7 0x1eba130a in Object::callp(StringName const&, Variant const**, int, Callable::CallError&) core/object/object.cpp:733
    #8 0x1dfaa195 in Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const core/variant/callable.cpp:62
    #9 0x1eb7a3ff in MessageQueue::_call_function(Callable const&, Variant const*, int, bool) core/object/message_queue.cpp:229
    #10 0x1eb7b4f2 in MessageQueue::flush() core/object/message_queue.cpp:275
    #11 0x13976142 in SceneTree::process(double) scene/main/scene_tree.cpp:461
    #12 0x2935dc2 in Main::iteration() main/main.cpp:3196
    #13 0x2714b89 in OS_LinuxBSD::run() platform/linuxbsd/os_linuxbsd.cpp:878
    #14 0x26f1b44 in main platform/linuxbsd/godot_linuxbsd.cpp:73
    #15 0x7fe808f2ad8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: heap-use-after-free scene/resources/gradient.h:116 in Gradient::get_color_at_offset(float)

```

### Steps to reproduce


https://user-images.githubusercontent.com/41945903/205903226-4b0786ee-40b6-4149-96fd-5d968fbc28a8.mp4



### Minimal reproduction project

N/A