godot/core
Sai Nane e6a7c63125 binder_common: Fix uninitialized marshalling
C# uses `long`s to access many native values. With `PtrToArg<m_enum>` and
`PtrToArg<bitfield<m_enum>>` this isn't a problem, as C++ code converts
through a `*(int64_t*)` cast in assignment, so all 64-bits are initialized.

However, with `PtrToArg<char32_t>`, value assignment happens through an
`*(int *)` cast, leaving 32 bits uninitialized where `int` is 32 bits. On
platforms where `int` is 16 bits, there are presumably 48 bits uninitialized,
though there are very few platforms where this is still the case.

The easiest way to see the practical effects of this is by looking at
`EventInputKey.Unicode`:

```csharp
    public override void _Input(InputEvent @event) {
        if (@event is InputEventKey keyEvent) {
            if (keyEvent.IsPressed() && !keyEvent.Echo) {
                var raw = keyEvent.Unicode;
                var value = raw & 0xffffffff;
                GD.Print($"Key pressed: raw: {raw}; masked: {(char) value} ({value})");
            }
        }
    }
```

Pressing 'a' emits the following line:
```
Key pressed: raw: -3617008645356650399; masked: a (97)
```

Examining execution flow in gdb shows this conversion going through the
following line:
```
PtrToArg<char32_t>::encode (p_ptr=0x7ffcd5bb4b18, p_val=97 U'a') at ./core/variant/binder_common.h:221
221			*(int *)p_ptr = p_val;
```

Here, `p_val` is still 97, which is the value `InputEventKey.Unicode`
is expected to have. After assignment, `p *(int64_t *)0x7ffcd5bb4b18` displays
`-3617008645356650399`, with only the lower 32 bits being properly assigned,
and is the value we see from C#.

With this patch applied, the above testing `_Input` now prints:
```
Key pressed: raw: 97; masked: a (97)
```

Thank you to blujay1269 for asking about an unexpected value they saw in
`EventInputKey.Unicode`, which prompted this investigation.
2024-08-09 05:15:54 +00:00
..
config Replace .NET detection code with ClassDB::class_exists("CSharpScript"). 2024-07-23 23:46:14 +03:00
crypto [Crypto] Expose TLSOptions getters 2024-06-14 22:33:10 +02:00
debugger Fix Game window stops responding when debugger pauses 2024-07-10 13:38:57 -07:00
error
extension Merge pull request #94373 from maiself/fix-library-path-as-abs 2024-07-17 12:24:07 +02:00
input Merge pull request #94413 from rburing/fix_action_press_tick 2024-07-17 11:44:31 +02:00
io [ResourceLoader] Add check to prevent double free crashes. 2024-08-06 08:31:03 +03:00
math Add a check to prevent user to call AStarGrid2D::update when its not needed 2024-07-06 14:59:38 +03:00
object Fix global class cache file not present when no class name 2024-07-31 16:57:25 -04:00
os Fix Game window stops responding when debugger pauses 2024-07-10 13:38:57 -07:00
string Remove editor pseudolocalization debug feature 2024-06-24 17:08:26 +02:00
templates Fixup recent changes to threading concerns 2024-07-16 11:03:02 +02:00
variant binder_common: Fix uninitialized marshalling 2024-08-09 05:15:54 +00:00
core_bind.cpp [Crypto] Expose OS.get_entropy 2024-06-17 12:02:37 +02:00
core_bind.h [Crypto] Expose OS.get_entropy 2024-06-17 12:02:37 +02:00
core_builders.py Update pre-commit hooks configuration to use ruff instead of black 2024-05-21 18:02:29 -05:00
core_constants.cpp
core_constants.h
core_globals.cpp
core_globals.h
core_string_names.cpp Cleanup unused/underused singleton StringNames 2024-05-14 18:18:29 +02:00
core_string_names.h Cleanup unused/underused singleton StringNames 2024-05-14 18:18:29 +02:00
doc_data.cpp
doc_data.h
register_core_types.cpp fixed cleanup order in unregister_core_types() 2024-06-18 00:44:01 -07:00
register_core_types.h
SCsub SCons: Fix mono dependency 2024-06-01 16:37:54 -05:00
typedefs.h
version.h