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 S4158 FP: Variable instance mismatch #2147

Closed
Corniel opened this issue Dec 11, 2018 · 4 comments · Fixed by #7428
Closed

Fix S4158 FP: Variable instance mismatch #2147

Corniel opened this issue Dec 11, 2018 · 4 comments · Fixed by #7428
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@Corniel
Copy link
Contributor

Corniel commented Dec 11, 2018

Description

public virtual object Clone()
{
    var clonedEntry = (Entry)MemberwiseClone();
    clonedEntry._id = Guid.NewGuid();
    clonedEntry._lines = new List<Line>();
   _lines.ForEach(line => clonedEntry.AddLine(line.Clone() as Line));
    return clonedEntry;
}

Will falsely raise. It seems to be different from earlier raised issue.

Workarround

public virtual object Clone()
{
    var clonedEntry = (Entry)MemberwiseClone();
    clonedEntry._id = Guid.NewGuid();
    clonedEntry._lines = new List<Line>();
    forach(var line in clonedEntry._lines )
   {
       clonedEntry.AddLine(line.Clone() as Line);
   }
    return clonedEntry;
}

Related information

  • 7.9.1.7622
  • Visual Studio 14.0
@valhristov valhristov added Area: Symbolic Execution Type: False Positive Rule IS triggered when it shouldn't be. labels Dec 14, 2018
@valhristov valhristov added this to the CFG + Symbolic Execution milestone Dec 14, 2018
@christophe-zurn-sonarsource
Copy link
Contributor

christophe-zurn-sonarsource commented Aug 6, 2019

As we are currently working on CFG/Symbolic Execution, I investigated a bit more this issue to understand the underlying problem.
For future reference: symbolic execution currently only keeps track of fields in the current class being analyzed. However, symbolic values of objects (variables, fields, parameters, ...) are mapped to their symbols (https://docs.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.isymbol?view=roslyn-dotnet). This is what is causing the issue: when an instance of the current class being analyzed is created within a method, the fields of the newly created instance and the current one are aliased since they are represented by the same ISymbol object. Fixing this implies changing how symbolic values are stored and mapped inside the ProgramState.

Here are two reproducers to illustrate this:

    class A
    {
        public List<string> _lines;

        public void doSomething()
        {
            var obj1 = new A();
            obj1._lines = new List<string>(); // obj1._lines and this._lines are both represented by the 'A#List<string>_lines' symbol
            _lines.ForEach(line => obj1._lines.Add(line)); // FP
        }
    }

    class B
    {
        public List<string> _lines;

        public void doSomething()
        {
            var obj1 = new B();
            var obj2 = new B();
            obj1._lines = new List<string>();
            obj1._lines.Add("str");
            obj2._lines = new List<string>();
            obj1._lines.ForEach(line => obj2._lines.Add(line)); // FP
        }
    }

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Area: CFG/SE CFG and SE related issues. and removed Area: Symbolic Execution labels Nov 29, 2019
@costin-zaharia-sonarsource costin-zaharia-sonarsource added the Area: C# C# rules related issues. label Dec 2, 2019
@costin-zaharia-sonarsource costin-zaharia-sonarsource removed this from the CFG + Symbolic Execution milestone Dec 2, 2019
@Thieum
Copy link

Thieum commented Feb 13, 2020

Another false positive case for S4158 involving local method and event:

        public void RemoveErrorsTest()
        {
            // Arrange
            var module = new TestModule();

            var errorsToRemove = new List<int>();
            var RemovedErrors = new List<ApplicationModuleError>();

            void Module_ErrorsRemovingEnded(object sender, ApplicationModuleErrorsRemovedEventArgs e)
            {
                errorsToRemove.AddRange(e.ErrorsToRemove);
                RemovedErrors.AddRange(e.RemovedErrors);
            }

            module.ErrorsRemovingEnded += Module_ErrorsRemovingEnded;

            // Act
            module.RemoveErrors(TestErrors.TestError0);

            // Assert
            Assert.AreEqual((int)TestErrors.TestError0, errorsToRemove[0]); // <-- False positive here
            Assert.AreEqual(0, RemovedErrors.Count);
        }

@stebla27
Copy link

stebla27 commented Feb 8, 2021

Had the same problem! When can I expect a fix?
The issue is very old (2018) and a little bit annoying!

var clone = MemberwiseClone() as MyClass;
clone.myList= new List<Item>();
myList.ForEach(item => clone.myList.Add(item.Clone() as Item));

@pavel-mikula-sonarsource
Copy link
Contributor

Hi @stebla27,

We don't have any ETA to fix reported issues. Prerequisite for this to be fixed is reimplementing our Symbolic Execution Engine that will be done in MMF-2229 in 2021. That will directly fix or unblock all issues related to CFG and SE.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: False Positive Rule IS triggered when it shouldn't be. labels Jun 25, 2021
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title False postive on S4158: variable mismatch S4158 FP: variable mismatch Oct 14, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 9.3 milestone May 30, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource modified the milestones: 9.3, 9.4 Jun 6, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S4158 FP: variable mismatch Fix S4158 FP: variable mismatch Jun 14, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Fix S4158 FP: variable mismatch Fix S4158 FP: Variable instance mismatch Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants