Skip to content

Commit 25b321a

Browse files
committed
fix: name mangling check for C++
1 parent e1a1eed commit 25b321a

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cexpr = "0.6"
3030
clang-sys = "1"
3131
clap = "4"
3232
clap_complete = "4"
33+
cpp_demangle = "0.4.4"
3334
env_logger = "0.10.0"
3435
itertools = { version = ">=0.10,<0.14", default-features = false }
3536
libloading = "0.8"

bindgen/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cexpr.workspace = true
3232
clang-sys = { workspace = true, features = ["clang_11_0"] }
3333
clap = { workspace = true, features = ["derive"], optional = true }
3434
clap_complete = { workspace = true, optional = true }
35+
cpp_demangle.workspace = true
3536
itertools = { workspace = true }
3637
log = { workspace = true, optional = true }
3738
prettyplease = { workspace = true, optional = true, features = ["verbatim"] }

bindgen/codegen/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -5858,6 +5858,32 @@ pub(crate) mod utils {
58585858
return true;
58595859
}
58605860

5861+
// Check if the mangled name is a valid C++ symbol
5862+
if let Ok(demangled) = cpp_demangle::Symbol::new(mangled_name) {
5863+
let demangled_name = demangled.to_string().replace("::", "_");
5864+
5865+
// Check that the demangled name is longer than the canonical name
5866+
if demangled_name.len() <= canonical_name.len() {
5867+
return false;
5868+
}
5869+
5870+
// Check that the demangled name starts with the canonical name
5871+
if !demangled_name.starts_with(canonical_name) {
5872+
return false;
5873+
}
5874+
5875+
// Check that the suffix is a signature
5876+
if let Some(suffix) = demangled_name.get(canonical_name.len()..) {
5877+
if !suffix.starts_with('(') || !suffix.ends_with(')') {
5878+
return false;
5879+
}
5880+
} else {
5881+
return false;
5882+
}
5883+
5884+
return true;
5885+
}
5886+
58615887
// Working with &[u8] makes indexing simpler than with &str
58625888
let canonical_name = canonical_name.as_bytes();
58635889
let mangled_name = mangled_name.as_bytes();

0 commit comments

Comments
 (0)