Skip to content

memory leak in DiagnosticBuilder.into_diagnostic() #69600

Closed

Description

@dwrensha

This code leaks the Box at self.0:

unsafe {
diagnostic = std::ptr::read(&self.0.diagnostic);
std::mem::forget(self);
};

The leak was introduced in 2fcd870. cc @nnethercote

Activity

  1. dwrensha commented on Mar 1, 2020

    @dwrensha
    ContributorAuthor

    We could avoid the leak and the unsafe by doing

    let diagnostic = self.0.diagnostic.clone();
    self.cancel();

    or if we're concerned about performance here, we could maybe do something with drop_in_place().

  2. added
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 1, 2020
  3. nnethercote commented on Mar 2, 2020

    @nnethercote
    Contributor

    Thanks for the report, I will measure the perf effects of the different alternatives.

    I'd like to know how you found this -- code inspection? Some kind of tool?

  4. dwrensha commented on Mar 2, 2020

    @dwrensha
    ContributorAuthor

    I found it while running fuzz-rustc and noticing that I kept running out of memory. I turned on -Z sanitizer=address and this popped out.

  5. dwrensha commented on Mar 2, 2020

    @dwrensha
    ContributorAuthor

    Another alternative could be to call std::mem::replace() to swap in a dummy diagnostic, instead of cloning the existing one.

  6. dwrensha commented on Mar 2, 2020

    @dwrensha
    ContributorAuthor

    Below is an example compiler input that triggers the leak:

    str// bass (FIXMoo {
    }
    
    
             )
  7. dwrensha commented on Mar 2, 2020

    @dwrensha
    ContributorAuthor

    Simpler example that triggers the leak: just a single closing brace

    }
  8. added a commit that references this issue on Mar 2, 2020
    99a595e
  9. added a commit that references this issue on Mar 2, 2020
    017a682
  10. added a commit that references this issue on Mar 3, 2020
    6a07f16
  11. added a commit that references this issue on Mar 3, 2020
    ef311d5
  12. added a commit that references this issue on Mar 5, 2020
    fff19a0
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

    C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions