Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S3459: False Positive with interop methods #242

Closed
nnpcYvIVl opened this issue Apr 26, 2017 · 5 comments
Closed

Fix S3459: False Positive with interop methods #242

nnpcYvIVl opened this issue Apr 26, 2017 · 5 comments
Assignees
Labels
Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@nnpcYvIVl
Copy link

S3459 is thrown on ullTotalPhys even though the Windows API function GlobalMemoryStatusEx sets it before use.

public static class Memory
{
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private class MEMORYSTATUSEX
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public ulong ullTotalPhys;
        public ulong ullAvailPhys;
        public ulong ullTotalPageFile;
        public ulong ullAvailPageFile;
        public ulong ullTotalVirtual;
        public ulong ullAvailVirtual;
        public ulong ullAvailExtendedVirtual;
        public MEMORYSTATUSEX()
        {
            this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
        }
    }

    public static ulong TotalPhysicalMemory
    {
        get
        {
            MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX();
            if (GlobalMemoryStatusEx(memStatus))
            {
                return memStatus.ullTotalPhys;
            }
            else
            {
                return 0;
            }
        }
    }
}
@Evangelink Evangelink added Area: Rules Type: False Positive Rule IS triggered when it shouldn't be. labels Apr 27, 2017
@Evangelink
Copy link
Contributor

Hi @nnpcYvIVl! Thanks for reporting this FP.

@Evangelink Evangelink changed the title S3459 False Positive Fix S3459: False Positive with interop methods Apr 28, 2017
@Evangelink
Copy link
Contributor

RSPEC-3459

@nnpcYvIVl
Copy link
Author

I'm still getting this S3459 false positive with the latest version of SonarLint. Can you verify that the patch applied fixed the code sample in this ticket?

@Evangelink
Copy link
Contributor

Hi @nnpcYvIVl,

I confirm that there is still one case not handled there with ullTotalPhys. Sorry for the inconvenience... I have created another ticket so you can follow progress!

Cheers,
Amaury

@nnpcYvIVl
Copy link
Author

@Evangelink Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

3 participants