Skip to content

Commit c44f6a9

Browse files
EgorBostephentoub
andauthored
Remove a few instances of unsafe code (#116111)
Co-authored-by: Stephen Toub <[email protected]>
1 parent f329648 commit c44f6a9

File tree

10 files changed

+32
-31
lines changed

10 files changed

+32
-31
lines changed

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PollingWildCardChangeToken.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Buffers.Binary;
56
using System.Diagnostics;
67
using System.Diagnostics.CodeAnalysis;
78
using System.IO;
@@ -178,14 +179,8 @@ private void ComputeHash(IncrementalHash sha256, string path, DateTime lastChang
178179
sha256.AppendData(_byteBuffer, 0, length);
179180
sha256.AppendData(Separator, 0, Separator.Length);
180181

181-
Debug.Assert(_byteBuffer.Length > sizeof(long));
182-
unsafe
183-
{
184-
fixed (byte* b = _byteBuffer)
185-
{
186-
*((long*)b) = lastChangedUtc.Ticks;
187-
}
188-
}
182+
BinaryPrimitives.WriteInt64LittleEndian(_byteBuffer, lastChangedUtc.Ticks);
183+
189184
sha256.AppendData(_byteBuffer, 0, sizeof(long));
190185
sha256.AppendData(Separator, 0, Separator.Length);
191186
}

src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/Hashing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static unsafe int GetHashCodeOrdinalIgnoreCaseAscii(ReadOnlySpan<char> s)
139139
}
140140
}
141141

142-
public static unsafe int GetHashCodeOrdinalIgnoreCase(ReadOnlySpan<char> s)
142+
public static int GetHashCodeOrdinalIgnoreCase(ReadOnlySpan<char> s)
143143
{
144144
#if NET
145145
return string.GetHashCode(s, StringComparison.OrdinalIgnoreCase);

src/libraries/System.Data.Odbc/src/Common/System/Data/ProviderBase/DbBuffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ internal IntPtr ReadIntPtr(int offset)
362362
return value;
363363
}
364364

365-
internal unsafe float ReadSingle(int offset)
365+
internal float ReadSingle(int offset)
366366
{
367367
int value = ReadInt32(offset);
368368
return BitConverter.Int32BitsToSingle(value);
@@ -634,7 +634,7 @@ internal void WriteIntPtr(int offset, IntPtr value)
634634
}
635635
}
636636

637-
internal unsafe void WriteSingle(int offset, float value)
637+
internal void WriteSingle(int offset, float value)
638638
{
639639
WriteInt32(offset, BitConverter.SingleToInt32Bits(value));
640640
}

src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ internal void WriteIntPtr(int offset, IntPtr value)
607607
}
608608
}
609609

610-
internal unsafe void WriteSingle(int offset, float value)
610+
internal void WriteSingle(int offset, float value)
611611
{
612612
WriteInt32(offset, BitConverter.SingleToInt32Bits(value));
613613
}

src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ internal virtual void DisposeCore(bool disposing)
253253
// nop
254254
}
255255

256-
private unsafe int ReadCore(Span<byte> buffer)
256+
private int ReadCore(Span<byte> buffer)
257257
{
258258
Debug.Assert(_handle != null);
259259
DebugAssertHandleValid(_handle);
@@ -277,7 +277,7 @@ private unsafe int ReadCore(Span<byte> buffer)
277277
}
278278
}
279279

