Skip to content

Commit 6ac21ae

Browse files
CopilotShyam-GuptaGrabYourPitchforks
authored
Fix GC hole in EnumExtensions.ChangeFlags (#14290)
* Initial plan * Fix GC hole in EnumExtensions.ChangeFlags Co-authored-by: Shyam-Gupta <24871676+Shyam-Gupta@users.noreply.github.com> * Remove unsafe keyword and use Unsafe.As consistently Co-authored-by: Shyam-Gupta <24871676+Shyam-Gupta@users.noreply.github.com> * Apply minimal fix: only use Unsafe.As on left-hand side Co-authored-by: GrabYourPitchforks <1746272+GrabYourPitchforks@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Shyam-Gupta <24871676+Shyam-Gupta@users.noreply.github.com> Co-authored-by: GrabYourPitchforks <1746272+GrabYourPitchforks@users.noreply.github.com>
1 parent c7ffcb5 commit 6ac21ae

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

src/System.Windows.Forms.Primitives/src/System/EnumExtensions.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,49 @@ internal static class EnumExtensions
1818
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1919
public static unsafe void ChangeFlags<T>(ref this T value, T flags, bool set) where T : unmanaged, Enum
2020
{
21-
T* v = (T*)Unsafe.AsPointer(ref value);
22-
2321
// These conditions get optimized away by the JIT.
2422
if (sizeof(T) == sizeof(byte))
2523
{
2624
if (set)
2725
{
28-
*(byte*)v |= *(byte*)&flags;
26+
Unsafe.As<T, byte>(ref value) |= *(byte*)&flags;
2927
}
3028
else
3129
{
32-
*(byte*)v &= (byte)~*(byte*)&flags;
30+
Unsafe.As<T, byte>(ref value) &= (byte)~*(byte*)&flags;
3331
}
3432
}
3533
else if (sizeof(T) == sizeof(ushort))
3634
{
3735
if (set)
3836
{
39-
*(ushort*)v |= *(ushort*)&flags;
37+
Unsafe.As<T, ushort>(ref value) |= *(ushort*)&flags;
4038
}
4139
else
4240
{
43-
*(ushort*)v &= (ushort)~*(ushort*)&flags;
41+
Unsafe.As<T, ushort>(ref value) &= (ushort)~*(ushort*)&flags;
4442
}
4543
}
4644
else if (sizeof(T) == sizeof(uint))
4745
{
4846
if (set)
4947
{
50-
*(uint*)v |= *(uint*)&flags;
48+
Unsafe.As<T, uint>(ref value) |= *(uint*)&flags;
5149
}
5250
else
5351
{
54-
*(uint*)v &= ~*(uint*)&flags;
52+
Unsafe.As<T, uint>(ref value) &= ~*(uint*)&flags;
5553
}
5654
}
5755
else if (sizeof(T) == sizeof(ulong))
5856
{
5957
if (set)
6058
{
61-
*(ulong*)v |= *(ulong*)&flags;
59+
Unsafe.As<T, ulong>(ref value) |= *(ulong*)&flags;
6260
}
6361
else
6462
{
65-
*(ulong*)v &= ~*(ulong*)&flags;
63+
Unsafe.As<T, ulong>(ref value) &= ~*(ulong*)&flags;
6664
}
6765
}
6866
}

0 commit comments

Comments
 (0)