Skip to content

Annotate credential output buffers with CredFree ownership - #2389

Open
Jevan Saks (jevansaks) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-credfree-annotations
Open

Jevan Saks (jevansaks) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-credfree-annotations

Conversation

Copilot AI commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Credential APIs lack output ownership annotations, so CsWin32 projects raw pointers instead of owned outputs released through CredFree.

  • Ownership metadata: Add [FreeWith("CredFree")] entries in emitter.settings.rsp for A/W variants of CredRead, CredEnumerate, CredGetTargetInfo, and CredReadDomainCredentials.
  • Related outputs: Apply the same annotation to documented allocations from CredMarshalCredential, CredUnmarshalCredential, and CredFindBestCredential.
CredReadW::Credential=[FreeWith("CredFree")]
CredMarshalCredentialW::MarshaledCredential=[FreeWith("CredFree")]

Co-authored-by: jevansaks <20667023+jevansaks@users.noreply.github.com>
Copilot AI changed the title [WIP] Add CredRead, CredEnumerate annotations for CredFree Annotate credential output buffers with CredFree ownership Oct 26, 2026
@github-actions

Copy link
Copy Markdown

📋 API Surface Diff

+14 additions / -14 deletions vs main branch (full validation run)

Click to expand API diff
diff --git "a/bin\\baseline.apidump.cs" "b/bin\\current.apidump.cs"
index bc6e749a..65da4c6f 100644
--- "a/bin\\baseline.apidump.cs"
+++ "b/bin\\current.apidump.cs"
@@ -516501,25 +516501,25 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credreadw")]
-	public unsafe static extern BOOL CredReadW ([In][Const] PWSTR TargetName, [In] CRED_TYPE Type, [Optional][Reserved] uint Flags, [Out] CREDENTIALW** Credential);
+	public unsafe static extern BOOL CredReadW ([In][Const] PWSTR TargetName, [In] CRED_TYPE Type, [Optional][Reserved] uint Flags, [Out][FreeWith ("CredFree")] CREDENTIALW** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credreada")]
-	public unsafe static extern BOOL CredReadA ([In][Const] PSTR TargetName, [In] CRED_TYPE Type, [Optional][Reserved] uint Flags, [Out] CREDENTIALA** Credential);
+	public unsafe static extern BOOL CredReadA ([In][Const] PSTR TargetName, [In] CRED_TYPE Type, [Optional][Reserved] uint Flags, [Out][FreeWith ("CredFree")] CREDENTIALA** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credenumeratew")]
-	public unsafe static extern BOOL CredEnumerateW ([Optional][In][Const] PWSTR Filter, [Optional][Reserved] CRED_ENUMERATE_FLAGS Flags, [Out] uint* Count, [Out] CREDENTIALW*** Credential);
+	public unsafe static extern BOOL CredEnumerateW ([Optional][In][Const] PWSTR Filter, [Optional][Reserved] CRED_ENUMERATE_FLAGS Flags, [Out] uint* Count, [Out][FreeWith ("CredFree")] CREDENTIALW*** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credenumeratea")]
-	public unsafe static extern BOOL CredEnumerateA ([Optional][In][Const] PSTR Filter, [Optional][Reserved] CRED_ENUMERATE_FLAGS Flags, [Out] uint* Count, [Out] CREDENTIALA*** Credential);
+	public unsafe static extern BOOL CredEnumerateA ([Optional][In][Const] PSTR Filter, [Optional][Reserved] CRED_ENUMERATE_FLAGS Flags, [Out] uint* Count, [Out][FreeWith ("CredFree")] CREDENTIALA*** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
@@ -516537,13 +516537,13 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credreaddomaincredentialsw")]
-	public unsafe static extern BOOL CredReadDomainCredentialsW ([In] CREDENTIAL_TARGET_INFORMATIONW* TargetInfo, [In] uint Flags, [Out] uint* Count, [Out] CREDENTIALW*** Credential);
+	public unsafe static extern BOOL CredReadDomainCredentialsW ([In] CREDENTIAL_TARGET_INFORMATIONW* TargetInfo, [In] uint Flags, [Out] uint* Count, [Out][FreeWith ("CredFree")] CREDENTIALW*** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credreaddomaincredentialsa")]
-	public unsafe static extern BOOL CredReadDomainCredentialsA ([In] CREDENTIAL_TARGET_INFORMATIONA* TargetInfo, [In] uint Flags, [Out] uint* Count, [Out] CREDENTIALA*** Credential);
+	public unsafe static extern BOOL CredReadDomainCredentialsA ([In] CREDENTIAL_TARGET_INFORMATIONA* TargetInfo, [In] uint Flags, [Out] uint* Count, [Out][FreeWith ("CredFree")] CREDENTIALA*** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
@@ -516573,37 +516573,37 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credgettargetinfow")]
-	public unsafe static extern BOOL CredGetTargetInfoW ([In][Const] PWSTR TargetName, [In] uint Flags, [Out] CREDENTIAL_TARGET_INFORMATIONW** TargetInfo);
+	public unsafe static extern BOOL CredGetTargetInfoW ([In][Const] PWSTR TargetName, [In] uint Flags, [Out][FreeWith ("CredFree")] CREDENTIAL_TARGET_INFORMATIONW** TargetInfo);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credgettargetinfoa")]
-	public unsafe static extern BOOL CredGetTargetInfoA ([In][Const] PSTR TargetName, [In] uint Flags, [Out] CREDENTIAL_TARGET_INFORMATIONA** TargetInfo);
+	public unsafe static extern BOOL CredGetTargetInfoA ([In][Const] PSTR TargetName, [In] uint Flags, [Out][FreeWith ("CredFree")] CREDENTIAL_TARGET_INFORMATIONA** TargetInfo);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credmarshalcredentialw")]
-	public unsafe static extern BOOL CredMarshalCredentialW ([In] CRED_MARSHAL_TYPE CredType, [In] void* Credential, [Out] PWSTR* MarshaledCredential);
+	public unsafe static extern BOOL CredMarshalCredentialW ([In] CRED_MARSHAL_TYPE CredType, [In] void* Credential, [Out][FreeWith ("CredFree")] PWSTR* MarshaledCredential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credmarshalcredentiala")]
-	public unsafe static extern BOOL CredMarshalCredentialA ([In] CRED_MARSHAL_TYPE CredType, [In] void* Credential, [Out] PSTR* MarshaledCredential);
+	public unsafe static extern BOOL CredMarshalCredentialA ([In] CRED_MARSHAL_TYPE CredType, [In] void* Credential, [Out][FreeWith ("CredFree")] PSTR* MarshaledCredential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credunmarshalcredentialw")]
-	public unsafe static extern BOOL CredUnmarshalCredentialW ([In][Const] PWSTR MarshaledCredential, [Out] CRED_MARSHAL_TYPE* CredType, [Out] void** Credential);
+	public unsafe static extern BOOL CredUnmarshalCredentialW ([In][Const] PWSTR MarshaledCredential, [Out] CRED_MARSHAL_TYPE* CredType, [Out][FreeWith ("CredFree")] void** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credunmarshalcredentiala")]
-	public unsafe static extern BOOL CredUnmarshalCredentialA ([In][Const] PSTR MarshaledCredential, [Out] CRED_MARSHAL_TYPE* CredType, [Out] void** Credential);
+	public unsafe static extern BOOL CredUnmarshalCredentialA ([In][Const] PSTR MarshaledCredential, [Out] CRED_MARSHAL_TYPE* CredType, [Out][FreeWith ("CredFree")] void** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.1.2600")]
@@ -516681,13 +516681,13 @@ public static class Apis
 	[SupportedOSPlatform ("windows6.0.6000")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credfindbestcredentialw")]
-	public unsafe static extern BOOL CredFindBestCredentialW ([In][Const] PWSTR TargetName, [In] uint Type, [In] uint Flags, [Out] CREDENTIALW** Credential);
+	public unsafe static extern BOOL CredFindBestCredentialW ([In][Const] PWSTR TargetName, [In] uint Type, [In] uint Flags, [Out][FreeWith ("CredFree")] CREDENTIALW** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows6.0.6000")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wincred/nf-wincred-credfindbestcredentiala")]
-	public unsafe static extern BOOL CredFindBestCredentialA ([In][Const] PSTR TargetName, [In] uint Type, [In] uint Flags, [Out] CREDENTIALA** Credential);
+	public unsafe static extern BOOL CredFindBestCredentialA ([In][Const] PSTR TargetName, [In] uint Type, [In] uint Flags, [Out][FreeWith ("CredFree")] CREDENTIALA** Credential);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]

This comment is automatically updated on each push.

@jevansaks
Jevan Saks (jevansaks) enabled auto-merge (squash) October 26, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CredRead, CredEnumerate and related functions are missing [FreeWith("CredFree")] annotations

2 participants