280-
private unsafe void WriteCore(ReadOnlySpan<byte> buffer)
280+
private void WriteCore(ReadOnlySpan<byte> buffer)
281281
{
282282
Debug.Assert(_handle != null);
283283
DebugAssertHandleValid(_handle);

src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ private static void DebugAssertHandleValid(SafePipeHandle handle)
110110

111111
// Reads a byte from the pipe stream. Returns the byte cast to an int
112112
// or -1 if the connection has been broken.
113-
public override unsafe int ReadByte()
113+
public override int ReadByte()
114114
{
115-
byte b;
116-
return Read(new Span<byte>(&b, 1)) > 0 ? b : -1;
115+
byte b = 0;
116+
return Read(new Span<byte>(ref b)) > 0 ? b : -1;
117117
}
118118

119-
public override unsafe void WriteByte(byte value)
119+
public override void WriteByte(byte value)
120120
{
121-
Write(new ReadOnlySpan<byte>(&value, 1));
121+
Write([value]);
122122
}
123123

124124
public override void Flush()

src/libraries/System.Memory/src/System/Buffers/SequenceReaderExtensions.Binary.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal static unsafe bool TryRead<T>(ref this SequenceReader<byte> reader, out
2929
if (span.Length < sizeof(T))
3030
return TryReadMultisegment(ref reader, out value);
3131

32-
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(span));
32+
value = MemoryMarshal.Read<T>(span);
3333
reader.Advance(sizeof(T));
3434
return true;
3535
}
@@ -48,7 +48,7 @@ private static unsafe bool TryReadMultisegment<T>(ref SequenceReader<byte> reade
4848
return false;
4949
}
5050

51-
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(tempSpan));
51+
value = MemoryMarshal.Read<T>(tempSpan);
5252
reader.Advance(sizeof(T));
5353
return true;
5454
}

src/libraries/System.Net.ServerSentEvents/src/System/Net/ServerSentEvents/Helpers.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void WriteUtf8String(this IBufferWriter<byte> writer, ReadOnlySpan
3838
writer.Advance(value.Length);
3939
}
4040

41-
public static unsafe void WriteUtf8String(this IBufferWriter<byte> writer, ReadOnlySpan<char> value)
41+
public static void WriteUtf8String(this IBufferWriter<byte> writer, ReadOnlySpan<char> value)
4242
{
4343
if (value.IsEmpty)
4444
{
@@ -52,10 +52,13 @@ public static unsafe void WriteUtf8String(this IBufferWriter<byte> writer, ReadO
5252
#if NET
5353
bytesWritten = Encoding.UTF8.GetBytes(value, buffer);
5454
#else
55-
fixed (char* chars = value)
56-
fixed (byte* bytes = buffer)
55+
unsafe
5756
{
58-
bytesWritten = Encoding.UTF8.GetBytes(chars, value.Length, bytes, maxByteCount);
57+
fixed (char* chars = value)
58+
fixed (byte* bytes = buffer)
59+
{
60+
bytesWritten = Encoding.UTF8.GetBytes(chars, value.Length, bytes, maxByteCount);
61+
}
5962
}
6063
#endif
6164
writer.Advance(bytesWritten);

src/libraries/System.Net.ServerSentEvents/src/System/Net/ServerSentEvents/SseParser.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ public static SseParser<T> Create<T>(Stream sseStream, SseItemParser<T> itemPars
5050
}
5151

5252
/// <summary>Encoding.UTF8.GetString(bytes)</summary>
53-
internal static unsafe string Utf8GetString(ReadOnlySpan<byte> bytes)
53+
internal static string Utf8GetString(ReadOnlySpan<byte> bytes)
5454
{
5555
#if NET
5656
return Encoding.UTF8.GetString(bytes);
5757
#else
58-
fixed (byte* ptr = bytes)
58+
unsafe
5959
{
60-
return ptr is null ?
61-
string.Empty :
62-
Encoding.UTF8.GetString(ptr, bytes.Length);
60+
fixed (byte* ptr = bytes)
61+
{
62+
return ptr is null ?
63+
string.Empty :
64+
Encoding.UTF8.GetString(ptr, bytes.Length);
65+
}
6366
}
6467
#endif
6568
}

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Regex.Replace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private static string Replace(MatchEvaluator evaluator, Regex regex, string inpu
216216
}
217217

218218
/// <summary>Creates a string from all the segments in the builder and then disposes of the builder.</summary>
219-
internal static unsafe string SegmentsToStringAndDispose(ref StructListBuilder<ReadOnlyMemory<char>> segments)
219+
internal static string SegmentsToStringAndDispose(ref StructListBuilder<ReadOnlyMemory<char>> segments)
220220
{
221221
Span<ReadOnlyMemory<char>> span = segments.AsSpan();
222222

0 commit comments

Comments
 (0)