Skip to content

Commit a9f8a4d

Browse files
Copilotstephentoub
andcommitted
Scope unsafe block to only the pointer manipulation lines
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent e17be78 commit a9f8a4d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Common/Polyfills/System/GuidHelpers.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static class GuidHelpers
2727
/// all generated GUIDs are strictly ordered by creation time, regardless of timestamp.
2828
/// </para>
2929
/// </remarks>
30-
public static unsafe Guid CreateMonotonicUuid(DateTimeOffset timestamp)
30+
public static Guid CreateMonotonicUuid(DateTimeOffset timestamp)
3131
{
3232
// UUID v7 format (RFC 9562):
3333
// - 48 bits: Unix timestamp in milliseconds (big-endian)
@@ -42,15 +42,18 @@ public static unsafe Guid CreateMonotonicUuid(DateTimeOffset timestamp)
4242
// Start with a random GUID and twiddle the relevant bits
4343
Guid guid = Guid.NewGuid();
4444

45-
int* guidAsInts = (int*)&guid;
46-
short* guidAsShorts = (short*)&guid;
47-
byte* guidAsBytes = (byte*)&guid;
48-
49-
// Set timestamp (48 bits) and version/counter using little-endian layout
50-
guidAsInts[0] = (int)(timestampMs >> 8);
51-
guidAsShorts[2] = (short)((timestampMs & 0xFF) | ((timestampMs >> 40) << 8));
52-
guidAsShorts[3] = (short)((counter & 0xFFF) | 0x7000);
53-
guidAsBytes[8] = (byte)((guidAsBytes[8] & 0x3F) | 0x80);
45+
unsafe
46+
{
47+
int* guidAsInts = (int*)&guid;
48+
short* guidAsShorts = (short*)&guid;
49+
byte* guidAsBytes = (byte*)&guid;
50+
51+
// Set timestamp (48 bits) and version/counter using little-endian layout
52+
guidAsInts[0] = (int)(timestampMs >> 8);
53+
guidAsShorts[2] = (short)((timestampMs & 0xFF) | ((timestampMs >> 40) << 8));
54+
guidAsShorts[3] = (short)((counter & 0xFFF) | 0x7000);
55+
guidAsBytes[8] = (byte)((guidAsBytes[8] & 0x3F) | 0x80);
56+
}
5457

5558
return guid;
5659
}

0 commit comments

Comments
 (0)