1From 4eba55790224dd43e88d6a4bbf4fea826a59498f Mon Sep 17 00:00:00 2001
2From: Christopher Ferris <[email protected]>
3Date: Fri, 13 Jan 2023 06:30:08 +0000
4Subject: [PATCH] Fix memory leak.
5
6The buffer to hold the demangled name is allocated before the
7name is demangled. If the parse fails, this buffer is not
8deallocated, leaking it. Only create the buffer when the parse
9passes.
10
11Test: Ran address sanitizer on host and verified this leaks without
12Test: the fix and does not with the fix.
13Change-Id: I3ee20727972fc511f63aae2b50f5a34f0c792a5f
14---
15 src/lib.rs | 3 +--
16 1 file changed, 1 insertion(+), 2 deletions(-)
17
18diff --git a/src/lib.rs b/src/lib.rs
19index 7610145..d220ec1 100644
20--- a/src/lib.rs
21+++ b/src/lib.rs
22@@ -163,10 +163,9 @@ unsafe fn rustc_demangle_native(
23         }
24     }
25
26-    let mut out_buf = SystemBuffer::from_raw(out, out_size)?;
27-
28     match rustc_demangle::try_demangle(mangled_str) {
29         Ok(demangle) => {
30+            let mut out_buf = SystemBuffer::from_raw(out, out_size)?;
31             while write!(out_buf.as_mut_slice(), "{:#}\0", demangle).is_err() {
32                 out_buf.resize()?;
33             }
34--
352.39.0.246.g2a6d74b583-goog
36
37