From bbdaa9873176e8893068cf0ef88730a1f80529eb Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sat, 2 Oct 2021 22:07:23 -0500 Subject: [PATCH] obj2txt safety comment --- stdlib/src/ssl.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/src/ssl.rs b/stdlib/src/ssl.rs index c5bf0c89b..98853d75b 100644 --- a/stdlib/src/ssl.rs +++ b/stdlib/src/ssl.rs @@ -130,7 +130,7 @@ fn nid2obj(nid: Nid) -> Option { fn obj2txt(obj: &Asn1ObjectRef, no_name: bool) -> Option { let no_name = if no_name { 1 } else { 0 }; let ptr = obj.as_ptr(); - let s = unsafe { + let b = unsafe { let buflen = sys::OBJ_obj2txt(std::ptr::null_mut(), 0, ptr, no_name); assert!(buflen >= 0); if buflen == 0 { @@ -145,11 +145,12 @@ fn obj2txt(obj: &Asn1ObjectRef, no_name: bool) -> Option { no_name, ); assert!(ret >= 0); - // SAFETY: set_len is safe when capacity is enoguh and all values are already initialized + // SAFETY: OBJ_obj2txt initialized the buffer successfully buf.set_len(buflen); - String::from_utf8(buf) - .unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned()) + buf }; + let s = + String::from_utf8(b).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned()); Some(s) }