Skip to content

Commit e1d56ab

Browse files
committed
fix(managed): Trying to interpret null pointers
1 parent 8258195 commit e1d56ab

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/managed/Internal_API/CallContext.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,22 +275,20 @@ internal static unsafe void WritePrimitiveToPointer(ref object value, ref void*
275275

276276
internal static unsafe object ReadPointerToPrimitive(ref byte* ptr, ref Type type)
277277
{
278-
ref byte start = ref ptr[0];
279-
280-
if (type == typeof(bool)) return (Unsafe.ReadUnaligned<bool>(ref start));
281-
if (type == typeof(char)) return Unsafe.ReadUnaligned<char>(ref start);
282-
if (type == typeof(sbyte)) return Unsafe.ReadUnaligned<sbyte>(ref start);
283-
if (type == typeof(byte)) return Unsafe.ReadUnaligned<byte>(ref start);
284-
if (type == typeof(short)) return Unsafe.ReadUnaligned<short>(ref start);
285-
if (type == typeof(ushort)) return Unsafe.ReadUnaligned<ushort>(ref start);
286-
if (type == typeof(int)) return Unsafe.ReadUnaligned<int>(ref start);
287-
if (type == typeof(uint)) return Unsafe.ReadUnaligned<uint>(ref start);
288-
if (type == typeof(long)) return Unsafe.ReadUnaligned<long>(ref start);
289-
if (type == typeof(ulong)) return Unsafe.ReadUnaligned<ulong>(ref start);
290-
if (type == typeof(float)) return Unsafe.ReadUnaligned<float>(ref start);
291-
if (type == typeof(double)) return Unsafe.ReadUnaligned<double>(ref start);
292-
if (type == typeof(decimal)) return Unsafe.ReadUnaligned<decimal>(ref start);
293-
return IntPtr.Zero;
278+
if (type == typeof(bool)) return (Unsafe.Read<bool>(ptr));
279+
if (type == typeof(char)) return Unsafe.Read<char>(ptr);
280+
if (type == typeof(sbyte)) return Unsafe.Read<sbyte>(ptr);
281+
if (type == typeof(byte)) return Unsafe.Read<byte>(ptr);
282+
if (type == typeof(short)) return Unsafe.Read<short>(ptr);
283+
if (type == typeof(ushort)) return Unsafe.Read<ushort>(ptr);
284+
if (type == typeof(int)) return Unsafe.Read<int>(ptr);
285+
if (type == typeof(uint)) return Unsafe.Read<uint>(ptr);
286+
if (type == typeof(long)) return Unsafe.Read<long>(ptr);
287+
if (type == typeof(ulong)) return Unsafe.Read<ulong>(ptr);
288+
if (type == typeof(float)) return Unsafe.Read<float>(ptr);
289+
if (type == typeof(double)) return Unsafe.Read<double>(ptr);
290+
if (type == typeof(decimal)) return Unsafe.Read<decimal>(ptr);
291+
return null;
294292
}
295293

296294
[SecurityCritical]
@@ -656,6 +654,7 @@ public static unsafe object ReadValue(ref Type type, ref byte* ptr)
656654
if (type == typeof(string))
657655
{
658656
IntPtr p = *(IntPtr*)ptr;
657+
if (p == IntPtr.Zero) return null;
659658
StringData* data = (StringData*)p;
660659

661660
if (data->len == 0) return "";
@@ -693,7 +692,9 @@ public static unsafe object ReadValue(ref Type type, ref byte* ptr)
693692
else if(type.IsArray)
694693
{
695694
IntPtr p = *(IntPtr*)ptr;
696-
ArrayData *data = (ArrayData*)p;
695+
if (p == IntPtr.Zero) return null;
696+
697+
ArrayData* data = (ArrayData*)p;
697698

698699
Type t = type.GetElementType()!;
699700
Array? arr = Cacher.CreateArray(t, data->Length);
@@ -718,6 +719,8 @@ public static unsafe object ReadValue(ref Type type, ref byte* ptr)
718719
if (dict == null) return null;
719720

720721
IntPtr p = *(IntPtr*)ptr;
722+
if (p == IntPtr.Zero) return null;
723+
721724
MapData* data = (MapData*)p;
722725

723726
IntPtr* keys = (IntPtr*)data->Keys;
@@ -744,6 +747,7 @@ public static unsafe object ReadValue(ref Type type, ref byte* ptr)
744747
}
745748
else if(type == typeof(IntPtr))
746749
{
750+
if ((IntPtr)ptr == IntPtr.Zero) return null;
747751
return *(IntPtr*)ptr;
748752
}
749753

0 commit comments

Comments
 (0)