changeset 52268:b019b5798e8f stable

rhg: fix a bug where only the first pattern in narrowspec was validated Apparently we "return" instead of doing "continue", which seems clearly unintentional. I split the function in two to make this particular bug impossible.
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Wed, 29 Jan 2025 14:48:50 +0000
parents 77b95a4abbb2
children 8317993a49f1
files rust/hg-core/src/narrow.rs
diffstat 1 files changed, 17 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/narrow.rs	Tue Jan 28 17:29:59 2025 +0000
+++ b/rust/hg-core/src/narrow.rs	Wed Jan 29 14:48:50 2025 +0000
@@ -107,24 +107,28 @@
     w(s.first()) || w(s.last())
 }
 
+fn validate_pattern(pattern: &[u8]) -> Result<(), SparseConfigError> {
+    if starts_or_ends_with_whitespace(pattern) {
+        return Err(SparseConfigError::WhitespaceAtEdgeOfPattern(
+            pattern.to_owned(),
+        ));
+    }
+    for prefix in VALID_PREFIXES.iter() {
+        if pattern.starts_with(prefix.as_bytes()) {
+            return Ok(());
+        }
+    }
+    Err(SparseConfigError::InvalidNarrowPrefix(pattern.to_owned()))
+}
+
 fn validate_patterns(patterns: &[u8]) -> Result<(), SparseConfigError> {
     for pattern in patterns.split(|c| *c == b'\n') {
         if pattern.is_empty() {
+            // TODO: probably not intentionally allowed (only because `split`
+            // produces "fake" empty line at the end)
             continue;
         }
-        if starts_or_ends_with_whitespace(pattern) {
-            return Err(SparseConfigError::WhitespaceAtEdgeOfPattern(
-                pattern.to_owned(),
-            ));
-        }
-        for prefix in VALID_PREFIXES.iter() {
-            if pattern.starts_with(prefix.as_bytes()) {
-                return Ok(());
-            }
-        }
-        return Err(SparseConfigError::InvalidNarrowPrefix(
-            pattern.to_owned(),
-        ));
+        validate_pattern(pattern)?
     }
     Ok(())
 }