Skip to content

sed - unknown POSIX class names like [[:foo:]] or [[:word:]] are accepted instead of rejected #646

Open

Description

@sylvestre

Known: #626 (open PR) — #626

GNU sed accepts only the POSIX character class names (alnum, alpha, blank, cntrl,
digit, graph, lower, print, punct, space, upper, xdigit) and stops with
Invalid character class name and exit status 1 for anything else. uutils sed copies the name
through to the Rust regex engine, which silently accepts its own extra names ([[:word:]],
[[:ascii:]]) and treats a misspelt name such as [[:foo:]] in some other way, with exit
status 0.

Reproduction

Rust (incorrect)

$ printf 'a\n' | ./target/release/sed 's/[[:foo:]]/X/'
a
# Exit code: 0

$ printf 'a_\n' | ./target/release/sed 's/[[:word:]]/X/'
X_
# Exit code: 0

GNU (correct)

$ printf 'a\n' | /usr/bin/sed 's/[[:foo:]]/X/'
/usr/bin/sed: -e expression #1, char 14: Invalid character class name
# Exit code: 1

$ printf 'a_\n' | /usr/bin/sed 's/[[:word:]]/X/'
/usr/bin/sed: -e expression #1, char 15: Invalid character class name
# Exit code: 1

Root cause

parse_character_class in src/sed/delimited_parser.rs:283-312 reads the name between [: and
:] into inner and copies it to the output without checking it:

if c == marker {
    line.advance();
    if !line.eol() && line.current() == ']' {
        line.advance();
        result.extend_from_slice(&inner);
        result.push(marker as u8);
        result.push(b']');
        terminated = true;
        break;
    }

Test

#[rstest]
#[case::misspelt("s/[[:foo:]]/X/")]
#[case::rust_only_word("s/[[:word:]]/X/")]
#[case::rust_only_ascii("s/[[:ascii:]]/X/")]
fn test_invalid_character_class_name(#[case] script: &str) {
    new_ucmd!()
        .arg(script)
        .pipe_in("a\n")
        .fails()
        .code_is(1)
        .stderr_contains("Invalid character class name");
}

#[test]
fn test_valid_character_class_names_still_work() {
    new_ucmd!()
        .arg("s/[[:digit:][:upper:]]/X/g")
        .pipe_in("a1B\n")
        .succeeds()
        .stdout_is("aXX\n");
}

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

Activity

  1. added
    bug
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
  2. majidninja commented on Nov 16, 2026

    @majidninja

    Hi, I'd like to take this one. I've got a fix ready and will open a PR soon.

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