Skip to content

Do not use [Out] string for P/Invokes #35692

Closed

Description

@elinor-fung

Add a rule to flag P/Invoke declarations passing string parameters by value with the [Out] attribute, which can destabilize the runtime if the string is an interned string.

 // Flag [Out] string
[DllImport("MyLibrary")]
private static extern void Foo([Out] string s);

// OK
[DllImport("MyLibrary")]
private static extern void Bar(out string s1, [Out] ref string s2);

Category: Interoperability
Default: Enabled

cc @terrajobst @stephentoub @AaronRobinsonMSFT @jkoritzinsky

Activity

  1. added
    api-suggestion
    code-analyzer
    on May 1, 2020
  2. added
    untriaged
    on May 1, 2020
  3. stephentoub commented on May 1, 2020

    @stephentoub
    Member

    Is there any situation in which this is considered valid? It's akin to pinning a string and then writing into it, right? I'm wondering if in addition to an analyzer (which I agree is warranted; I didn't realize we allowed this), .NET 5 should simply throw an exception at runtime if you try to do this?

  4. AaronRobinsonMSFT commented on May 1, 2020

    @AaronRobinsonMSFT
    Member

    .NET 5 should simply throw an exception at runtime if you try to do this?

    I believe @jkoritzinsky added this.

  5. elinor-fung commented on May 11, 2020

    @elinor-fung
    MemberAuthor

    @terrajobst @stephentoub Any concerns with us going ahead with this? Is there a specific/official approval we should ask for?

  6. removed
    untriaged
    on Jun 8, 2020
  7. added this to the 5.0 milestone on Jun 8, 2020
  8. added and removed
    api-suggestion
    on Jun 24, 2020
  9. added
    api-ready-for-review
    and removed on Jul 6, 2020
  10. terrajobst commented on Jul 15, 2020

    @terrajobst
    Contributor

    @terrajobst @stephentoub Any concerns with us going ahead with this? Is there a specific/official approval we should ask for?

    Approval for which part? Throwing the exception, the analyzer or both? I thinking doing both seems great to me.

  11. elinor-fung commented on Jul 15, 2020

    @elinor-fung
    MemberAuthor

    @terrajobst Approval for the analyzer. We were told we would need to go through an official API review process via marking this issue as api-ready-for-review. Is that still the case - should we be marking it differently or are we no longer expecting an API review for analyzers?

  12. added
    api-approved
    and removed
    api-ready-for-review
    on Jul 16, 2020
  13. terrajobst commented on Jul 16, 2020

    @terrajobst
    Contributor

    Video

    The analyzer/fixer makes sense.

  14. elinor-fung commented on Jul 28, 2020

    @elinor-fung
    MemberAuthor

    Analyzer was added with dotnet/roslyn-analyzers#3879
    Doc has also been added now: MicrosoftDocs/visualstudio-docs@df17e88

  15. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Runtime.InteropServicescode-analyzer

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions