Skip to content

sed - addr,~N range stays open when its end line never reaches the command #655

Closed

Description

@sylvestre

Known: #617 (open PR) — #617

An addr,~N range should end at the first line after addr whose number is a multiple of N.
uutils sed only closes the range when the command is actually evaluated on a multiple of N. If
that line is skipped (by an earlier d, a branch, n/N, or an enclosing block), the range
stays open until the next multiple of N, or the end of input.

Reproduction

Rust (incorrect)

$ seq 12 | ./target/release/sed -n '3d;2,~3p'
2
4
5
6
# Exit code: 0

GNU (correct)

$ seq 12 | /usr/bin/sed -n '3d;2,~3p'
2
4
# Exit code: 0

The range 2,~3 should end on line 3. Line 3 is deleted before it reaches p. GNU closes the
range on the next line the command sees (line 4, which is still printed); uutils sed keeps it
open until line 6.

Root cause

The StepEnd arm of applies() in src/sed/processor.rs:110-116:

Address::StepEnd(step) => {
    // Inclusive end on multiple of step
    if linenum.is_multiple_of(*step) {
        command.start_line = None;
    }
    Ok(true)
}

It ignores start (the line where the range began) and only tests the current line number.

Test

#[rstest]
#[case::end_line_deleted("3d;2,~3p", "2\n4\n")]
#[case::end_lines_deleted("3,4d;2,~3p", "2\n5\n")]
#[case::start_on_multiple("3,~3p", "3\n4\n5\n6\n")]
fn test_step_end_range_with_skipped_end(#[case] script: &str, #[case] expected: &str) {
    new_ucmd!()
        .args(&["-n", script])
        .pipe_in("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n")
        .succeeds()
        .stdout_is(expected);
}

Reported by Anthropic's OSS Scanner (ANT-2026-AEKMYYZ6).

Activity

  1. added
    bug
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions