about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src
diff options
context:
space:
mode:
authorAndrew Sun <me@andrewsun.com>2021-01-08 11:50:21 -0500
committerAndrew Sun <me@andrewsun.com>2021-01-08 11:50:21 -0500
commit80ca198212e967684557075b2f86b44e18048c70 (patch)
treeb4c627032c310bdeba7db3721acce3622d1ab771 /compiler/rustc_codegen_llvm/src
parentbc4c5bac408f96db0a0e1c8934ff0c403b3986d3 (diff)
downloadrust-80ca198212e967684557075b2f86b44e18048c70.tar.gz
rust-80ca198212e967684557075b2f86b44e18048c70.zip
Check if the pointer is null/string is not utf8
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
-rw-r--r--compiler/rustc_codegen_llvm/src/llvm_util.rs27
1 files 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 e0771313f76..a9d57ea8b8a 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<String> {
                 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![],
     }