Skip to content

Sanitize RegexSet input so alternation is properly handled #1756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@

## Changed

* Regex inputs are sanitized so alternation (`a|b`) is handled correctly but
wildcard patterns (`*`) are now considered invalid.

## Removed

## Fixed
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindgen-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "../README.md"
repository = "https://github.com/rust-lang/rust-bindgen"
documentation = "https://docs.rs/bindgen"
homepage = "https://rust-lang.github.io/rust-bindgen/"
version = "0.61.0"
version = "0.62.0"
edition = "2018"
# If you change this, also update README.md and msrv in .github/workflows/bindgen.yml
rust-version = "1.57.0"
Expand Down
39 changes: 39 additions & 0 deletions bindgen-tests/tests/expectations/tests/whitelist-alternates.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/empty-union.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --opaque-type "*"
// bindgen-flags: --opaque-type ".*"

template <int> class a {
union {};
Expand Down
4 changes: 2 additions & 2 deletions bindgen-tests/tests/headers/forward_declared_opaque.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --opaque-type "*"
// bindgen-flags: --opaque-type ".*"

union a;
struct b;
struct b;
4 changes: 2 additions & 2 deletions bindgen-tests/tests/headers/ord-enum.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --rustified-enum * --with-derive-ord
// bindgen-flags: --rustified-enum ".*" --with-derive-ord

enum A {
A0 = 0,
Expand All @@ -12,4 +12,4 @@ enum B {
B1 = B0 + 3,
B2 = B0 + 2,
B3 = B0 - 2,
};
};
8 changes: 8 additions & 0 deletions bindgen-tests/tests/headers/whitelist-alternates.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// bindgen-flags: --whitelist-type 'Whitelisted.*|Allow.*'
// Test for changes introduced in #1756

struct WhitelistedType {};
struct AllowType {};
// this would have been accepted because the start anchor
// wouldn't be applied to the second alternative:
struct NoAllowType {};
2 changes: 1 addition & 1 deletion bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "../README.md"
repository = "https://github.com/rust-lang/rust-bindgen"
documentation = "https://docs.rs/bindgen"
homepage = "https://rust-lang.github.io/rust-bindgen/"
version = "0.61.0"
version = "0.62.0"
edition = "2018"
build = "build.rs"
# If you change this, also update README.md and msrv in .github/workflows/bindgen.yml
Expand Down
8 changes: 6 additions & 2 deletions bindgen/regex_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ impl RegexSet {
where
S: AsRef<str>,
{
self.items.push(string.as_ref().to_owned());
let string = string.as_ref().to_owned();
if string == "*" {
warn!("using wildcard patterns (`*`) is no longer considered valid. Use `.*` instead");
}
self.items.push(string);
self.matched.push(Cell::new(false));
self.set = None;
}
Expand All @@ -53,7 +57,7 @@ impl RegexSet {
/// Must be called before calling `matches()`, or it will always return
/// false.
pub fn build(&mut self, record_matches: bool) {
let items = self.items.iter().map(|item| format!("^{}$", item));
let items = self.items.iter().map(|item| format!("^({})$", item));
self.record_matches = record_matches;
self.set = match RxSet::new(items) {
Ok(x) => Some(x),
Expand Down