Skip to content

Commit 6086694

Browse files
agausmannkulppvdrz
authored
Sanitize RegexSet input so alternation is properly handled (#1756)
* tests: Avoid using globs as regexes * Sanitize regex set input to properly handle alternation * Add test case for alternates/anchors interaction * emit warning if wildcard pattern is used * update changelog and bump versions Co-authored-by: Darren Kulp <[email protected]> Co-authored-by: Christian Poveda <[email protected]>
1 parent 170b79c commit 6086694

File tree

10 files changed

+65
-11
lines changed

10 files changed

+65
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151

152152
## Changed
153153

154+
* Regex inputs are sanitized so alternation (`a|b`) is handled correctly but
155+
wildcard patterns (`*`) are now considered invalid.
156+
154157
## Removed
155158

156159
## Fixed

Cargo.lock

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

bindgen-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ readme = "../README.md"
1111
repository = "https://github.com/rust-lang/rust-bindgen"
1212
documentation = "https://docs.rs/bindgen"
1313
homepage = "https://rust-lang.github.io/rust-bindgen/"
14-
version = "0.61.0"
14+
version = "0.62.0"
1515
edition = "2018"
1616
# If you change this, also update README.md and msrv in .github/workflows/bindgen.yml
1717
rust-version = "1.57.0"

bindgen-tests/tests/expectations/tests/whitelist-alternates.rs

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

bindgen-tests/tests/headers/empty-union.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --opaque-type "*"
1+
// bindgen-flags: --opaque-type ".*"
22

33
template <int> class a {
44
union {};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --opaque-type "*"
1+
// bindgen-flags: --opaque-type ".*"
22

33
union a;
4-
struct b;
4+
struct b;

bindgen-tests/tests/headers/ord-enum.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --rustified-enum * --with-derive-ord
1+
// bindgen-flags: --rustified-enum ".*" --with-derive-ord
22

33
enum A {
44
A0 = 0,
@@ -12,4 +12,4 @@ enum B {
1212
B1 = B0 + 3,
1313
B2 = B0 + 2,
1414
B3 = B0 - 2,
15-
};
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// bindgen-flags: --whitelist-type 'Whitelisted.*|Allow.*'
2+
// Test for changes introduced in #1756
3+
4+
struct WhitelistedType {};
5+
struct AllowType {};
6+
// this would have been accepted because the start anchor
7+
// wouldn't be applied to the second alternative:
8+
struct NoAllowType {};

bindgen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "../README.md"
1414
repository = "https://github.com/rust-lang/rust-bindgen"
1515
documentation = "https://docs.rs/bindgen"
1616
homepage = "https://rust-lang.github.io/rust-bindgen/"
17-
version = "0.61.0"
17+
version = "0.62.0"
1818
edition = "2018"
1919
build = "build.rs"
2020
# If you change this, also update README.md and msrv in .github/workflows/bindgen.yml

bindgen/regex_set.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ impl RegexSet {
2626
where
2727
S: AsRef<str>,
2828
{
29-
self.items.push(string.as_ref().to_owned());
29+
let string = string.as_ref().to_owned();
30+
if string == "*" {
31+
warn!("using wildcard patterns (`*`) is no longer considered valid. Use `.*` instead");
32+
}
33+
self.items.push(string);
3034
self.matched.push(Cell::new(false));
3135
self.set = None;
3236
}
@@ -53,7 +57,7 @@ impl RegexSet {
5357
/// Must be called before calling `matches()`, or it will always return
5458
/// false.
5559
pub fn build(&mut self, record_matches: bool) {
56-
let items = self.items.iter().map(|item| format!("^{}$", item));
60+
let items = self.items.iter().map(|item| format!("^({})$", item));
5761
self.record_matches = record_matches;
5862
self.set = match RxSet::new(items) {
5963
Ok(x) => Some(x),

0 commit comments

Comments
 (0)