Rust: memory leak in DiagnosticBuilder.into_diagnostic()

Created on 1 Mar 2020  路  6Comments  路  Source: rust-lang/rust

This code leaks the Box at self.0:

https://github.com/rust-lang/rust/blob/d3c79346a3e7ddbb5fb417810f226ac5a9209007/src/librustc_errors/diagnostic_builder.rs#L141-L144

The leak was introduced in 2fcd870711ce267c79408ec631f7eba8e0afcdf6. cc @nnethercote

C-bug T-compiler

Most helpful comment

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

All 6 comments

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

}
Was this page helpful?
0 / 5 - 0 ratings