From 5809493be2f6e47e3bbfc8a645ea0d82d5aedd82 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 09:44:37 +0000 Subject: vercmp: three real bugs, found by comparing against pacman's own binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tests/versions.rs started as a hand-typed table of ALPM ordering vectors and turned into something better: a differential test that runs the hand-written comparator against pacman's `vercmp` over a 34-fragment cross-product (1156 pairs) of shapes that appear in real recipes. It found three bugs, all of which would have shipped. 1. **Numeric vs alphabetic chunks were inverted.** The code had 'a numeric chunk is always older than an alphabetic one'. It is the opposite: 1.0.1 > 1.0.a. The unit test asserted the wrong direction with the comment 'alpha sorts after numeric', so the suite was protecting the bug. 2. **Separators were ignored entirely.** They are not merely skipped. ALPM distinguishes an alphabetic chunk glued to the previous chunk from one after a separator, and the separated one is newer regardless of the letters: 1.0 > 1.0a, but 1.0 < 1.0.a, and 1.0z < 1.0.a. Two versions differing only in where the dot sits were being called equal. 3. **The end-of-string rule looked at the wrong thing.** When one side runs out, the decision comes from the other side's *next raw character* before any separator skipping: a letter there makes the exhausted string newer (the 1.0rc1 < 1.0 rule), anything else makes it older. The rewrite is character-wise rather than a chunk pre-pass, because the glued-or-separated distinction cannot be made after the separators have been thrown away. Rule 3 is the kind of thing nobody can be confident about from the spec alone, which is exactly what the oracle is for; it is skipped with a note when pacman is not installed, and the curated table — every pair of which was also checked against the binary — always runs. Also: the recipe parser's epoch is now exercised end to end (an epoch added to a PKGBUILD must land on the candidate, and 1:0.9-1 must beat 1.0-1 rather than read as a downgrade), and the downgrade path asserts the index keeps serving the higher version, since a downgrade is a human decision and not an automatic rebuild. 161 tests pass, clippy clean. --- src/pkgver/mod.rs | 162 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 116 insertions(+), 46 deletions(-) (limited to 'src/pkgver') diff --git a/src/pkgver/mod.rs b/src/pkgver/mod.rs index 75be308..e968256 100644 --- a/src/pkgver/mod.rs +++ b/src/pkgver/mod.rs @@ -87,66 +87,136 @@ pub fn vercmp(a: &str, b: &str) -> Ordering { va.cmp_full(&vb) } -#[derive(Debug, PartialEq, Eq)] +fn is_alnum(c: u8) -> bool { + c.is_ascii_alphanumeric() +} + +/// The chunk starting at `k`, and the index just past it. enum Chunk<'a> { Num(u128), Alpha(&'a str), + None, } -/// Split into maximal digit / alpha runs, ignoring everything else. -fn chunks(s: &str) -> Vec> { - let b = s.as_bytes(); - let mut out = Vec::new(); - let mut i = 0; - while i < b.len() { - let c = b[i] as char; - if c.is_ascii_digit() { - let start = i; - while i < b.len() && (b[i] as char).is_ascii_digit() { - i += 1; - } - // Leading zeros are not significant. - let digits = s[start..i].trim_start_matches('0'); - out.push(Chunk::Num(digits.parse::().unwrap_or(0))); - } else if c.is_ascii_alphabetic() { - let start = i; - while i < b.len() && (b[i] as char).is_ascii_alphabetic() { - i += 1; - } - out.push(Chunk::Alpha(&s[start..i])); - } else { +fn read_chunk(s: &[u8], k: usize) -> (Chunk<'_>, usize) { + if k >= s.len() { + return (Chunk::None, k); + } + if s[k].is_ascii_digit() { + let mut i = k; + while i < s.len() && s[i].is_ascii_digit() { + i += 1; + } + // Leading zeros are not significant. + let digits = std::str::from_utf8(&s[k..i]).unwrap_or("0").trim_start_matches('0'); + (Chunk::Num(digits.parse::().unwrap_or(0)), i) + } else if s[k].is_ascii_alphabetic() { + let mut i = k; + while i < s.len() && s[i].is_ascii_alphabetic() { i += 1; } + (Chunk::Alpha(std::str::from_utf8(&s[k..i]).unwrap_or("")), i) + } else { + (Chunk::None, k) } - out } -/// ALPM's `vercmp_string`: chunk-wise comparison where digits precede letters. +/// Was the character before `k` a separator? A chunk at the very start of a +/// version counts as attached. +fn preceded_by_separator(s: &[u8], k: usize) -> bool { + k > 0 && !is_alnum(s[k - 1]) +} + +/// ALPM's `vercmp`, character-wise. +/// +/// Three rules do all the work, and each one is surprising the first time: +/// +/// 1. A numeric chunk is newer than an alphabetic chunk at the same position, so +/// `1.0.1` > `1.0.a`. +/// 2. When one string runs out, the other side decides by its *next raw +/// character*: a letter there makes the exhausted string newer (`1.0` > +/// `1.0a`, the `1.0rc1 < 1.0` rule), anything else makes it older +/// (`1.0` < `1.0.1`). +/// 3. Separators are not merely skipped. An alphabetic chunk that follows a +/// separator is newer than one glued to the previous chunk, whatever the +/// letters are: `1.0.a` > `1.0z`. Two versions that differ only in where the +/// dot sits are *not* equal, which is the part a chunk-splitting +/// implementation gets wrong. +/// +/// The cross-product test in `tests/versions.rs` checks this against pacman's own +/// `vercmp` over ~1100 pairs, which is the only reason anyone can be confident +/// about rule 3. pub fn vercmp_string(a: &str, b: &str) -> Ordering { - let ca = chunks(a); - let cb = chunks(b); - let mut i = 0; + let (a, b) = (a.as_bytes(), b.as_bytes()); + let (mut i, mut j) = (0usize, 0usize); loop { - match (ca.get(i), cb.get(i)) { - (None, None) => return Ordering::Equal, - (None, Some(Chunk::Num(_))) => return Ordering::Less, - (None, Some(Chunk::Alpha(_))) => return Ordering::Greater, - (Some(Chunk::Num(_)), None) => return Ordering::Greater, - (Some(Chunk::Alpha(_)), None) => return Ordering::Less, - (Some(x), Some(y)) => { - let o = match (x, y) { - (Chunk::Num(x), Chunk::Num(y)) => x.cmp(y), - (Chunk::Alpha(x), Chunk::Alpha(y)) => x.cmp(y), - // A numeric chunk is always older than an alphabetic one. - (Chunk::Num(_), Chunk::Alpha(_)) => Ordering::Less, - (Chunk::Alpha(_), Chunk::Num(_)) => Ordering::Greater, - }; - if o != Ordering::Equal { - return o; + // End-of-string checks come before skipping separators, because whether + // the next character is glued or separated is exactly what decides them. + match (i >= a.len(), j >= b.len()) { + (true, true) => return Ordering::Equal, + (true, false) => { + return if j < b.len() && b[j].is_ascii_alphabetic() { + Ordering::Greater + } else { + Ordering::Less + } + } + (false, true) => { + return if i < a.len() && a[i].is_ascii_alphabetic() { + Ordering::Less + } else { + Ordering::Greater + } + } + (false, false) => {} + } + while i < a.len() && !is_alnum(a[i]) { + i += 1; + } + while j < b.len() && !is_alnum(b[j]) { + j += 1; + } + if i >= a.len() && j >= b.len() { + return Ordering::Equal; + } + if i >= a.len() || j >= b.len() { + let alpha = if i >= a.len() { + j < b.len() && b[j].is_ascii_alphabetic() + } else { + i < a.len() && a[i].is_ascii_alphabetic() + }; + let a_first = i >= a.len(); + return match (a_first, alpha) { + (true, true) => Ordering::Greater, + (true, false) => Ordering::Less, + (false, true) => Ordering::Less, + (false, false) => Ordering::Greater, + }; + } + let (ca, na) = read_chunk(a, i); + let (cb, nb) = read_chunk(b, j); + match (ca, cb) { + (Chunk::Num(x), Chunk::Num(y)) => { + if x != y { + return x.cmp(&y); + } + } + (Chunk::Alpha(x), Chunk::Alpha(y)) => { + let (xa, yb) = (preceded_by_separator(a, i), preceded_by_separator(b, j)); + if xa != yb { + return xa.cmp(&yb); + } + if x != y { + return x.cmp(y); } - i += 1; } + // A numeric chunk is always newer than an alphabetic one. + (Chunk::Num(_), Chunk::Alpha(_)) => return Ordering::Greater, + (Chunk::Alpha(_), Chunk::Num(_)) => return Ordering::Less, + _ => {} } + i = na; + j = nb; } } @@ -287,7 +357,7 @@ mod tests { assert_eq!(ord("1.0-1", "1.0-2"), Ordering::Less); assert_eq!(ord("1.1-1", "1.0-1"), Ordering::Greater); assert_eq!(ord("1.10-1", "1.9-1"), Ordering::Greater, "numeric chunks compare numerically"); - assert_eq!(ord("1.0.a-1", "1.0.1-1"), Ordering::Greater, "alpha sorts after numeric"); + assert_eq!(ord("1.0.a-1", "1.0.1-1"), Ordering::Less, "a numeric chunk is newer than an alphabetic one"); assert_eq!(ord("1.5-1", "1.5.0-1"), Ordering::Less, "a trailing numeric chunk is newer"); assert_eq!(ord("1:1.0-1", "2.0-1"), Ordering::Greater, "epoch dominates"); assert_eq!(ord("1.0-1", "1.0_2-1"), Ordering::Less, "non-alnum separators are ignored"); -- cgit v1.2.3