Skip to content

Commit e4eef4a

Browse files
AliveDevildkocher
authored andcommitted
Make string -> PCWSTR -> PWSTR dangerous
1 parent 7174fa5 commit e4eef4a

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Windows.Win32.Foundation;
2+
3+
namespace Windows.Win32;
4+
5+
public static partial class Foundation_PWSTR_Extensions
6+
{
7+
8+
public static unsafe PWSTR DangerousAsPWSTR(this in PCWSTR pwstr) => pwstr.Value;
9+
10+
}

core/src/main/csharp/Windows/Win32/PWSTR.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ namespace Windows.Win32.Foundation;
66

77
public unsafe partial struct PWSTR
88
{
9-
public static PWSTR DangerousFromString(in ReadOnlySpan<char> value)
9+
public static PWSTR DangerousFromString(in Span<char> value)
1010
{
1111
return (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(value));
1212
}
1313

14-
public static PWSTR DangerousFromString(string value) => DangerousFromString(value.AsSpan());
15-
1614
public string ToString(int length) => Value is null ? null : new(Value, 0, length);
1715
}

core/src/main/csharp/ch/cyberduck/core/credentialmanager/WinCredentialManager.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Net;
2525
using System.Runtime.InteropServices;
2626
using System.Text;
27+
using Windows.Win32;
2728
using Windows.Win32.Foundation;
2829
using Windows.Win32.Security.Credentials;
2930
using static System.Runtime.CompilerServices.Unsafe;
@@ -142,8 +143,8 @@ public static unsafe bool SaveCredentials(string target, WindowsCredentialManage
142143
{
143144
var cred = new CREDENTIALW
144145
{
145-
TargetName = PWSTR.DangerousFromString(target),
146-
UserName = PWSTR.DangerousFromString(credential.UserName),
146+
TargetName = PCWSTR.DangerousFromString(target).DangerousAsPWSTR(),
147+
UserName = PCWSTR.DangerousFromString(credential.UserName).DangerousAsPWSTR(),
147148
Type = credential.Type,
148149
Flags = credential.Flags,
149150
Persist = credential.Persist,
@@ -178,7 +179,7 @@ public static unsafe bool SaveCredentials(string target, WindowsCredentialManage
178179
var key = string.Format(formatString, item.Key, innerIndex);
179180
attributes.Memory.Span[index] = new CREDENTIAL_ATTRIBUTEW()
180181
{
181-
Keyword = PWSTR.DangerousFromString(key),
182+
Keyword = PCWSTR.DangerousFromString(key).DangerousAsPWSTR(),
182183
ValueSize = (uint)length,
183184
Value = ((byte*)wstr.Value) + i
184185
};

core/src/main/csharp/ch/cyberduck/core/local/ShellApplicationFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public class ProgIdApplication : Application, IInvokeApplication, WindowsApplica
232232
{
233233
public ProgIdApplication(string identifier, string name, string defaultIcon) : base(identifier, name)
234234
{
235-
PWSTR pszIconFile = PWSTR.DangerousFromString(defaultIcon);
235+
PWSTR pszIconFile = PCWSTR.DangerousFromString(defaultIcon).DangerousAsPWSTR();
236236
IconIndex = PathParseIconLocation(pszIconFile);
237237
IconPath = pszIconFile.ToString();
238238
}

0 commit comments

Comments
 (0)