gitbutlerapp/gitbutler · error

invalid range: {start}-{end}

Error message

invalid range: {start}-{end}

What it means

Thrown by `Hunk::new(start, end, hash)` when `start > end`, i.e. the range is inverted. `Hunk::from_str` funnels parsed values through this constructor, so both direct construction and string parsing of an inverted range like "58-12" produce it. The message echoes the offending pair as `{start}-{end}`.

Source

Thrown at crates/but-meta/src/virtual_branches_legacy_types.rs:422

                if raw_hash.is_empty() {
                    None
                } else {
                    let mut buf = [0u8; 16];
                    hex::decode_to_slice(raw_hash, &mut buf)?;
                    Some(md5::Digest(buf))
                }
            } else {
                None
            };

            Hunk::new(start, end, hash)
        }
    }

    impl Hunk {
        pub fn new(start: u32, end: u32, hash: Option<HunkHash>) -> anyhow::Result<Self> {
            if start > end {
                Err(anyhow!("invalid range: {start}-{end}"))
            } else {
                Ok(Hunk {
                    hash,
                    start,
                    end,
                    hunk_header: None,
                })
            }
        }
    }

    #[derive(Debug, PartialEq, Clone)]
    pub struct HunkHeader {
        pub old_start: u32,
        pub old_lines: u32,
        pub new_start: u32,
        pub new_lines: u32,
    }

View on GitHub (pinned to caf1f223d3)

Solutions

  1. Fix the data or computation so start <= end
  2. If the inversion is a known artifact, normalize by swapping bounds before calling `Hunk::new` (only if semantically valid for your data)
  3. Add a debug assertion or test in the producer of hunk ranges so inversions are caught where they are created, not at parse time

Example fix

// before
let hunk = Hunk::new(58, 12, None)?; // Err: invalid range: 58-12

// after
let hunk = Hunk::new(12, 58, None)?;
Defensive patterns

Strategy: validation

Validate before calling

let (start, end) = if start <= end { (start, end) } else { anyhow::bail!("hunk bounds inverted: {start} > {end}") };
let hunk = Hunk::new(start, end, hash)?;

Type guard

fn is_valid_range(start: u32, end: u32) -> bool {
    start <= end
}

Prevention

When it happens

Trigger: `Hunk::new(58, 12, None)`, or `Hunk::from_str("58-12")` — any input where the first number exceeds the second; also arithmetic that computes an end before swapping or diff-hunk merging bugs producing inverted ranges.

Common situations: Corrupted legacy toml with reversed hunk bounds; code that computes (new_start, old_end) from diff edits and gets the ordering wrong; fixture data generated by scripts that don't enforce ordering.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@caf1f223d3 (2026-08-20). Data as JSON: /api/errors/47f39e8c8dc021e6. Report an issue: GitHub.