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 S1751: Rule shouldn't raise on exceptions within while loops #682

Closed
jcurl opened this issue Aug 10, 2017 · 1 comment
Closed

Fix S1751: Rule shouldn't raise on exceptions within while loops #682

jcurl opened this issue Aug 10, 2017 · 1 comment
Assignees
Labels
Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@jcurl
Copy link

jcurl commented Aug 10, 2017

Description

See GitHub issue #590 . The following code snippet results in S1751 on the return statement. As the catch contains a way out, the loop exits and will reenter the while loop.

protected virtual FileStream CreateLock(string lockFileName, bool retries)
{
    if (retries) ResetRetryTimeout();
    FileStream filelock = null;
    while (true) {
        try {
            filelock = new FileStream(lockFileName,
                FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.DeleteOnClose);
            return filelock;
        } catch (Exception e) {
            int code = System.Runtime.InteropServices.Marshal.GetHRForException(e);
            if (code == unchecked((int)0x80070020) ||
                code == unchecked((int)0x80070021)) {
                // Sharing violation
                if (!retries) return null;
                if (!WaitRetryTimeout()) throw;
            } else {
                // All others are considered an error and we don't retry
                throw;
            }
        }
    };
}

Repro steps

See the code snippet above.

Expected behavior

Rule S1751 should not be raised

Actual behavior

S1751 is being raised as a false positive.

Known workarounds

None

Related information

  • SonarC# Version 3.4.0.1732 (10/Aug/2017)
  • Visual Studio Version VS2015 Enterprise
@Evangelink
Copy link
Contributor

Hi @jcurl. Thanks for the feedback, we will have a look at this soon.

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

2 participants