Skip to content

Commit

Permalink
Gracefully handle UTF-8 errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 27, 2024
1 parent 0600260 commit 8178f51
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
49 changes: 26 additions & 23 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,12 @@ If you encounter an error missing from this list, please file an issue or a PR!"
let mut kind = ModuleKind::Normal;
let mut looking_for_name = false;
for token in cursor.tokens().iter() {
match token.spelling().to_str().unwrap() {
let spelling = token.spelling();
let name = match spelling.to_str() {
Ok(name) => Cow::Borrowed(name),
Err(_) => spelling.to_string_lossy(),
};
match name.as_ref() {
"inline" => {
debug_assert!(
kind != ModuleKind::Inline,
Expand All @@ -2185,29 +2190,27 @@ If you encounter an error missing from this list, please file an issue or a PR!"
assert!(looking_for_name);
break;
}
name => {
if looking_for_name {
if module_name.is_none() {
module_name = Some(name.to_owned());
}
break;
} else {
// This is _likely_, but not certainly, a macro that's
// been placed just before the namespace keyword.
// Unfortunately, clang tokens don't let us easily see
// through the ifdef tokens, so we don't know what this
// token should really be. Instead of panicking though,
// we warn the user that we assumed the token was blank,
// and then move on.
//
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
warn!(
"Ignored unknown namespace prefix '{}' at {:?} in {:?}",
name,
token,
cursor
);
name if looking_for_name => {
if module_name.is_none() {
module_name =
Some(name.into_owned());
}
break;
}
name => {
// This is _likely_, but not certainly, a macro that's
// been placed just before the namespace keyword.
// Unfortunately, clang tokens don't let us easily see
// through the ifdef tokens, so we don't know what this
// token should really be. Instead of panicking though,
// we warn the user that we assumed the token was blank,
// and then move on.
//
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
warn!(
"Ignored unknown namespace prefix '{}' at {:?} in {:?}",
name, token, cursor
);
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions bindgen/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,17 @@ fn handle_function_macro(
};
let tokens: Vec<_> = cursor.tokens().iter().collect();
if let Some(boundary) = tokens.iter().position(is_closing_paren) {
let mut tokens = tokens
let tokens: Result<Vec<_>, _> = tokens
.iter()
.map(|token| token.spelling().to_str().unwrap())
.collect::<Vec<_>>();
.map(|token| token.spelling().to_str())
.collect();

let mut tokens = if let Ok(tokens) = tokens {
tokens
} else {
// Skip macros containing invalid UTF-8.
return;
};

let name = tokens.remove(0);
let args: Vec<_> = tokens
Expand Down

0 comments on commit 8178f51

Please sign in to comment.