<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NSspi
{
    internal static class PackageSupport
    {
        internal static SecPkgInfo GetPackageCapabilities( string packageName )
        {
            SecPkgInfo info;
            SecurityStatus status;
            SecurityStatus freeStatus;

            IntPtr rawInfoPtr;
            
            rawInfoPtr = new IntPtr();
            info = new SecPkgInfo();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            { }
            finally
            {
                status = NativeMethods.QuerySecurityPackageInfo( packageName, ref rawInfoPtr );

                if( status == SecurityStatus.OK && rawInfoPtr != IntPtr.Zero )
                {
                    Marshal.PtrToStructure( rawInfoPtr, info );
                    freeStatus = NativeMethods.FreeContextBuffer( rawInfoPtr );
                }
            }

            return info;
        }<commit_msg>Better protection against leaking handles - use a nested try-finally when allocating inside a CER. The inner finally protects against the case where the PtrToStructure, which allocates, fails. <commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NSspi
{
    internal static class PackageSupport
    {
        internal static SecPkgInfo GetPackageCapabilities( string packageName )
        {
            SecPkgInfo info;
            SecurityStatus status;
            SecurityStatus freeStatus;

            IntPtr rawInfoPtr;
            
            rawInfoPtr = new IntPtr();
            info = new SecPkgInfo();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            { }
            finally
            {
                status = NativeMethods.QuerySecurityPackageInfo( packageName, ref rawInfoPtr );

                if( status == SecurityStatus.OK && rawInfoPtr != IntPtr.Zero )
                {
                    try
                    {
                        // This performs allocations as it makes room for the strings contained in the SecPkgInfo class.
                        Marshal.PtrToStructure( rawInfoPtr, info );
                    }
                    finally
                    {
                        freeStatus = NativeMethods.FreeContextBuffer( rawInfoPtr );
                    }
                }
            }

            return info;
        }
{'fraction_non_alphanumeric': 0.060953084595493166, 'fraction_numerical': 0.0, 'mean_word_length': 1.6393762183235867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6840411', 'n_tokens_mistral': 632, 'n_tokens_neox': 609, 'n_words': 166}