[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Fix memory leak in CryptoDigest type



commit 3a640520996ae45ed32f483432af735187bb7ee5
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Wed Jun 20 17:28:28 2018 -0400

    Fix memory leak in CryptoDigest type
    
    If you're owning a C pointer, you need to implement Drop.
---
 src/rust/external/crypto_digest.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/rust/external/crypto_digest.rs b/src/rust/external/crypto_digest.rs
index 4eae1550a..3e8801f20 100644
--- a/src/rust/external/crypto_digest.rs
+++ b/src/rust/external/crypto_digest.rs
@@ -140,7 +140,7 @@ extern "C" {
     fn crypto_digest_new() -> *mut crypto_digest_t;
     fn crypto_digest256_new(algorithm: digest_algorithm_t) -> *mut crypto_digest_t;
     fn crypto_digest512_new(algorithm: digest_algorithm_t) -> *mut crypto_digest_t;
-    fn crypto_digest_free(digest: *mut crypto_digest_t);
+    fn crypto_digest_free_(digest: *mut crypto_digest_t);
     fn crypto_digest_add_bytes(digest: *mut crypto_digest_t, data: *const c_char, len: size_t);
     fn crypto_digest_get_digest(digest: *mut crypto_digest_t, out: *mut c_char, out_len: size_t);
     fn crypto_digest_dup(digest: *const crypto_digest_t) -> *mut crypto_digest_t;
@@ -292,6 +292,14 @@ impl CryptoDigest {
     }
 }
 
+impl Drop for CryptoDigest {
+    fn drop(&mut self) {
+        unsafe {
+            crypto_digest_free_(self.0 as *mut crypto_digest_t);
+        }
+    }
+}
+
 /// Get the 256-bit digest output of a `crypto_digest_t`.
 ///
 /// # Inputs



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits