Skip to content

Commit 61575f0

Browse files
Copilotjkotas
andcommitted
Address review feedback: Update function signatures to use byte* and fix unsafe context issues
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
1 parent b6f7b7c commit 61575f0

File tree

14 files changed

+30
-30
lines changed

14 files changed

+30
-30
lines changed

src/libraries/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Err.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static unsafe string ErrErrorStringN(ulong error)
3535
fixed (byte* buf = &buffer[0])
3636
{
3737
ErrErrorStringN(error, buf, buffer.Length);
38-
return Utf8StringMarshaller.ConvertToManaged((byte*)buf)!;
38+
return Utf8StringMarshaller.ConvertToManaged(buf)!;
3939
}
4040
}
4141

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid)
118118
// Get the process information for the specified pid
119119
info = new ProcessInfo();
120120

121-
info.ProcessName = Utf8StringMarshaller.ConvertToManaged((byte*)kinfo->ki_comm)!;
121+
info.ProcessName = Utf8StringMarshaller.ConvertToManaged(kinfo->ki_comm)!;
122122
info.BasePriority = kinfo->ki_nice;
123123
info.VirtualBytes = (long)kinfo->ki_size;
124124
info.WorkingSet = kinfo->ki_rssize;

src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFString.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static partial class CoreFoundation
1717
/// If it has the wrong encoding, or if the interior pointer isn't being shared for some reason, returns NULL
1818
/// </summary>
1919
[LibraryImport(Libraries.CoreFoundationLibrary)]
20-
private static partial IntPtr CFStringGetCStringPtr(
20+
private static unsafe partial byte* CFStringGetCStringPtr(
2121
SafeCFStringHandle cfString,
2222
CFStringBuiltInEncodings encoding);
2323

@@ -37,13 +37,13 @@ internal static string CFStringToString(SafeCFStringHandle cfString)
3737
// If the string is already stored internally as UTF-8 we can (usually)
3838
// get the raw pointer to the data blob, then we can Marshal in the string
3939
// via pointer semantics, avoiding a copy.
40-
IntPtr interiorPointer = CFStringGetCStringPtr(
40+
byte* interiorPointer = CFStringGetCStringPtr(
4141
cfString,
4242
CFStringBuiltInEncodings.kCFStringEncodingUTF8);
4343

44-
if (interiorPointer != IntPtr.Zero)
44+
if (interiorPointer != null)
4545
{
46-
return Utf8StringMarshaller.ConvertToManaged((byte*)interiorPointer)!;
46+
return Utf8StringMarshaller.ConvertToManaged(interiorPointer)!;
4747
}
4848

4949
SafeCFDataHandle cfData = CFStringCreateExternalRepresentation(

src/libraries/Common/src/Interop/OSX/Interop.NetworkFramework.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ internal struct NetworkFrameworkError
6767
{
6868
public int ErrorCode;
6969
public int ErrorDomain;
70-
public IntPtr ErrorMessage; // C string of NULL
70+
public byte* ErrorMessage; // C string of NULL
7171
}
7272

7373
internal static Exception CreateExceptionForNetworkFrameworkError(in NetworkFrameworkError error)
7474
{
7575
string? message = null;
7676
NetworkFrameworkErrorDomain domain = (NetworkFrameworkErrorDomain)error.ErrorDomain;
7777

78-
if (error.ErrorMessage != IntPtr.Zero)
78+
if (error.ErrorMessage != null)
7979
{
8080
message = Utf8StringMarshaller.ConvertToManaged(error.ErrorMessage);
8181
}

src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.FormatInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal static unsafe Error GetFileSystemTypeNameForMountPoint(string name, out
6060
int result = GetFileSystemTypeNameForMountPoint(name, formatBuffer, MountPointFormatBufferSizeInBytes, &formatType);
6161
if (result == 0)
6262
{
63-
format = formatType == -1 ? Utf8StringMarshaller.ConvertToManaged((byte*)formatBuffer)!
63+
format = formatType == -1 ? Utf8StringMarshaller.ConvertToManaged(formatBuffer)!
6464
: (Enum.GetName(typeof(UnixFileSystemTypes), formatType) ?? "");
6565
return Error.SUCCESS;
6666
}

src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private static unsafe void AddMountPoint(void* context, byte* name)
2828
AllMountPointsContext* callbackContext = (AllMountPointsContext*)context;
2929
try
3030
{
31-
callbackContext->_results.Add(Utf8StringMarshaller.ConvertToManaged((byte*)name)!);
31+
callbackContext->_results.Add(Utf8StringMarshaller.ConvertToManaged(name)!);
3232
}
3333
catch (Exception e)
3434
{

src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.GssApiException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static string GetGssApiDisplayStatus(Status majorStatus, Status minorSta
8686
Interop.NetSecurityNative.Status displayCallStatus = isMinor ?
8787
DisplayMinorStatus(out minStat, status, ref displayBuffer) :
8888
DisplayMajorStatus(out minStat, status, ref displayBuffer);
89-
return (Status.GSS_S_COMPLETE != displayCallStatus) ? null : Utf8StringMarshaller.ConvertToManaged((byte*)displayBuffer._data);
89+
return (Status.GSS_S_COMPLETE != displayCallStatus) ? null : Utf8StringMarshaller.ConvertToManaged(displayBuffer._data);
9090
}
9191
finally
9292
{

src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.GssBuffer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ internal static partial class NetSecurityNative
1414
internal struct GssBuffer : IDisposable
1515
{
1616
internal ulong _length;
17-
internal IntPtr _data;
17+
internal byte* _data;
1818

1919
internal int Copy(byte[] destination, int offset)
2020
{
2121
Debug.Assert(destination != null, "target destination cannot be null");
2222
Debug.Assert((offset >= 0 && offset < destination.Length) || destination.Length == 0, $"invalid offset {offset}");
2323

24-
if (_data == IntPtr.Zero || _length == 0)
24+
if (_data == null || _length == 0)
2525
{
2626
return 0;
2727
}
@@ -40,7 +40,7 @@ internal int Copy(byte[] destination, int offset)
4040

4141
internal byte[] ToByteArray()
4242
{
43-
if (_data == IntPtr.Zero || _length == 0)
43+
if (_data == null || _length == 0)
4444
{
4545
return Array.Empty<byte>();
4646
}
@@ -51,16 +51,16 @@ internal byte[] ToByteArray()
5151
return destination;
5252
}
5353

54-
internal unsafe ReadOnlySpan<byte> Span => (_data != IntPtr.Zero && _length != 0) ?
55-
new ReadOnlySpan<byte>(_data.ToPointer(), checked((int)_length)) :
54+
internal unsafe ReadOnlySpan<byte> Span => (_data != null && _length != 0) ?
55+
new ReadOnlySpan<byte>(_data, checked((int)_length)) :
5656
default;
5757

5858
public void Dispose()
5959
{
60-
if (_data != IntPtr.Zero)
60+
if (_data != null)
6161
{
6262
Interop.NetSecurityNative.ReleaseGssBuffer(_data, _length);
63-
_data = IntPtr.Zero;
63+
_data = null;
6464
}
6565

6666
_length = 0;

src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.NetSecurityNative.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ internal static partial class Interop
1212
internal static partial class NetSecurityNative
1313
{
1414
[LibraryImport(Interop.Libraries.NetSecurityNative, EntryPoint = "NetSecurityNative_ReleaseGssBuffer")]
15-
internal static partial void ReleaseGssBuffer(
16-
IntPtr bufferPtr,
15+
internal static unsafe partial void ReleaseGssBuffer(
16+
byte* bufferPtr,
1717
ulong length);
1818

1919
[LibraryImport(Interop.Libraries.NetSecurityNative, EntryPoint = "NetSecurityNative_DisplayMinorStatus")]

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal static unsafe string GetOidValue(IntPtr asn1ObjectPtr)
6868

6969
if (bytesNeeded < StackCapacity)
7070
{
71-
return Utf8StringMarshaller.ConvertToManaged(bufStack, bytesNeeded);
71+
return Marshal.PtrToStringUTF8((IntPtr)bufStack, bytesNeeded);
7272
}
7373

7474
// bytesNeeded does not count the \0 which will be written on the end (based on OpenSSL 1.0.1f),
@@ -94,7 +94,7 @@ internal static unsafe string GetOidValue(IntPtr asn1ObjectPtr)
9494
throw new CryptographicException();
9595
}
9696

97-
return Utf8StringMarshaller.ConvertToManaged(buf, bytesNeeded);
97+
return Marshal.PtrToStringUTF8((IntPtr)buf, bytesNeeded);
9898
}
9999
}
100100
}

0 commit comments

Comments
 (0)