microsoft / microsoft/win32metadata

The friendly wrapper for `GetNamedSecurityInfo()` should return `ppSecurityDescriptor` as a `LocalFreeSafeHandle`.

Open
#2,168 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
1.5k
Forks
149
Avg merge
5d 16h
Merged PRs (30d)
4

Description

## Actual behavior

The [docs](https://learn.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-getnamedsecurityinfow) for this say: "A pointer to a variable that receives a pointer to the security descriptor of the object. When you have finished using the pointer, free the returned buffer by calling the [LocalFree](https://learn.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-localfree) function.".

## Expected behavior

That the friendly overload for this (and any) function returning a value that requires freeing/releasing of resources be wrapped in the appropriate SafeHandle type, especially if CsWin32 already provides the SafeHandle type.

## Repro steps

1. `NativeMethods.txt` content:
```
GetNamedSecurityInfo
```

2. `NativeMethods.json` content (if present): N/A

3. Any of your own code that should be shared?
```C#
internal unsafe static WIN32_ERROR GetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, OBJECT_SECURITY_INFORMATION SecurityInfo, out SafeNoReleaseHandle? ppsidOwner, out SafeNoReleaseHandle? ppsidGroup, out LocalFreeSafeHandle? ppDacl, out LocalFreeSafeHandle? ppSacl, out LocalFreeSafeHandle ppSecurityDescriptor)
{
fixed (char* pObjectNameLocal = pObjectName)
{
PSID psidOwner = default, pSidGroup = default; ACL* pDacl = null, pSacl = null; PSECURITY_DESCRIPTOR pSecurityDescriptor = default;
var res = PInvoke.GetNamedSecurityInfo(pObjectNameLocal, ObjectType, SecurityInfo, &psidOwner, &pSidGroup, &pDacl, &pSacl, &pSecurityDescriptor);
if (res != WIN32_ERROR.ERROR_SUCCESS)
{
throw ExceptionUtilities.GetExceptionForLastWin32Error(res);
}
if (pSecurityDescriptor == default)
{
throw new InvalidOperationException("Failed to retrieve security descriptor.");
}
ppsidOwner = psidOwner != default ? new((IntPtr)psidOwner.Value) : null;
ppsidGroup = pSidGroup != default ? new((IntPtr)pSidGroup.Value) : null;
ppDacl = pDacl is not null ? new((IntPtr)pDacl, false) : null;
ppSacl = pSacl is not null ? new((IntPtr)pSacl, false) : null;
ppSecurityDescriptor = new((IntPtr)pSecurityDescriptor, true);
return res;
}
}
```

### Context

- CsWin32 version: 0.3.253
- Win32Metadata version (if explicitly set by project): N/A
- Target Framework: net472
- `LangVersion` (if explicitly set by project): N/A

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the generation described with NativeMethods.txt containing GetNamedSecurityInfo, then inspect the friendly overload and the available LocalFreeSafeHandle type. Done means the ppSecurityDescriptor output uses LocalFreeSafeHandle and the generated wrapper correctly releases the returned security descriptor; verify the generated API with the C# repro shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.