about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPhilipp Krones <hello@philkrones.com>2020-07-13 15:59:45 +0200
committerGitHub <noreply@github.com>2020-07-13 15:59:45 +0200
commit26ede3115fbaaab5aecf6ca32ab97cd0feb9f38b (patch)
treeb3e95076013e9d5c5bb44c997d9ed510e6c82925
parent32ef448bdb915f5ccb37daf7c32ad50b85c8ef0d (diff)
parent1b3bc16533a3e701616648920603c10674eb653b (diff)
downloadrust-26ede3115fbaaab5aecf6ca32ab97cd0feb9f38b.tar.gz
rust-26ede3115fbaaab5aecf6ca32ab97cd0feb9f38b.zip
Rollup merge of #5784 - matthiaskrgr:ice_5780, r=phansch
Fix out of bounds access by checking length equality BEFORE accessing by index.

Fixes #5780

changelog: fix out of bounds access in unnested_or_patterns lint.

Edit: I did not bother reducing a testcase from `librustc_typeck` crate but I can confirm that with the change the crash no longer occurs.
-rw-r--r--clippy_lints/src/unnested_or_patterns.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/clippy_lints/src/unnested_or_patterns.rs b/clippy_lints/src/unnested_or_patterns.rs
index 169a486d1eb..502bf0c4279 100644
--- a/clippy_lints/src/unnested_or_patterns.rs
+++ b/clippy_lints/src/unnested_or_patterns.rs
@@ -400,8 +400,8 @@ fn extend_with_matching(
 
 /// Are the patterns in `ps1` and `ps2` equal save for `ps1[idx]` compared to `ps2[idx]`?
 fn eq_pre_post(ps1: &[P<Pat>], ps2: &[P<Pat>], idx: usize) -> bool {
-    ps1[idx].is_rest() == ps2[idx].is_rest() // Avoid `[x, ..] | [x, 0]` => `[x, .. | 0]`.
-        && ps1.len() == ps2.len()
+    ps1.len() == ps2.len()
+        && ps1[idx].is_rest() == ps2[idx].is_rest() // Avoid `[x, ..] | [x, 0]` => `[x, .. | 0]`.
         && over(&ps1[..idx], &ps2[..idx], |l, r| eq_pat(l, r))
         && over(&ps1[idx + 1..], &ps2[idx + 1..], |l, r| eq_pat(l, r))
 }