Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit dcf9f36

Browse files
benaadamsjkotas
authored andcommitted
Remove redundant unsafe contexts (#21826)
1 parent b188e9d commit dcf9f36

23 files changed

+88
-88
lines changed

src/System.Private.CoreLib/shared/System/Text/DecoderNLS.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override void Reset()
4747
_fallbackBuffer?.Reset();
4848
}
4949

50-
public override unsafe int GetCharCount(byte[] bytes, int index, int count)
50+
public override int GetCharCount(byte[] bytes, int index, int count)
5151
{
5252
return GetCharCount(bytes, index, count, false);
5353
}
@@ -91,7 +91,7 @@ public unsafe override int GetCharCount(byte* bytes, int count, bool flush)
9191
return _encoding.GetCharCount(bytes, count, this);
9292
}
9393

94-
public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount,
94+
public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
9595
char[] chars, int charIndex)
9696
{
9797
return GetChars(bytes, byteIndex, byteCount, chars, charIndex, false);

src/System.Private.CoreLib/shared/System/Text/Encoding.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ internal unsafe bool AddChar(char ch, int numBytes)
14591459
return true;
14601460
}
14611461

1462-
internal unsafe bool AddChar(char ch)
1462+
internal bool AddChar(char ch)
14631463
{
14641464
return AddChar(ch, 1);
14651465
}
@@ -1515,7 +1515,7 @@ internal unsafe int BytesUsed
15151515
}
15161516
}
15171517

1518-
internal unsafe bool Fallback(byte fallbackByte)
1518+
internal bool Fallback(byte fallbackByte)
15191519
{
15201520
// Build our buffer
15211521
byte[] byteBuffer = new byte[] { fallbackByte };
@@ -1524,7 +1524,7 @@ internal unsafe bool Fallback(byte fallbackByte)
15241524
return Fallback(byteBuffer);
15251525
}
15261526

1527-
internal unsafe bool Fallback(byte byte1, byte byte2)
1527+
internal bool Fallback(byte byte1, byte byte2)
15281528
{
15291529
// Build our buffer
15301530
byte[] byteBuffer = new byte[] { byte1, byte2 };
@@ -1533,7 +1533,7 @@ internal unsafe bool Fallback(byte byte1, byte byte2)
15331533
return Fallback(byteBuffer);
15341534
}
15351535

1536-
internal unsafe bool Fallback(byte byte1, byte byte2, byte byte3, byte byte4)
1536+
internal bool Fallback(byte byte1, byte byte2, byte byte3, byte byte4)
15371537
{
15381538
// Build our buffer
15391539
byte[] byteBuffer = new byte[] { byte1, byte2, byte3, byte4 };
@@ -1566,7 +1566,7 @@ internal unsafe bool Fallback(byte[] byteBuffer)
15661566
return true;
15671567
}
15681568

1569-
internal unsafe int Count
1569+
internal int Count
15701570
{
15711571
get
15721572
{
@@ -1634,34 +1634,34 @@ internal unsafe bool AddByte(byte b, int moreBytesExpected)
16341634
return true;
16351635
}
16361636

1637-
internal unsafe bool AddByte(byte b1)
1637+
internal bool AddByte(byte b1)
16381638
{
16391639
return (AddByte(b1, 0));
16401640
}
16411641

1642-
internal unsafe bool AddByte(byte b1, byte b2)
1642+
internal bool AddByte(byte b1, byte b2)
16431643
{
16441644
return (AddByte(b1, b2, 0));
16451645
}
16461646

1647-
internal unsafe bool AddByte(byte b1, byte b2, int moreBytesExpected)
1647+
internal bool AddByte(byte b1, byte b2, int moreBytesExpected)
16481648
{
16491649
return (AddByte(b1, 1 + moreBytesExpected) && AddByte(b2, moreBytesExpected));
16501650
}
16511651

1652-
internal unsafe bool AddByte(byte b1, byte b2, byte b3)
1652+
internal bool AddByte(byte b1, byte b2, byte b3)
16531653
{
16541654
return AddByte(b1, b2, b3, (int)0);
16551655
}
16561656

1657-
internal unsafe bool AddByte(byte b1, byte b2, byte b3, int moreBytesExpected)
1657+
internal bool AddByte(byte b1, byte b2, byte b3, int moreBytesExpected)
16581658
{
16591659
return (AddByte(b1, 2 + moreBytesExpected) &&
16601660
AddByte(b2, 1 + moreBytesExpected) &&
16611661
AddByte(b3, moreBytesExpected));
16621662
}
16631663

1664-
internal unsafe bool AddByte(byte b1, byte b2, byte b3, byte b4)
1664+
internal bool AddByte(byte b1, byte b2, byte b3, byte b4)
16651665
{
16661666
return (AddByte(b1, 3) &&
16671667
AddByte(b2, 2) &&
@@ -1724,7 +1724,7 @@ internal unsafe int CharsUsed
17241724
}
17251725
}
17261726

1727-
internal unsafe int Count
1727+
internal int Count
17281728
{
17291729
get
17301730
{

src/System.Private.CoreLib/shared/System/Text/ValueStringBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public unsafe void Append(char* value, int length)
227227
_pos += length;
228228
}
229229

230-
public unsafe void Append(ReadOnlySpan<char> value)
230+
public void Append(ReadOnlySpan<char> value)
231231
{
232232
int pos = _pos;
233233
if (pos > _chars.Length - value.Length)

src/System.Private.CoreLib/shared/System/UIntPtr.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private unsafe UIntPtr(SerializationInfo info, StreamingContext context)
6060
_value = (void*)l;
6161
}
6262

63-
unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
63+
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
6464
{
6565
if (info == null)
6666
throw new ArgumentNullException(nameof(info));
@@ -196,7 +196,7 @@ public static UIntPtr Subtract(UIntPtr pointer, int offset)
196196
return new UIntPtr((nuint)pointer._value - (nuint)offset);
197197
}
198198

199-
public static unsafe int Size
199+
public static int Size
200200
{
201201
[Intrinsic]
202202
[NonVersionable]

src/System.Private.CoreLib/src/Microsoft/Win32/UnsafeNativeMethods.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal static unsafe class ManifestEtw
4343
//
4444
// Callback
4545
//
46-
internal unsafe delegate void EtwEnableCallback(
46+
internal delegate void EtwEnableCallback(
4747
[In] ref Guid sourceId,
4848
[In] int isEnabled,
4949
[In] byte level,
@@ -69,15 +69,15 @@ [In][Out]ref long registrationHandle
6969
internal static extern uint EventUnregister([In] long registrationHandle);
7070

7171
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteString", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
72-
internal static extern unsafe int EventWriteString(
72+
internal static extern int EventWriteString(
7373
[In] long registrationHandle,
7474
[In] byte level,
7575
[In] long keyword,
7676
[In] string msg
7777
);
7878

7979
[StructLayout(LayoutKind.Sequential)]
80-
internal unsafe struct EVENT_FILTER_DESCRIPTOR
80+
internal struct EVENT_FILTER_DESCRIPTOR
8181
{
8282
public long Ptr;
8383
public int Size;

src/System.Private.CoreLib/src/System/Delegate.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected Delegate(object target, string method)
5656
// This constructor is called from a class to generate a
5757
// delegate based upon a static method name and the Type object
5858
// for the class defining the method.
59-
protected unsafe Delegate(Type target, string method)
59+
protected Delegate(Type target, string method)
6060
{
6161
if (target == null)
6262
throw new ArgumentNullException(nameof(target));
@@ -548,7 +548,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
548548

549549
// V2 internal API.
550550
// This is Critical because it skips the security check when creating the delegate.
551-
internal static unsafe Delegate CreateDelegateNoSecurityCheck(Type type, object target, RuntimeMethodHandle method)
551+
internal static Delegate CreateDelegateNoSecurityCheck(Type type, object target, RuntimeMethodHandle method)
552552
{
553553
// Validate the parameters.
554554
if (type == null)

src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeMetadataGenerator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed class EventPipeMetadataGenerator
1515

1616
private EventPipeMetadataGenerator() { }
1717

18-
public unsafe byte[] GenerateEventMetadata(EventMetadata eventMetadata)
18+
public byte[] GenerateEventMetadata(EventMetadata eventMetadata)
1919
{
2020
ParameterInfo[] parameters = eventMetadata.Parameters;
2121
EventParameterInfo[] eventParams = new EventParameterInfo[parameters.Length];
@@ -33,7 +33,7 @@ public unsafe byte[] GenerateEventMetadata(EventMetadata eventMetadata)
3333
eventParams);
3434
}
3535

36-
public unsafe byte[] GenerateEventMetadata(
36+
public byte[] GenerateEventMetadata(
3737
int eventId,
3838
string eventName,
3939
EventKeywords keywords,
@@ -292,7 +292,7 @@ private static unsafe void GenerateMetadataForProperty(PropertyAnalysis property
292292
}
293293

294294

295-
internal unsafe uint GetMetadataLength()
295+
internal uint GetMetadataLength()
296296
{
297297
uint ret = 0;
298298

src/System.Private.CoreLib/src/System/Diagnostics/Eventing/FrameworkEventSource.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ private void GetRequestStreamStop(long id, bool success, bool synchronous)
628628
#if !CORECLR
629629
[System.Security.SecuritySafeCritical]
630630
#endif // !CORECLR
631-
public unsafe void BeginGetResponse(object id, string uri, bool success, bool synchronous)
631+
public void BeginGetResponse(object id, string uri, bool success, bool synchronous)
632632
{
633633
if (IsEnabled())
634634
GetResponseStart(IdForObject(id), uri, success, synchronous);
@@ -638,7 +638,7 @@ public unsafe void BeginGetResponse(object id, string uri, bool success, bool sy
638638
#if !CORECLR
639639
[System.Security.SecuritySafeCritical]
640640
#endif // !CORECLR
641-
public unsafe void EndGetResponse(object id, bool success, bool synchronous, int statusCode)
641+
public void EndGetResponse(object id, bool success, bool synchronous, int statusCode)
642642
{
643643
if (IsEnabled())
644644
GetResponseStop(IdForObject(id), success, synchronous, statusCode);
@@ -648,7 +648,7 @@ public unsafe void EndGetResponse(object id, bool success, bool synchronous, int
648648
#if !CORECLR
649649
[System.Security.SecuritySafeCritical]
650650
#endif // !CORECLR
651-
public unsafe void BeginGetRequestStream(object id, string uri, bool success, bool synchronous)
651+
public void BeginGetRequestStream(object id, string uri, bool success, bool synchronous)
652652
{
653653
if (IsEnabled())
654654
GetRequestStreamStart(IdForObject(id), uri, success, synchronous);
@@ -658,7 +658,7 @@ public unsafe void BeginGetRequestStream(object id, string uri, bool success, bo
658658
#if !CORECLR
659659
[System.Security.SecuritySafeCritical]
660660
#endif // !CORECLR
661-
public unsafe void EndGetRequestStream(object id, bool success, bool synchronous)
661+
public void EndGetRequestStream(object id, bool success, bool synchronous)
662662
{
663663
if (IsEnabled())
664664
GetRequestStreamStop(IdForObject(id), success, synchronous);

src/System.Private.CoreLib/src/System/Reflection/AssemblyName.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ internal static char EscapedAscii(char digit, char next)
576576
+ 10)));
577577
}
578578

579-
private static unsafe bool IsReservedUnreservedOrHash(char c)
579+
private static bool IsReservedUnreservedOrHash(char c)
580580
{
581581
if (IsUnreserved(c))
582582
{
@@ -585,7 +585,7 @@ private static unsafe bool IsReservedUnreservedOrHash(char c)
585585
return (RFC3986ReservedMarks.Contains(c));
586586
}
587587

588-
internal static unsafe bool IsUnreserved(char c)
588+
internal static bool IsUnreserved(char c)
589589
{
590590
if (IsAsciiLetterOrDigit(c))
591591
{

src/System.Private.CoreLib/src/System/Reflection/Associates.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal static bool IncludeAccessor(MethodInfo associate, bool nonPublic)
3535
return false;
3636
}
3737

38-
private static unsafe RuntimeMethodInfo AssignAssociates(
38+
private static RuntimeMethodInfo AssignAssociates(
3939
int tkMethod,
4040
RuntimeType declaredType,
4141
RuntimeType reflectedType)
@@ -108,7 +108,7 @@ private static unsafe RuntimeMethodInfo AssignAssociates(
108108
return associateMethod;
109109
}
110110

111-
internal static unsafe void AssignAssociates(
111+
internal static void AssignAssociates(
112112
MetadataImport scope,
113113
int mdPropEvent,
114114
RuntimeType declaringType,

src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private static IList<CustomAttributeData> GetCustomAttributes(RuntimeModule modu
251251
#endregion
252252

253253
#region Internal Static Members
254-
internal static unsafe CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken)
254+
internal static CustomAttributeRecord[] GetCustomAttributeRecords(RuntimeModule module, int targetToken)
255255
{
256256
MetadataImport scope = module.MetadataImport;
257257

@@ -1416,7 +1416,7 @@ private static bool IsCustomAttributeDefined(
14161416
return false;
14171417
}
14181418

1419-
private static unsafe object[] GetCustomAttributes(
1419+
private static object[] GetCustomAttributes(
14201420
RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType)
14211421
{
14221422
RuntimeType.ListBuilder<object> attributes = new RuntimeType.ListBuilder<object>();
@@ -1435,7 +1435,7 @@ private static unsafe object[] GetCustomAttributes(
14351435
return result;
14361436
}
14371437

1438-
private static unsafe void AddCustomAttributes(
1438+
private static void AddCustomAttributes(
14391439
ref RuntimeType.ListBuilder<object> attributes,
14401440
RuntimeModule decoratedModule, int decoratedMetadataToken,
14411441
RuntimeType attributeFilterType, bool mustBeInheritable, ref RuntimeType.ListBuilder<object> derivedAttributes)
@@ -1570,7 +1570,7 @@ private static unsafe void AddCustomAttributes(
15701570
}
15711571
}
15721572

1573-
private static unsafe bool FilterCustomAttributeRecord(
1573+
private static bool FilterCustomAttributeRecord(
15741574
CustomAttributeRecord caRecord,
15751575
MetadataImport scope,
15761576
RuntimeModule decoratedModule,

src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class DynamicILGenerator : ILGenerator
2323
internal DynamicScope m_scope;
2424
private int m_methodSigToken;
2525

26-
internal unsafe DynamicILGenerator(DynamicMethod method, byte[] methodSignature, int size)
26+
internal DynamicILGenerator(DynamicMethod method, byte[] methodSignature, int size)
2727
: base(method, size)
2828
{
2929
m_scope = new DynamicScope();
@@ -753,7 +753,7 @@ internal override byte[] GetLocalsSignature()
753753
return m_localSignature;
754754
}
755755

756-
internal override unsafe byte[] GetRawEHInfo()
756+
internal override byte[] GetRawEHInfo()
757757
{
758758
return m_exceptionHeader;
759759
}
@@ -916,7 +916,7 @@ internal class DynamicScope
916916
#endregion
917917

918918
#region Constructor
919-
internal unsafe DynamicScope()
919+
internal DynamicScope()
920920
{
921921
m_tokens = new List<object>();
922922
m_tokens.Add(null);

src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private static RuntimeModule GetDynamicMethodsModule()
251251
return s_anonymouslyHostedDynamicMethodsModule;
252252
}
253253

254-
private unsafe void Init(string name,
254+
private void Init(string name,
255255
MethodAttributes attributes,
256256
CallingConventions callingConvention,
257257
Type returnType,
@@ -369,7 +369,7 @@ public sealed override Delegate CreateDelegate(Type delegateType, object target)
369369
}
370370

371371
// This is guaranteed to return a valid handle
372-
internal unsafe RuntimeMethodHandle GetMethodDescriptor()
372+
internal RuntimeMethodHandle GetMethodDescriptor()
373373
{
374374
if (m_methodHandle == null)
375375
{

src/System.Private.CoreLib/src/System/Reflection/MdFieldInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public override void SetValueDirect(TypedReference obj, object value)
8282

8383
[DebuggerStepThroughAttribute]
8484
[Diagnostics.DebuggerHidden]
85-
public unsafe override object GetValue(object obj)
85+
public override object GetValue(object obj)
8686
{
8787
return GetValue(false);
8888
}
8989

90-
public unsafe override object GetRawConstantValue() { return GetValue(true); }
90+
public override object GetRawConstantValue() { return GetValue(true); }
9191

92-
private unsafe object GetValue(bool raw)
92+
private object GetValue(bool raw)
9393
{
9494
// Cannot cache these because they could be user defined non-agile enumerations
9595

0 commit comments

Comments
 (0)