Skip to content

tr: Raise error when STRING2 argument has unreachable character during complement #6345

@BenWiederhake

Description

@BenWiederhake

When complementing, we basically translate all bytes not mentioned in STRING1 to bytes in STRING2. This means that only the first 256-len(ARRAYISH1) bytes of ARRAY2 can be accessed.

GNU tr issues a (confusing, but well-intentioned) error message if the command-line argument contains garbage:

$ x230="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$ echo -n 'hello' | ../gnu/src/tr -c '[:upper:]' "${x230}" # 256-26 times 'x'
xxxxx
$ echo -n 'hello' | ../gnu/src/tr -c '[:upper:]' "${x230}x" # 256-26+1 times 'x'
../gnu/src/tr: when translating with complemented character classes,
string2 must map all characters in the domain to one
[$? = 1]
$ echo -n 'hello' | cargo run tr -c '[:upper:]' "${x230}" # we accidentally treat this correctly
xxxxx
$ echo -n 'hello' | cargo run tr -c '[:upper:]' "${x230}x" # this needs to change
xxxxx

Computing the ideal size should be easy, but checking it isn't:

  • If we just consider the length of STRING2, then this will obviously fail due to expansions
  • I believe there was some reason why checking the length of ARRAY2 didn't work. Hmm. I'm confused.

(Original issue: #6133)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions