summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Gausmann <adam@gaussian.dev>2022-10-24 10:39:52 -0500
committerGitHub <noreply@github.com>2022-10-24 10:39:52 -0500
commit6086694d40f8df09db79bc9a7b7eec29131f424a (patch)
treee403d255053bb1e61a34f19dda1f878f888511cf
parent170b79c88ea5d527184eccc2903ae4ac52272800 (diff)
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 <darren@kulp.ch> Co-authored-by: Christian Poveda <christian.poveda@ferrous-systems.com>
-rw-r--r--CHANGELOG.md3
-rw-r--r--Cargo.lock4
-rw-r--r--bindgen-cli/Cargo.toml2
-rw-r--r--bindgen-tests/tests/expectations/tests/whitelist-alternates.rs39
-rw-r--r--bindgen-tests/tests/headers/empty-union.hpp2
-rw-r--r--bindgen-tests/tests/headers/forward_declared_opaque.h4
-rw-r--r--bindgen-tests/tests/headers/ord-enum.h4
-rw-r--r--bindgen-tests/tests/headers/whitelist-alternates.h8
-rw-r--r--bindgen/Cargo.toml2
-rw-r--r--bindgen/regex_set.rs8
10 files changed, 65 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d9c5aca4..87a74acd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/Cargo.lock b/Cargo.lock
index 765f391f..d656f9dd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -48,7 +48,7 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bindgen"
-version = "0.61.0"
+version = "0.62.0"
dependencies = [
"bitflags",
"cexpr",
@@ -68,7 +68,7 @@ dependencies = [
[[package]]
name = "bindgen-cli"
-version = "0.61.0"
+version = "0.62.0"
dependencies = [
"bindgen",
"clap 3.2.12",
diff --git a/bindgen-cli/Cargo.toml b/bindgen-cli/Cargo.toml
index b4feb6d9..7bff8f46 100644
--- a/bindgen-cli/Cargo.toml
+++ b/bindgen-cli/Cargo.toml
@@ -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"
diff --git a/bindgen-tests/tests/expectations/tests/whitelist-alternates.rs b/bindgen-tests/tests/expectations/tests/whitelist-alternates.rs
new file mode 100644
index 00000000..77d963d8
--- /dev/null
+++ b/bindgen-tests/tests/expectations/tests/whitelist-alternates.rs
@@ -0,0 +1,39 @@
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
+
+#[repr(C)]
+#[derive(Debug, Default, Copy, Clone)]
+pub struct WhitelistedType {}
+#[test]
+fn bindgen_test_layout_WhitelistedType() {
+ assert_eq!(
+ ::std::mem::size_of::<WhitelistedType>(),
+ 0usize,
+ concat!("Size of: ", stringify!(WhitelistedType))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<WhitelistedType>(),
+ 1usize,
+ concat!("Alignment of ", stringify!(WhitelistedType))
+ );
+}
+#[repr(C)]
+#[derive(Debug, Default, Copy, Clone)]
+pub struct AllowType {}
+#[test]
+fn bindgen_test_layout_AllowType() {
+ assert_eq!(
+ ::std::mem::size_of::<AllowType>(),
+ 0usize,
+ concat!("Size of: ", stringify!(AllowType))
+ );
+ assert_eq!(
+ ::std::mem::align_of::<AllowType>(),
+ 1usize,
+ concat!("Alignment of ", stringify!(AllowType))
+ );
+}
diff --git a/bindgen-tests/tests/headers/empty-union.hpp b/bindgen-tests/tests/headers/empty-union.hpp
index 3b067e39..113a95ee 100644
--- a/bindgen-tests/tests/headers/empty-union.hpp
+++ b/bindgen-tests/tests/headers/empty-union.hpp
@@ -1,4 +1,4 @@
-// bindgen-flags: --opaque-type "*"
+// bindgen-flags: --opaque-type ".*"
template <int> class a {
union {};
diff --git a/bindgen-tests/tests/headers/forward_declared_opaque.h b/bindgen-tests/tests/headers/forward_declared_opaque.h
index 1b58edb9..69a12cc7 100644
--- a/bindgen-tests/tests/headers/forward_declared_opaque.h
+++ b/bindgen-tests/tests/headers/forward_declared_opaque.h
@@ -1,4 +1,4 @@
-// bindgen-flags: --opaque-type "*"
+// bindgen-flags: --opaque-type ".*"
union a;
-struct b; \ No newline at end of file
+struct b;
diff --git a/bindgen-tests/tests/headers/ord-enum.h b/bindgen-tests/tests/headers/ord-enum.h
index 364f711e..b14e77c2 100644
--- a/bindgen-tests/tests/headers/ord-enum.h
+++ b/bindgen-tests/tests/headers/ord-enum.h
@@ -1,4 +1,4 @@
-// bindgen-flags: --rustified-enum * --with-derive-ord
+// bindgen-flags: --rustified-enum ".*" --with-derive-ord
enum A {
A0 = 0,
@@ -12,4 +12,4 @@ enum B {
B1 = B0 + 3,
B2 = B0 + 2,
B3 = B0 - 2,
-}; \ No newline at end of file
+};
diff --git a/bindgen-tests/tests/headers/whitelist-alternates.h b/bindgen-tests/tests/headers/whitelist-alternates.h
new file mode 100644
index 00000000..7aa3bf87
--- /dev/null
+++ b/bindgen-tests/tests/headers/whitelist-alternates.h
@@ -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 {};
diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml
index 5a8e96be..13e5f445 100644
--- a/bindgen/Cargo.toml
+++ b/bindgen/Cargo.toml
@@ -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
diff --git a/bindgen/regex_set.rs b/bindgen/regex_set.rs
index 9262c4ee..9f1e2251 100644
--- a/bindgen/regex_set.rs
+++ b/bindgen/regex_set.rs
@@ -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;
}
@@ -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),