rust-lang/rust

memory leak in DiagnosticBuilder.into_diagnostic()

dwrensha opened this issue ยท 6 comments

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

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().

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?

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.

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

Below is an example compiler input that triggers the leak:

str// bass (FIXMoo {
}


         )

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

}