Skip to content

Commit 11b0789

Browse files
committed
Gracefully handle UTF-8 errors.
1 parent ddda194 commit 11b0789

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

bindgen/ir/context.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,12 @@ If you encounter an error missing from this list, please file an issue or a PR!"
21592159
let mut kind = ModuleKind::Normal;
21602160
let mut looking_for_name = false;
21612161
for token in cursor.tokens().iter() {
2162-
match token.spelling().to_str().unwrap() {
2162+
let spelling = token.spelling();
2163+
let name = match spelling.to_str() {
2164+
Ok(name) => Cow::Borrowed(name),
2165+
Err(_) => spelling.to_string_lossy(),
2166+
};
2167+
match name.as_ref() {
21632168
"inline" => {
21642169
debug_assert!(
21652170
kind != ModuleKind::Inline,
@@ -2185,29 +2190,26 @@ If you encounter an error missing from this list, please file an issue or a PR!"
21852190
assert!(looking_for_name);
21862191
break;
21872192
}
2188-
name => {
2189-
if looking_for_name {
2190-
if module_name.is_none() {
2191-
module_name = Some(name.to_owned());
2192-
}
2193-
break;
2194-
} else {
2195-
// This is _likely_, but not certainly, a macro that's
2196-
// been placed just before the namespace keyword.
2197-
// Unfortunately, clang tokens don't let us easily see
2198-
// through the ifdef tokens, so we don't know what this
2199-
// token should really be. Instead of panicking though,
2200-
// we warn the user that we assumed the token was blank,
2201-
// and then move on.
2202-
//
2203-
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
2204-
warn!(
2205-
"Ignored unknown namespace prefix '{}' at {:?} in {:?}",
2206-
name,
2207-
token,
2208-
cursor
2209-
);
2193+
name if looking_for_name => {
2194+
if module_name.is_none() {
2195+
module_name = Some(name.to_owned());
22102196
}
2197+
break;
2198+
}
2199+
name => {
2200+
// This is _likely_, but not certainly, a macro that's
2201+
// been placed just before the namespace keyword.
2202+
// Unfortunately, clang tokens don't let us easily see
2203+
// through the ifdef tokens, so we don't know what this
2204+
// token should really be. Instead of panicking though,
2205+
// we warn the user that we assumed the token was blank,
2206+
// and then move on.
2207+
//
2208+
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
2209+
warn!(
2210+
"Ignored unknown namespace prefix '{}' at {:?} in {:?}",
2211+
name, token, cursor
2212+
);
22112213
}
22122214
}
22132215
}

bindgen/ir/var.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,17 @@ fn handle_function_macro(
169169
};
170170
let tokens: Vec<_> = cursor.tokens().iter().collect();
171171
if let Some(boundary) = tokens.iter().position(is_closing_paren) {
172-
let mut tokens = tokens
172+
let tokens: Result<Vec<_>, _> = tokens
173173
.iter()
174-
.map(|token| token.spelling().to_str().unwrap())
175-
.collect::<Vec<_>>();
174+
.map(|token| token.spelling().to_str())
175+
.collect();
176+
177+
let mut tokens = if let Ok(tokens) = tokens {
178+
tokens
179+
} else {
180+
// Skip macros containing invalid UTF-8.
181+
return;
182+
};
176183

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

0 commit comments

Comments
 (0)