From 553904d8a4a6ca31c8b31b5e8b14f30654e0d888 Mon Sep 17 00:00:00 2001 From: Steve Hawley Date: Wed, 11 Jan 2023 10:41:27 -0500 Subject: [PATCH] [dotnet] sanitized pinvokes that use string (#17143) sanitized the pinvokes that use string. --- src/CoreFoundation/CFMutableString.cs | 11 +++++++---- src/CoreFoundation/CFString.cs | 8 +++++--- src/CoreFoundation/Dispatch.cs | 15 +++++++++++++-- src/CoreFoundation/DispatchSource.cs | 5 +++-- src/CoreFoundation/OSLog.cs | 5 +++-- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/CoreFoundation/CFMutableString.cs b/src/CoreFoundation/CFMutableString.cs index f098366d85..7c62b4a239 100644 --- a/src/CoreFoundation/CFMutableString.cs +++ b/src/CoreFoundation/CFMutableString.cs @@ -54,8 +54,10 @@ namespace CoreFoundation { if (maxLength < 0) throw new ArgumentException (nameof (maxLength)); Handle = CFStringCreateMutable (IntPtr.Zero, maxLength); - if (@string is not null) - CFStringAppendCharacters (Handle, @string, @string.Length); + if (@string is not null) { + using var stringPtr = new TransientString (@string, TransientString.Encoding.Unicode); + CFStringAppendCharacters (Handle, stringPtr, @string.Length); + } } [DllImport (Constants.CoreFoundationLibrary)] @@ -72,14 +74,15 @@ namespace CoreFoundation { } [DllImport (Constants.CoreFoundationLibrary, CharSet = CharSet.Unicode)] - static extern void CFStringAppendCharacters (/* CFMutableStringRef* */ IntPtr theString, string chars, nint numChars); + static extern void CFStringAppendCharacters (/* CFMutableStringRef* */ IntPtr theString, IntPtr chars, nint numChars); public void Append (string @string) { if (@string is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (@string)); str = null; // destroy any cached value - CFStringAppendCharacters (Handle, @string, @string.Length); + using var stringPtr = new TransientString (@string, TransientString.Encoding.Unicode); + CFStringAppendCharacters (Handle, stringPtr, @string.Length); } [DllImport (Constants.CoreFoundationLibrary)] diff --git a/src/CoreFoundation/CFString.cs b/src/CoreFoundation/CFString.cs index a550914020..85f44cc1f9 100644 --- a/src/CoreFoundation/CFString.cs +++ b/src/CoreFoundation/CFString.cs @@ -130,7 +130,7 @@ namespace CoreFoundation { protected CFString () { } [DllImport (Constants.CoreFoundationLibrary, CharSet = CharSet.Unicode)] - extern static IntPtr CFStringCreateWithCharacters (IntPtr allocator, string str, nint count); + extern static IntPtr CFStringCreateWithCharacters (IntPtr allocator, IntPtr str, nint count); [DllImport (Constants.CoreFoundationLibrary, CharSet = CharSet.Unicode)] extern static nint CFStringGetLength (IntPtr handle); @@ -146,7 +146,8 @@ namespace CoreFoundation { if (value is null) return NativeHandle.Zero; - return CFStringCreateWithCharacters (IntPtr.Zero, value, value.Length); + using var valuePtr = new TransientString (value, TransientString.Encoding.Unicode); + return CFStringCreateWithCharacters (IntPtr.Zero, valuePtr, value.Length); } public static void ReleaseNative (NativeHandle handle) @@ -160,7 +161,8 @@ namespace CoreFoundation { if (str is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (str)); - Handle = CFStringCreateWithCharacters (IntPtr.Zero, str, str.Length); + using var strPtr = new TransientString (str, TransientString.Encoding.Unicode); + Handle = CFStringCreateWithCharacters (IntPtr.Zero, strPtr, str.Length); this.str = str; } diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index 54922b28a8..4c242543d0 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -556,11 +556,16 @@ namespace CoreFoundation { } } + static IntPtr dispatch_queue_create (string label, IntPtr attr) + { + using var labelPtr = new TransientString (label); + return dispatch_queue_create (labelPtr, attr); + } // // Native methods // [DllImport (Constants.libcLibrary)] - extern static IntPtr dispatch_queue_create (string label, IntPtr attr); + extern static IntPtr dispatch_queue_create (IntPtr label, IntPtr attr); #if NET [SupportedOSPlatform ("macos")] @@ -573,8 +578,14 @@ namespace CoreFoundation { [TV (10, 0)] [Watch (3, 0)] #endif + static IntPtr dispatch_queue_create_with_target (string label, IntPtr attr, IntPtr target) + { + using var labelPtr = new TransientString (label); + return dispatch_queue_create_with_target (labelPtr, attr, target); + } + [DllImport (Constants.libcLibrary, EntryPoint = "dispatch_queue_create_with_target$V2")] - extern static IntPtr dispatch_queue_create_with_target (string label, IntPtr attr, IntPtr target); + extern static IntPtr dispatch_queue_create_with_target (IntPtr label, IntPtr attr, IntPtr target); [DllImport (Constants.libcLibrary)] extern static void dispatch_async_f (IntPtr queue, IntPtr context, dispatch_callback_t dispatch); diff --git a/src/CoreFoundation/DispatchSource.cs b/src/CoreFoundation/DispatchSource.cs index 7fd7d7c1dd..4c4d699747 100644 --- a/src/CoreFoundation/DispatchSource.cs +++ b/src/CoreFoundation/DispatchSource.cs @@ -575,7 +575,7 @@ namespace CoreFoundation { const int O_EVTONLY = 0x8000; [DllImport (Constants.libcLibrary, SetLastError = true)] - extern static int open (string path, int flags); + extern static int open (IntPtr path, int flags); [DllImport (Constants.libcLibrary)] internal extern static int close (int fd); @@ -585,7 +585,8 @@ namespace CoreFoundation { if (path is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (path)); - fd = open (path, O_EVTONLY); + using var pathPtr = new TransientString (path); + fd = open (pathPtr, O_EVTONLY); if (fd == -1) throw new IOException ("Failure to open the file", Marshal.GetLastWin32Error ()); if (type_vnode == IntPtr.Zero) diff --git a/src/CoreFoundation/OSLog.cs b/src/CoreFoundation/OSLog.cs index 231dcce152..24d6052f7a 100644 --- a/src/CoreFoundation/OSLog.cs +++ b/src/CoreFoundation/OSLog.cs @@ -84,7 +84,7 @@ namespace CoreFoundation { extern static void os_release (IntPtr handle); [DllImport ("__Internal")] - extern static void xamarin_os_log (IntPtr logHandle, OSLogLevel level, string message); + extern static void xamarin_os_log (IntPtr logHandle, OSLogLevel level, IntPtr message); [Preserve (Conditional = true)] internal OSLog (NativeHandle handle, bool owns) @@ -112,7 +112,8 @@ namespace CoreFoundation { if (message is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (message)); - xamarin_os_log (Handle, level, message); + using var messagePtr = new TransientString (message); + xamarin_os_log (Handle, level, messagePtr); } } }