rust: discard hash context when finished
When we allocate a context but then abandon it, we never discard it,
which means that the underlying crypto library context may leak. This
doesn't happen with our default block code, but it may with OpenSSL.
Note that we do call git_hash_free, which frees the memory we called
from git_hash_alloc, but doesn't discard the underlying context itself.
This can be seen with the following command when compiling with OpenSSL
and running with nightly Rust:
RUSTFLAGS='-Z sanitizer=leak' cargo test
Discard the context in our context handler. Note that it is fine to do
so even after finalizing the context, so our final functions which take
self instead of &mut self will not mishandle memory.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
next
parent
251e7af992
commit
fdfcd7543e
|
|
@ -194,7 +194,10 @@ impl Clone for CryptoHasher {
|
|||
|
||||
impl Drop for CryptoHasher {
|
||||
fn drop(&mut self) {
|
||||
unsafe { c::git_hash_free(self.ctx) };
|
||||
unsafe {
|
||||
c::git_hash_discard(self.ctx);
|
||||
c::git_hash_free(self.ctx);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -356,6 +359,7 @@ pub mod c {
|
|||
pub fn git_hash_clone(dst: *mut c_void, src: *const c_void);
|
||||
pub fn git_hash_update(ctx: *mut c_void, inp: *const c_void, len: usize);
|
||||
pub fn git_hash_final(hash: *mut u8, ctx: *mut c_void);
|
||||
pub fn git_hash_discard(ctx: *mut c_void);
|
||||
pub fn git_hash_final_oid(hash: *mut c_void, ctx: *mut c_void);
|
||||
}
|
||||
}
|
||||
|
|
@ -450,6 +454,7 @@ mod tests {
|
|||
h.update(&data[2..]);
|
||||
|
||||
let h2 = h.clone();
|
||||
let h3 = h2.clone();
|
||||
|
||||
let actual_oid = h.into_oid();
|
||||
assert_eq!(**oid, actual_oid);
|
||||
|
|
@ -463,6 +468,7 @@ mod tests {
|
|||
|
||||
let actual_oid = h.into_oid();
|
||||
assert_eq!(**oid, actual_oid);
|
||||
std::mem::drop(h3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue