Skip to content

C#: Improve dataflow for mutation definition and mutation operator calls.#21839

Closed
michaelnebel wants to merge 6 commits into
github:mainfrom
michaelnebel:csharp/mutationassignabledefinition
Closed

C#: Improve dataflow for mutation definition and mutation operator calls.#21839
michaelnebel wants to merge 6 commits into
github:mainfrom
michaelnebel:csharp/mutationassignabledefinition

Conversation

@michaelnebel
Copy link
Copy Markdown
Contributor

@michaelnebel michaelnebel commented May 12, 2026

In this PR, we improve data-flow for mutation operations and mutator operator calls.
Note, that c++ implicitly (if this is not an instance operator) the resulting value to c.

Comments on DCA

  • Performance looks good.
  • The only interesting result discrepancy is for cs/invalid-dynamic-call, which is a removal of a false positive (related to a dynamic call of --). However, I believe this is somewhat due to wobliness as there is another PR, which "introduces" a similar result in the mono project (which is a false positive). I am not able to reproduce with a local example.

@github-actions github-actions Bot added the C# label May 12, 2026
@michaelnebel michaelnebel force-pushed the csharp/mutationassignabledefinition branch 2 times, most recently from 78ff6ae to cb29538 Compare May 19, 2026 14:04
@michaelnebel michaelnebel marked this pull request as ready for review May 20, 2026 13:27
@michaelnebel michaelnebel requested a review from a team as a code owner May 20, 2026 13:27
Copilot AI review requested due to automatic review settings May 20, 2026 13:27
@michaelnebel michaelnebel requested a review from hvitved May 20, 2026 13:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the C# dataflow/SSA modeling around mutation operations (++, --) and mutator operator calls, and refreshes the affected library-test baselines accordingly.

Changes:

  • Extend assignable/mutation definition handling to cover additional mutation-operator call cases.
  • Adjust sign-analysis SSA assignment extraction to avoid treating mutator operations as simple assignments.
  • Add/extend operator dataflow library tests and update corresponding .expected outputs.
Show a summary per file
File Description
csharp/ql/test/library-tests/fields/Fields11.expected Updated baseline for field-flow expectations impacted by mutation handling.
csharp/ql/test/library-tests/dataflow/operators/operatorFlow.expected Updated operator-flow graph baseline with new mutator operator edges/nodes.
csharp/ql/test/library-tests/dataflow/operators/Operator.cs Adds user-defined decrement operator + new flow assertions for -- scenarios.
csharp/ql/test/library-tests/dataflow/local/TaintTrackingStep.expected Updated baseline to include SSA steps for decrement operations.
csharp/ql/test/library-tests/dataflow/local/DataFlowStep.expected Updated baseline to include dataflow steps for decrement operations.
csharp/ql/test/library-tests/csharp7/LocalTaintFlow.expected Updated baseline for taint steps involving increment operations.
csharp/ql/test/library-tests/assignables/GetAnAssignedValue.expected Updated baseline to treat mutation operations as assigned-value sources.
csharp/ql/test/library-tests/assignables/AssignableDefinition.expected Updated baseline for mutation definitions to record the mutation op as the source.
csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll Tweaks SSA-to-expression extraction to exclude mutator-operation defining expressions.
csharp/ql/lib/semmle/code/csharp/Assignable.qll Introduces mutation-operation assignment abstraction and updates mutation-definition plumbing.
csharp/ql/lib/change-notes/2026-05-19-mutation-operator-dataflow.md Adds a change note entry (currently duplicates an existing one).

Copilot's findings

  • Files reviewed: 11/11 changed files
  • Comments generated: 3

var x = new C1();
var y = x--;
Sink(x.Field); // $ hasValueFlow=2
Sink(y.Field); // $ hasValueFlow=2
}

/**
* A mutation operation that implicitly assigns the result to its operand. For example, `a++` in
---
category: minorAnalysis
---
* C# 14: Added support for user-defined instance increment/decrement operators.
@michaelnebel michaelnebel marked this pull request as draft May 20, 2026 13:44
@michaelnebel michaelnebel marked this pull request as ready for review May 20, 2026 13:45
@michaelnebel michaelnebel marked this pull request as draft May 20, 2026 13:46
@michaelnebel michaelnebel force-pushed the csharp/mutationassignabledefinition branch from cb29538 to be7f7a4 Compare May 20, 2026 13:52
Comment thread csharp/ql/lib/semmle/code/csharp/exprs/Dynamic.qll Fixed
@michaelnebel
Copy link
Copy Markdown
Contributor Author

michaelnebel commented May 20, 2026

@hvitved : No need to review this as it shouldn't be merged.
We could of course consider to do this for pre increment/decrement operator calls and expressions, but I think that might cause confusion.

@michaelnebel michaelnebel force-pushed the csharp/mutationassignabledefinition branch from be7f7a4 to dec76af Compare May 20, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants