godot/core/variant
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
..
array.cpp Core: Improve vformat error reporting on sprintf failure 2024-07-04 10:54:55 +02:00
array.h [Core] Fix incorrect comparison for Array const iterator 2024-04-13 17:32:33 +02:00
binder_common.h binder_common: Fix uninitialized marshalling 2024-08-09 05:15:54 +00:00
callable_bind.cpp Add methods to get argument count of methods 2024-03-10 11:02:43 +01:00
callable_bind.h Add methods to get argument count of methods 2024-03-10 11:02:43 +01:00
callable.cpp fix callable not clearing freed pointer 2024-06-02 21:22:16 -07:00
callable.h Add methods to get argument count of methods 2024-03-10 11:02:43 +01:00
container_type_validate.h [Core] Replace ERR_FAIL_COND with ERR_FAIL_NULL where applicable 2023-09-11 19:45:49 +02:00
dictionary.cpp Allow returning Dictionary after merging 2024-03-06 14:49:35 +01:00
dictionary.h Allow returning Dictionary after merging 2024-03-06 14:49:35 +01:00
method_ptrcall.h Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
native_ptr.h Enforce template syntax typename over class 2024-03-07 22:39:09 -06:00
SCsub Reorganized core/ directory, it was too fatty already 2020-11-07 20:17:12 -03:00
type_info.h Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
typed_array.h [Core] Fix sharing of typed arrays from constructor 2024-05-07 09:52:17 +02:00
variant_call.cpp [TextServer, GDExtension] Fix building text servers as GDExtension, expose new/changed low-level methods to GDExtension API. 2024-06-12 19:30:19 +03:00
variant_callable.cpp Add methods to get argument count of methods 2024-03-10 11:02:43 +01:00
variant_callable.h Add methods to get argument count of methods 2024-03-10 11:02:43 +01:00
variant_construct.cpp Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
variant_construct.h [Core] Fix Variant::construct of Object 2024-07-25 12:25:29 +02:00
variant_destruct.cpp Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
variant_destruct.h Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
variant_internal.h Merge pull request #91104 from RandomShaper/simple_type_cpp 2024-05-08 09:54:16 +02:00
variant_op.cpp Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
variant_op.h Revert "Make freed object different than null in comparison operators" 2024-07-01 14:11:43 +02:00
variant_parser.cpp Add PackedVector4Array Variant type 2024-05-03 00:58:27 +02:00
variant_parser.h Use compatible text resource format when possible 2024-04-23 12:04:44 +08:00
variant_setget.cpp Add shorthand for using singleton string names 2024-05-11 18:53:08 +02:00
variant_setget.h Use Core/Scene stringnames consistently 2024-05-13 23:41:07 +02:00
variant_utility.cpp Use Core/Scene stringnames consistently 2024-05-13 23:41:07 +02:00
variant_utility.h Add const lvalue ref to core/* container parameters 2024-02-14 11:20:36 -03:00
variant.cpp Revert "Make freed object different than null in comparison operators" 2024-07-01 14:11:43 +02:00
variant.h Core: Improve vformat error reporting on sprintf failure 2024-07-04 10:54:55 +02:00