From 80ca198212e967684557075b2f86b44e18048c70 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Fri, 8 Jan 2021 11:50:21 -0500 Subject: [PATCH] Check if the pointer is null/string is not utf8 --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index e0771313f764b..a9d57ea8b8aa4 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -228,14 +228,25 @@ pub fn handle_native_features(sess: &Session) -> Vec { return vec![]; } - let ptr = unsafe { llvm::LLVMGetHostCPUFeatures() }; - let str = unsafe { CStr::from_ptr(ptr).to_string_lossy() }; - - let features = str.split(",").map(|s| s.to_owned()).collect(); - - unsafe { llvm::LLVMDisposeMessage(ptr) }; - - features + let features_string = unsafe { + let ptr = llvm::LLVMGetHostCPUFeatures(); + let features_string = if !ptr.is_null() { + CStr::from_ptr(ptr) + .to_str() + .unwrap_or_else(|e| { + bug!("LLVM returned a non-utf8 features string: {}", e); + }) + .to_owned() + } else { + bug!("could not allocate host CPU features, LLVM returned a `null` string"); + }; + + llvm::LLVMDisposeMessage(ptr); + + features_string + }; + + features_string.split(",").map(|s| s.to_owned()).collect() } None => vec![], }