repo
stringclasses 1
value | created_at
stringlengths 20
20
| instance_id
stringlengths 19
21
| test_patch
stringlengths 264
4.57M
| hints_text
stringlengths 0
30.7k
| patch
stringlengths 340
7.55M
| issue_numbers
sequencelengths 1
3
| pull_number
int64 19
1.17k
| problem_statement
stringlengths 84
10.2k
| version
stringclasses 13
values | base_commit
stringlengths 40
40
| environment_setup_commit
stringclasses 13
values |
|---|---|---|---|---|---|---|---|---|---|---|---|
rust-lang/regex
|
2024-03-04T12:38:54Z
|
rust-lang__regex-1170
|
diff --git a/regex-automata/src/nfa/thompson/compiler.rs b/regex-automata/src/nfa/thompson/compiler.rs
--- a/regex-automata/src/nfa/thompson/compiler.rs
+++ b/regex-automata/src/nfa/thompson/compiler.rs
@@ -1928,6 +1930,11 @@ mod tests {
State::Sparse(SparseTransitions { transitions })
}
+ fn s_look(look: Look, next: usize) -> State {
+ let next = sid(next);
+ State::Look { look, next }
+ }
+
fn s_bin_union(alt1: usize, alt2: usize) -> State {
State::BinaryUnion { alt1: sid(alt1), alt2: sid(alt2) }
}
diff --git a/regex-automata/src/nfa/thompson/compiler.rs b/regex-automata/src/nfa/thompson/compiler.rs
--- a/regex-automata/src/nfa/thompson/compiler.rs
+++ b/regex-automata/src/nfa/thompson/compiler.rs
@@ -1978,6 +1985,80 @@ mod tests {
);
}
+ #[test]
+ fn compile_no_unanchored_prefix_with_start_anchor() {
+ let nfa = NFA::compiler()
+ .configure(NFA::config().which_captures(WhichCaptures::None))
+ .build(r"^a")
+ .unwrap();
+ assert_eq!(
+ nfa.states(),
+ &[s_look(Look::Start, 1), s_byte(b'a', 2), s_match(0)]
+ );
+ }
+
+ #[test]
+ fn compile_yes_unanchored_prefix_with_end_anchor() {
+ let nfa = NFA::compiler()
+ .configure(NFA::config().which_captures(WhichCaptures::None))
+ .build(r"a$")
+ .unwrap();
+ assert_eq!(
+ nfa.states(),
+ &[
+ s_bin_union(2, 1),
+ s_range(0, 255, 0),
+ s_byte(b'a', 3),
+ s_look(Look::End, 4),
+ s_match(0),
+ ]
+ );
+ }
+
+ #[test]
+ fn compile_yes_reverse_unanchored_prefix_with_start_anchor() {
+ let nfa = NFA::compiler()
+ .configure(
+ NFA::config()
+ .reverse(true)
+ .which_captures(WhichCaptures::None),
+ )
+ .build(r"^a")
+ .unwrap();
+ assert_eq!(
+ nfa.states(),
+ &[
+ s_bin_union(2, 1),
+ s_range(0, 255, 0),
+ s_byte(b'a', 3),
+ // Anchors get flipped in a reverse automaton.
+ s_look(Look::End, 4),
+ s_match(0),
+ ],
+ );
+ }
+
+ #[test]
+ fn compile_no_reverse_unanchored_prefix_with_end_anchor() {
+ let nfa = NFA::compiler()
+ .configure(
+ NFA::config()
+ .reverse(true)
+ .which_captures(WhichCaptures::None),
+ )
+ .build(r"a$")
+ .unwrap();
+ assert_eq!(
+ nfa.states(),
+ &[
+ // Anchors get flipped in a reverse automaton.
+ s_look(Look::Start, 1),
+ s_byte(b'a', 2),
+ s_match(0),
+ ],
+ );
+ }
+
#[test]
fn compile_empty() {
assert_eq!(build("").states(), &[s_match(0),]);
|
diff --git a/regex-automata/src/nfa/thompson/compiler.rs b/regex-automata/src/nfa/thompson/compiler.rs
--- a/regex-automata/src/nfa/thompson/compiler.rs
+++ b/regex-automata/src/nfa/thompson/compiler.rs
@@ -961,10 +961,12 @@ impl Compiler {
// for all matches. When an unanchored prefix is not added, then the
// NFA's anchored and unanchored start states are equivalent.
let all_anchored = exprs.iter().all(|e| {
- e.borrow()
- .properties()
- .look_set_prefix()
- .contains(hir::Look::Start)
+ let props = e.borrow().properties();
+ if self.config.get_reverse() {
+ props.look_set_suffix().contains(hir::Look::End)
+ } else {
+ props.look_set_prefix().contains(hir::Look::Start)
+ }
});
let anchored = !self.config.get_unanchored_prefix() || all_anchored;
let unanchored_prefix = if anchored {
|
[
"1169"
] | 1,170
|
Valid prefix search (with ^) goes into dead state
#### What version of regex are you using?
regex-automata = "0.4.5"
#### Describe the bug at a high level.
I'm trying to do a prefix search by adding a `^` at the beginning of my pattern. I'm searching right-to-left, but the dfa is failing to find a match and entering the "dead" state.
#### What are the steps to reproduce the behavior?
```rust
use regex_automata::{hybrid::dfa::DFA, nfa::thompson, Input};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let pattern = r"^Qu";
let dfa = DFA::builder()
.thompson(thompson::Config::new().reverse(true))
.build_many(&[pattern])?;
let mut cache = dfa.create_cache();
let hay = "Quartz";
let mut state = dfa.start_state_reverse(&mut cache, &Input::new(hay))?;
for &b in hay.as_bytes().iter().rev() {
state = dfa.next_state(&mut cache, state, b)?;
if state.is_dead() {
panic!("DEAD");
}
if state.is_match() {
println!("BREAK");
break;
}
}
state = dfa.next_eoi_state(&mut cache, state)?;
assert!(state.is_match());
Ok(())
}
```
#### What is the actual behavior?
```
❯ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/reg`
thread 'main' panicked at src/main.rs:17:13:
DEAD
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
#### What is the expected behavior?
I expect the program to run through to the end, finding a match and passing the assertion, as the pattern `^Qu` should match the string "Quartz".
If I remove the `^` from the pattern, this program behaves as expected.
|
1.10
|
10fe722a3fcfdc17068b21f3262189cc52227bb5
|
10fe722a3fcfdc17068b21f3262189cc52227bb5
|
|
rust-lang/regex
|
2024-01-19T21:16:11Z
|
rust-lang__regex-1154
|
diff --git a/regex-automata/src/util/search.rs b/regex-automata/src/util/search.rs
--- a/regex-automata/src/util/search.rs
+++ b/regex-automata/src/util/search.rs
@@ -1966,4 +1971,23 @@ mod tests {
let expected_size = 3 * core::mem::size_of::<usize>();
assert_eq!(expected_size, core::mem::size_of::<MatchErrorKind>());
}
+
+ #[test]
+ fn incorrect_asref_guard() {
+ struct Bad(std::cell::Cell<bool>);
+
+ impl AsRef<[u8]> for Bad {
+ fn as_ref(&self) -> &[u8] {
+ if self.0.replace(false) {
+ &[]
+ } else {
+ &[0; 1000]
+ }
+ }
+ }
+
+ let bad = Bad(std::cell::Cell::new(true));
+ let input = Input::new(&bad);
+ assert!(input.end() <= input.haystack().len());
+ }
}
|
diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,12 +1,14 @@
1.10.3 (TBD)
============
This is a new patch release that fixes the feature configuration of optional
-dependencies.
+dependencies, and fixes an unsound use of bounds check elision.
Bug fixes:
* [BUG #1147](https://github.com/rust-lang/regex/issues/1147):
Set `default-features=false` for the `memchr` and `aho-corasick` dependencies.
+* [BUG #1154](https://github.com/rust-lang/regex/pull/1154):
+Fix unsound bounds check elision.
1.10.2 (2023-10-16)
diff --git a/regex-automata/src/util/search.rs b/regex-automata/src/util/search.rs
--- a/regex-automata/src/util/search.rs
+++ b/regex-automata/src/util/search.rs
@@ -110,9 +110,14 @@ impl<'h> Input<'h> {
/// Create a new search configuration for the given haystack.
#[inline]
pub fn new<H: ?Sized + AsRef<[u8]>>(haystack: &'h H) -> Input<'h> {
+ // Perform only one call to `haystack.as_ref()` to protect from incorrect
+ // implementations that return different values from multiple calls.
+ // This is important because there's code that relies on `span` not being
+ // out of bounds with respect to the stored `haystack`.
+ let haystack = haystack.as_ref();
Input {
- haystack: haystack.as_ref(),
- span: Span { start: 0, end: haystack.as_ref().len() },
+ haystack,
+ span: Span { start: 0, end: haystack.len() },
anchored: Anchored::No,
earliest: false,
}
|
[
"1154"
] | 1,154
|
Make `Input::new` guard against incorrect `AsRef` implementations
Currently `Input::new` calls `haystack.as_ref()` twice, once to get the actual `haystack` slice and the second time to get its length. It makes the assumption that the second call will return the same slice, but malicious implementations of `AsRef` can return different slices and thus different lengths. This is important because there's `unsafe` code relying on the `Input`'s span being inbounds with respect to the `haystack`, but if the second call to `.as_ref()` returns a bigger slice this won't be true.
For example, this snippet causes MIRI to report UB on an unchecked slice access in `find_fwd_imp` (though it will also panic sometime later when run normally, but at that point the UB already happened):
```rs
use regex_automata::{Input, meta::{Builder, Config}};
use std::cell::Cell;
struct Bad(Cell<bool>);
impl AsRef<[u8]> for Bad {
fn as_ref(&self) -> &[u8] {
if self.0.replace(false) {
&[]
} else {
&[0; 1000]
}
}
}
let bad = Bad(Cell::new(true));
let input = Input::new(&bad);
let regex = Builder::new()
.configure(Config::new().auto_prefilter(false)) // Not setting this causes some checked access to occur before the unchecked ones, avoiding the UB
.build("a+")
.unwrap();
regex.find(input);
```
The proposed fix is to just call `.as_ref()` once and use the length of the returned slice as the span's end value. A regression test has also been added.
|
1.10
|
027eebd6fde307076603530c999afcfd271bb037
|
10fe722a3fcfdc17068b21f3262189cc52227bb5
|
|
rust-lang/regex
|
2023-10-16T14:23:23Z
|
rust-lang__regex-1111
|
diff --git a/testdata/regression.toml b/testdata/regression.toml
--- a/testdata/regression.toml
+++ b/testdata/regression.toml
@@ -813,3 +813,18 @@ name = "hir-optimization-out-of-order-class"
regex = '^[[:alnum:]./-]+$'
haystack = "a-b"
matches = [[0, 3]]
+
+# This is a regression test for an improper reverse suffix optimization. This
+# occurred when I "broadened" the applicability of the optimization to include
+# multiple possible literal suffixes instead of only sticking to a non-empty
+# longest common suffix. It turns out that, at least given how the reverse
+# suffix optimization works, we need to stick to the longest common suffix for
+# now.
+#
+# See: https://github.com/rust-lang/regex/issues/1110
+# See also: https://github.com/astral-sh/ruff/pull/7980
+[[test]]
+name = 'improper-reverse-suffix-optimization'
+regex = '(\\N\{[^}]+})|([{}])'
+haystack = 'hiya \N{snowman} bye'
+matches = [[[5, 16], [5, 16], []]]
|
diff --git a/regex-automata/src/meta/strategy.rs b/regex-automata/src/meta/strategy.rs
--- a/regex-automata/src/meta/strategy.rs
+++ b/regex-automata/src/meta/strategy.rs
@@ -1167,21 +1167,34 @@ impl ReverseSuffix {
return Err(core);
}
let kind = core.info.config().get_match_kind();
- let suffixseq = crate::util::prefilter::suffixes(kind, hirs);
- let Some(suffixes) = suffixseq.literals() else {
- debug!(
- "skipping reverse suffix optimization because \
- the extract suffix sequence is not finite",
- );
- return Err(core);
+ let suffixes = crate::util::prefilter::suffixes(kind, hirs);
+ let lcs = match suffixes.longest_common_suffix() {
+ None => {
+ debug!(
+ "skipping reverse suffix optimization because \
+ a longest common suffix could not be found",
+ );
+ return Err(core);
+ }
+ Some(lcs) if lcs.is_empty() => {
+ debug!(
+ "skipping reverse suffix optimization because \
+ the longest common suffix is the empty string",
+ );
+ return Err(core);
+ }
+ Some(lcs) => lcs,
};
- let Some(pre) = Prefilter::new(kind, suffixes) else {
- debug!(
- "skipping reverse suffix optimization because \
+ let pre = match Prefilter::new(kind, &[lcs]) {
+ Some(pre) => pre,
+ None => {
+ debug!(
+ "skipping reverse suffix optimization because \
a prefilter could not be constructed from the \
longest common suffix",
- );
- return Err(core);
+ );
+ return Err(core);
+ }
};
if !pre.is_fast() {
debug!(
diff --git a/regex-automata/src/meta/strategy.rs b/regex-automata/src/meta/strategy.rs
--- a/regex-automata/src/meta/strategy.rs
+++ b/regex-automata/src/meta/strategy.rs
@@ -1268,7 +1281,7 @@ impl ReverseSuffix {
e.try_search_half_rev_limited(&input, min_start)
} else if let Some(e) = self.core.hybrid.get(&input) {
trace!(
- "using lazy DFA for reverse inner search at {:?}, \
+ "using lazy DFA for reverse suffix search at {:?}, \
but will be stopped at {} to avoid quadratic behavior",
input.get_span(),
min_start,
|
[
"1110"
] | 1,111
|
broadening of reverse suffix optimization has led to incorrect matches
Specifically, this program succeeds in `regex 1.9.x` but fails in `regex 1.10.1`:
```rust
fn main() -> anyhow::Result<()> {
let re = regex::Regex::new(r"(\\N\{[^}]+})|([{}])").unwrap();
let hay = r#"hiya \N{snowman} bye"#;
let matches = re.find_iter(hay).map(|m| m.range()).collect::<Vec<_>>();
assert_eq!(matches, vec![5..16]);
Ok(())
}
```
Its output with `1.10.1`:
```
$ cargo run -q
thread 'main' panicked at main.rs:7:5:
assertion `left == right` failed
left: [7..8, 15..16]
right: [5..16]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
I believe the issue here was my change to broaden the reverse suffix optimization to use one of many possible literals. But this turns out to be not be quite correct since the rules that govern prefixes don't apply to suffixes. In this case, the literal optimization extracts `{` and `}` as suffixes. It looks for a `{` first and finds a match at that position via the second alternate in the regex. But this winds up missing the match that came before it with the first alternate since the `{` isn't a suffix of the first alternate.
This is why we should, at least at present, only use this optimization when there is a non-empty longest common suffix. In that case, and only that case, we know that it is a suffix of every possible path through the regex.
Thank you to @charliermarsh for finding this! See: https://github.com/astral-sh/ruff/pull/7980
|
1.10
|
e7bd19dd3ebf4b1a861275f0353202bf93a39ab1
|
10fe722a3fcfdc17068b21f3262189cc52227bb5
|
|
rust-lang/regex
|
2023-10-06T15:45:29Z
|
rust-lang__regex-1098
| "diff --git a/Cargo.toml b/Cargo.toml\n--- a/Cargo.toml\n+++ b/Cargo.toml\n@@ -15,7 +15,7 @@ categor(...TRUNCATED)
| "diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml\n--- a/.github/workflows/ci.yml\n+(...TRUNCATED)
|
[
"1047",
"1043"
] | 1,098
| "internal error when translating Ast to Hir\n#### What version of regex are you using?\r\n\r\nregex-(...TRUNCATED)
|
1.9
|
17284451f10aa06c6c42e622e3529b98513901a8
|
17284451f10aa06c6c42e622e3529b98513901a8
|
|
rust-lang/regex
|
2023-08-30T23:19:37Z
|
rust-lang__regex-1080
| "diff --git a/regex-automata/src/util/pool.rs b/regex-automata/src/util/pool.rs\n--- a/regex-automat(...TRUNCATED)
| "Note: A prerequisite to understanding this comment is probably to read the comments in the code sni(...TRUNCATED)
| "diff --git a/regex-automata/src/util/pool.rs b/regex-automata/src/util/pool.rs\n--- a/regex-automat(...TRUNCATED)
|
[
"934"
] | 1,080
| "sharing one regex across many threads can lead to big slowdowns due to mutex contention\nTo reprodu(...TRUNCATED)
|
1.9
|
9a505a1804f8f89e3448a2a2c5c70573dc6362e5
|
17284451f10aa06c6c42e622e3529b98513901a8
|
rust-lang/regex
|
2023-08-26T12:53:23Z
|
rust-lang__regex-1072
| "diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml\n--- a/.github/workflows/ci.yml\n+(...TRUNCATED)
| "diff --git a/regex-automata/src/util/prefilter/aho_corasick.rs b/regex-automata/src/util/prefilter/(...TRUNCATED)
|
[
"1070"
] | 1,072
| "RegexSet and Regex give different results for the same pattern in 1.9\n#### What version of regex a(...TRUNCATED)
|
1.9
|
7536e055840f74f1f7bda8ffecf851cb3e500147
|
17284451f10aa06c6c42e622e3529b98513901a8
|
|
rust-lang/regex
|
2023-08-05T21:38:26Z
|
rust-lang__regex-1063
| "diff --git a/testdata/regression.toml b/testdata/regression.toml\n--- a/testdata/regression.toml\n+(...TRUNCATED)
| "CLI reproduction:\r\n\r\n```\r\n$ regex-cli find match meta -p '(?:(\\d+)[:.])?(\\d{1,2})[:.](\\d{2(...TRUNCATED)
| "diff --git a/regex-automata/src/meta/limited.rs b/regex-automata/src/meta/limited.rs\n--- a/regex-a(...TRUNCATED)
|
[
"1060"
] | 1,063
| "reverse inner literal optimization results in incorrect match offsets in some cases\n#### What vers(...TRUNCATED)
|
1.9
|
bbf0b38df618734b92d7b92acc8a8bf31b6d0046
|
17284451f10aa06c6c42e622e3529b98513901a8
|
rust-lang/regex
|
2023-08-04T18:09:34Z
|
rust-lang__regex-1062
| "diff --git a/regex-automata/src/nfa/thompson/compiler.rs b/regex-automata/src/nfa/thompson/compiler(...TRUNCATED)
| "diff --git a/regex-automata/src/dfa/dense.rs b/regex-automata/src/dfa/dense.rs\n--- a/regex-automat(...TRUNCATED)
|
[
"1059"
] | 1,062
| "1.9 memory usage: `globset`-generated RegexSet allocates and retains 48× more memory (600MB) vs re(...TRUNCATED)
|
1.9
|
a1910244f873e003efbe6e80ad9302c8ea949430
|
17284451f10aa06c6c42e622e3529b98513901a8
|
|
rust-lang/regex
|
2023-07-12T13:56:35Z
|
rust-lang__regex-1038
| "diff --git a/testdata/anchored.toml b/testdata/anchored.toml\n--- a/testdata/anchored.toml\n+++ b/t(...TRUNCATED)
| "diff --git a/regex-automata/src/meta/strategy.rs b/regex-automata/src/meta/strategy.rs\n--- a/regex(...TRUNCATED)
|
[
"1036",
"1036"
] | 1,038
| "Anchored search with reverse suffix strategy does not work\n#### What version of regex are you usin(...TRUNCATED)
|
1.9
|
bbb285b81fd1108536eedc52990a95f30ca6bdf5
|
17284451f10aa06c6c42e622e3529b98513901a8
|
|
rust-lang/regex
|
2023-07-10T21:51:46Z
|
rust-lang__regex-1033
| "diff --git a/regex-syntax/src/hir/literal.rs b/regex-syntax/src/hir/literal.rs\n--- a/regex-syntax/(...TRUNCATED)
|
Yeah I think this sounds right to me!
| "diff --git a/regex-syntax/src/hir/literal.rs b/regex-syntax/src/hir/literal.rs\n--- a/regex-syntax/(...TRUNCATED)
|
[
"1032"
] | 1,033
| "`hir::literal::Extractor` not producing optimal literals in some cases\n#### What version of regex (...TRUNCATED)
|
1.9
|
28e16fa5c34ab30a84b20de730cbdbe636e8a6df
|
17284451f10aa06c6c42e622e3529b98513901a8
|
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 1