Skip to content

Commit 136c6d1

Browse files
Copilotjkotas
andcommitted
Final batch: Replace Marshal calls in Interop.Errors, Crypto, ASN1, and OpenSsl files
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
1 parent 02c26a2 commit 136c6d1

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

src/libraries/Common/src/Interop/Unix/Interop.Errors.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Runtime.InteropServices;
6+
using System.Runtime.InteropServices.Marshalling;
67

78
internal static partial class Interop
89
{
@@ -179,7 +180,7 @@ internal static unsafe string StrError(int platformErrno)
179180
message = buffer;
180181
}
181182

182-
return Marshal.PtrToStringUTF8((IntPtr)message)!;
183+
return Utf8StringMarshaller.ConvertToManaged(message)!;
183184
}
184185

185186
#if SERIAL_PORTS

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.Runtime.InteropServices;
7+
using System.Runtime.InteropServices.Marshalling;
78
using System.Security.Cryptography;
89

910
using Microsoft.Win32.SafeHandles;
@@ -67,7 +68,7 @@ internal static unsafe string GetOidValue(IntPtr asn1ObjectPtr)
6768

6869
if (bytesNeeded < StackCapacity)
6970
{
70-
return Marshal.PtrToStringUTF8((IntPtr)bufStack, bytesNeeded);
71+
return Utf8StringMarshaller.ConvertToManaged(bufStack, bytesNeeded);
7172
}
7273

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

96-
return Marshal.PtrToStringUTF8((IntPtr)buf, bytesNeeded);
97+
return Utf8StringMarshaller.ConvertToManaged(buf, bytesNeeded);
9798
}
9899
}
99100
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Globalization;
77
using System.Runtime.InteropServices;
8+
using System.Runtime.InteropServices.Marshalling;
89
using System.Security.Cryptography;
910
using System.Security.Cryptography.X509Certificates;
1011
using Microsoft.Win32.SafeHandles;
@@ -71,13 +72,13 @@ internal static int BioTell(SafeBioHandle bio)
7172
byte usedDefault;
7273
IntPtr ptr = GetX509RootStorePath_private(&usedDefault);
7374
defaultPath = (usedDefault != 0);
74-
return Marshal.PtrToStringUTF8(ptr);
75+
return Utf8StringMarshaller.ConvertToManaged(ptr);
7576
}
7677

7778
internal static unsafe string? GetX509RootStoreFile()
7879
{
7980
byte unused;
80-
return Marshal.PtrToStringUTF8(GetX509RootStoreFile_private(&unused));
81+
return Utf8StringMarshaller.ConvertToManaged(GetX509RootStoreFile_private(&unused));
8182
}
8283

8384
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509RootStorePath")]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Net;
1313
using System.Net.Security;
1414
using System.Runtime.InteropServices;
15+
using System.Runtime.InteropServices.Marshalling;
1516
using System.Security.Authentication;
1617
using System.Security.Authentication.ExtendedProtection;
1718
using System.Security.Cryptography;
@@ -1077,7 +1078,7 @@ internal static SslException CreateSslException(string message)
10771078
// Capture last error to be consistent with CreateOpenSslCryptographicException
10781079
ulong errorVal = Crypto.ErrPeekLastError();
10791080
Crypto.ErrClearError();
1080-
string msg = SR.Format(message, Marshal.PtrToStringUTF8(Crypto.ErrReasonErrorString(errorVal)));
1081+
string msg = SR.Format(message, Utf8StringMarshaller.ConvertToManaged(Crypto.ErrReasonErrorString(errorVal)));
10811082
return new SslException(msg, (int)errorVal);
10821083
}
10831084

0 commit comments

Comments
 (0)