diff options
| author | Jubilee <workingjubilee@gmail.com> | 2024-10-28 10:18:52 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-28 10:18:52 -0700 |
| commit | bd43f8e9fdda660eb0165c87c270aba189bd5a95 (patch) | |
| tree | b5131fe0d980d059246d0b45f3bb7a5dac534801 /compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs | |
| parent | 6ea83ffe2ca3cbe576bf8b1dcd25a353da2374e6 (diff) | |
| parent | 4bd84b23a8537314132e98b9fb2c3fea2cb57496 (diff) | |
| download | rust-bd43f8e9fdda660eb0165c87c270aba189bd5a95.tar.gz rust-bd43f8e9fdda660eb0165c87c270aba189bd5a95.zip | |
Rollup merge of #132260 - Zalathar:type-safe-cast, r=compiler-errors
cg_llvm: Use a type-safe helper to cast `&str` and `&[u8]` to `*const c_char` In `rustc_codegen_llvm` there are many uses of `.as_ptr().cast()` to convert a string or byte-slice to `*const c_char`, which then gets passed through FFI. This works, but is fragile, because there's nothing constraining the pointer cast to actually be from `u8` to `c_char`. If the original value changes to something else that has an `as_ptr` method, or the context changes to expect something other than `c_char`, the cast will silently do the wrong thing. By making the cast more explicit via a helper method, we can be sure that it will either perform the intended cast, or fail at compile time.
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs')
| -rw-r--r-- | compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs index c6b2a623ea6..a298ed86276 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs @@ -14,7 +14,7 @@ use rustc_target::abi::Size; use tracing::{debug, instrument}; use crate::builder::Builder; -use crate::common::CodegenCx; +use crate::common::{AsCCharPtr, CodegenCx}; use crate::coverageinfo::map_data::FunctionCoverageCollector; use crate::llvm; @@ -236,7 +236,7 @@ fn create_pgo_func_name_var<'ll, 'tcx>( unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar( llfn, - mangled_fn_name.as_ptr().cast(), + mangled_fn_name.as_c_char_ptr(), mangled_fn_name.len(), ) } @@ -248,7 +248,7 @@ pub(crate) fn write_filenames_section_to_buffer<'a>( ) { let (pointers, lengths) = filenames .into_iter() - .map(|s: &str| (s.as_ptr().cast(), s.len())) + .map(|s: &str| (s.as_c_char_ptr(), s.len())) .unzip::<_, _, Vec<_>, Vec<_>>(); unsafe { @@ -291,7 +291,7 @@ pub(crate) fn write_mapping_to_buffer( } pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 { - unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_ptr().cast(), bytes.len()) } + unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_c_char_ptr(), bytes.len()) } } pub(crate) fn mapping_version() -> u32 { |
