Skip to content

Do not pass Utf8JsonReader by value #33772

Open

Description

@terrajobst

The rule should flag cases where a reader is passed around by value.

Category: Reliability

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. added
    code-fixer
    on Mar 21, 2020
  5. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Small
    • Fixer: Large
  6. removed
    untriaged
    on Mar 21, 2020
  7. carlossanlop commented on Jan 15, 2021

    @carlossanlop
    Contributor

    @terrajobst is this what you intend the analyzer/fixer to do?:
    Update: The answer below was "yes" :)

    Suggested severity: Warning

    Before

    static void Main()
    {
        Utf8JsonReader reader = new(/**/);
        MyMethod(reader);
    }
    
    static void MyMethod(Utf8JsonReader reader)
    {
    }

    After

    static void Main()
    {
        Utf8JsonReader reader = new(/**/);
        MyMethod(ref reader);
    }
    
    static void MyMethod(ref Utf8JsonReader reader)
    {
    }
  8. carlossanlop commented on Feb 23, 2021

    @carlossanlop
    Contributor

    Ping @terrajobst , @GrabYourPitchforks and @bartonjs to help clarify our question above.

  9. bartonjs commented on Feb 23, 2021

    @bartonjs
    Member

    Yep, the before/after is correct.

  10. GrabYourPitchforks commented on Feb 23, 2021

    @GrabYourPitchforks
    Member

    Is the fixer's behavior of "whenever you see a byval argument, change it to byref" viable? That likely won't catch local copies being made within a method.

  11. bartonjs commented on Feb 23, 2021

    @bartonjs
    Member

    The fixer doesn't have to fix everything.

    Method-call copies are probably almost always wrong. Local copies might be intentional, but if they're rare they can always suppress. (e.g. JsonDocument does a local copy to reset the ref if it hits an "I need more data" state)

  12. GrabYourPitchforks commented on Feb 23, 2021

    @GrabYourPitchforks
    Member

    Sounds reasonable. Carry on! :)

  13. 5 remaining items

  14. added
    api-approved
    and removed
    api-ready-for-review
    on Jun 3, 2021
  15. NewellClark commented on Aug 26, 2021

    @NewellClark
    Contributor

    I would like to be assigned to this, please.

  16. NewellClark commented on Sep 6, 2021

    @NewellClark
    Contributor

    I just ran my working prototype against dotnet/runtime and I found quite a few false positives in the System.Text.Json library. I found several false positives for the return by-value error in JsonElement.cs (EnumerateArray() and EnumerateObject()).
    I also found several pass by-value false positives in ThrowHelper.Serialization.cs.

    We may be able to improve the struct-enumerator heuristic to eliminate the return-by-value false positives. Currently, we recognize any nested value type that ends with "Enumerator" as a mutable value type, and we suppress diagnostics for methods named "GetEnumerator". We could also suppress methods containing "Enumerate", though I question whether that scenario is common enough to warrant special-casing. Might just be easier to suppress the warning in the runtime libraries.
    Alternatively, we could simply not show a warning for method return values, since methods are likely to be factory methods. All of the return value false positives in dotnet/runtime were factory methods. I think properties should still be reported, though.

    As for the pass by-value false positives, all of them are pass by-reference read-only parameters. I'm going to suggest that we not report diagnostics for "in" parameters. Since "in" is a language feature that beginners don't typically use, I think it's safe to assume that someone who marks a parameter as "in" likely knows what they're doing.

  17. added
    in-pr
    on Nov 23, 2021
  18. modified the milestones: Future, 8.0.0 on Nov 9, 2022
  19. buyaa-n commented on Aug 14, 2023

    @buyaa-n
    Contributor

    Moving to future milestone as the PR blocked with an open question

  20. modified the milestones: 8.0.0, Future on Aug 14, 2023
  21. modified the milestones: Future, 9.0.0 on Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Text.Jsoncode-analyzercode-fixerin-pr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions