Skip to content

Commit 80ca198

Browse files
committed
Check if the pointer is null/string is not utf8
1 parent bc4c5ba commit 80ca198

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,25 @@ pub fn handle_native_features(sess: &Session) -> Vec<String> {
228228
return vec![];
229229
}
230230

231-
let ptr = unsafe { llvm::LLVMGetHostCPUFeatures() };
232-
let str = unsafe { CStr::from_ptr(ptr).to_string_lossy() };
233-
234-
let features = str.split(",").map(|s| s.to_owned()).collect();
235-
236-
unsafe { llvm::LLVMDisposeMessage(ptr) };
237-
238-
features
231+
let features_string = unsafe {
232+
let ptr = llvm::LLVMGetHostCPUFeatures();
233+
let features_string = if !ptr.is_null() {
234+
CStr::from_ptr(ptr)
235+
.to_str()
236+
.unwrap_or_else(|e| {
237+
bug!("LLVM returned a non-utf8 features string: {}", e);
238+
})
239+
.to_owned()
240+
} else {
241+
bug!("could not allocate host CPU features, LLVM returned a `null` string");
242+
};
243+
244+
llvm::LLVMDisposeMessage(ptr);
245+
246+
features_string
247+
};
248+
249+
features_string.split(",").map(|s| s.to_owned()).collect()
239250
}
240251
None => vec![],
241252
}

0 commit comments

Comments
 (0)