Skip to content

Commit cb44822

Browse files
authored
Use nint for native-sized integers in Marshal (#118335)
* Fix CA2020: Prevent behavioral change
1 parent 89e2ee2 commit cb44822

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ internal static unsafe void GetAnsiStringBytes(ReadOnlySpan<char> chars, Span<by
5656
bytes[actualByteLength] = 0;
5757
}
5858

59-
public static unsafe IntPtr AllocHGlobal(IntPtr cb)
59+
public static unsafe IntPtr AllocHGlobal(nint cb)
6060
{
61-
return (nint)NativeMemory.Alloc((nuint)(nint)cb);
61+
return (nint)NativeMemory.Alloc((nuint)cb);
6262
}
6363

6464
public static unsafe void FreeHGlobal(IntPtr hglobal)
6565
{
6666
NativeMemory.Free((void*)(nint)hglobal);
6767
}
6868

69-
public static unsafe IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
69+
public static unsafe IntPtr ReAllocHGlobal(IntPtr pv, nint cb)
7070
{
71-
return (nint)NativeMemory.Realloc((void*)(nint)pv, (nuint)(nint)cb);
71+
return (nint)NativeMemory.Realloc((void*)(nint)pv, (nuint)cb);
7272
}
7373

7474
public static IntPtr AllocCoTaskMem(int cb) => AllocHGlobal((nint)(uint)cb);

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static unsafe void FreeHGlobal(IntPtr hglobal)
144144
}
145145
}
146146

147-
public static unsafe IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
147+
public static unsafe IntPtr ReAllocHGlobal(IntPtr pv, nint cb)
148148
{
149149
if (pv == IntPtr.Zero)
150150
{

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public static unsafe int ReadInt32(IntPtr ptr, int ofs)
351351
[RequiresDynamicCode("Marshalling code for the object might not be available")]
352352
[EditorBrowsable(EditorBrowsableState.Never)]
353353
[Obsolete("ReadIntPtr(Object, Int32) may be unavailable in future releases.")]
354-
public static IntPtr ReadIntPtr(object ptr, int ofs)
354+
public static nint ReadIntPtr(object ptr, int ofs)
355355
{
356356
#if TARGET_64BIT
357357
return (nint)ReadInt64(ptr, ofs);
@@ -360,7 +360,7 @@ public static IntPtr ReadIntPtr(object ptr, int ofs)
360360
#endif
361361
}
362362

363-
public static IntPtr ReadIntPtr(IntPtr ptr, int ofs)
363+
public static nint ReadIntPtr(IntPtr ptr, int ofs)
364364
{
365365
#if TARGET_64BIT
366366
return (nint)ReadInt64(ptr, ofs);
@@ -369,7 +369,7 @@ public static IntPtr ReadIntPtr(IntPtr ptr, int ofs)
369369
#endif
370370
}
371371

372-
public static IntPtr ReadIntPtr(IntPtr ptr) => ReadIntPtr(ptr, 0);
372+
public static nint ReadIntPtr(IntPtr ptr) => ReadIntPtr(ptr, 0);
373373

374374
public static unsafe long ReadInt64(IntPtr ptr, int ofs)
375375
{
@@ -468,32 +468,28 @@ public static unsafe void WriteInt32(IntPtr ptr, int ofs, int val)
468468

469469
public static void WriteInt32(IntPtr ptr, int val) => WriteInt32(ptr, 0, val);
470470

471-
public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val)
471+
public static void WriteIntPtr(IntPtr ptr, int ofs, nint val)
472472
{
473473
#if TARGET_64BIT
474474
WriteInt64(ptr, ofs, (long)val);
475475
#else // 32
476-
#pragma warning disable CA2020 // Prevent from behavioral change
477476
WriteInt32(ptr, ofs, (int)val);
478-
#pragma warning restore CA2020
479477
#endif
480478
}
481479

482480
[RequiresDynamicCode("Marshalling code for the object might not be available")]
483481
[EditorBrowsable(EditorBrowsableState.Never)]
484482
[Obsolete("WriteIntPtr(Object, Int32, IntPtr) may be unavailable in future releases.")]
485-
public static void WriteIntPtr(object ptr, int ofs, IntPtr val)
483+
public static void WriteIntPtr(object ptr, int ofs, nint val)
486484
{
487485
#if TARGET_64BIT
488486
WriteInt64(ptr, ofs, (long)val);
489487
#else // 32
490-
#pragma warning disable CA2020 // Prevent from behavioral change
491488
WriteInt32(ptr, ofs, (int)val);
492-
#pragma warning restore CA2020
493489
#endif
494490
}
495491

496-
public static void WriteIntPtr(IntPtr ptr, IntPtr val) => WriteIntPtr(ptr, 0, val);
492+
public static void WriteIntPtr(IntPtr ptr, nint val) => WriteIntPtr(ptr, 0, val);
497493

498494
public static unsafe void WriteInt64(IntPtr ptr, int ofs, long val)
499495
{

0 commit comments

Comments
 (0)