Skip to content

expand, unexpand: read a hyphen-leading tab list as a tab list - #14291

Open
arbelonson-source wants to merge 1 commit into
uutils:mainfrom
arbelonson-source:fix/expand-unexpand-tab-list-hyphen
Open

arbelonson-source wants to merge 1 commit into
uutils:mainfrom
arbelonson-source:fix/expand-unexpand-tab-list-hyphen

Conversation

@arbelonson-source

Copy link
Copy Markdown
Contributor

-1 is the obsolete spelling of --tabs=1, and expand_shortcuts rewrote it wherever it appeared — including where it was already the value of -t.

Command GNU uutils before
expand -t -1 f tab size contains invalid character(s): '-1' error: a value is required for '--tabs <N, LIST>' but none was supplied
expand -t -0 f tab size contains invalid character(s): '-0' same clap error
unexpand -t -1 f tab size contains invalid character(s): '-1' same clap error

The check that produces GNU's message is already here and already correct — expand -t abc and expand -t 1,-2 match GNU byte for byte today. The hyphen-leading value simply never reached it: clap took -1 for an option, and when allowed through, expand_shortcuts rewrote it to --tabs=1 so the message named the rewritten token rather than what was typed.

Found by differential testing against GNU coreutils 9.11.

Approach

The obsolete -N form is now rewritten only where an option is expected — not as the value of -t/--tabs, and not after a -- terminator, which GNU treats as a file name. allow_hyphen_values lets clap pass the value through to the existing check.

Same shape as the --/option-value handling in #14248 and #14255.

Testing

  • 26-case differential check against GNU across both utilities: all match, including the forms that had to keep working — the obsolete -4 and -1,3, the attached -t8, --tabs=4, clusters (-it 4, -at 4), and -- -1
  • One regression test per utility, covering -t -1 and -t -0 plus the obsolete spelling still working
  • cargo test --features "expand,unexpand" --no-default-features: 93 passed, 0 failed (91 pre-existing, 2 new)
  • cargo fmt --check and cargo clippy -p uu_expand -p uu_unexpand --all-targets: clean

Disclosure

Prepared with AI assistance (Claude Opus 5, via Claude Code), per the AI policy in CONTRIBUTING.md. The GNU behaviour above was established by running the installed GNU binaries as a black box; I did not read GNU coreutils source. All testing was run locally.

`-1` is the obsolete spelling of `--tabs=1`, and `expand_shortcuts`
rewrote it wherever it appeared, including where it was already the
value of `-t`:

    $ expand -t -1 file
    error: a value is required for '--tabs <N, LIST>' but none was supplied

GNU reads it as a (bad) tab list and reports it as one:
`tab size contains invalid character(s): '-1'`. The check that produces
that message already exists here -- `expand -t abc` and `expand -t 1,-2`
match GNU today -- the value just never reached it.

Rewrite the obsolete form only where an option is expected: not as the
value of -t/--tabs, and not past a `--` terminator, which GNU treats as
a file name. `allow_hyphen_values` lets clap pass the value through.
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/tail-n0f is now passing!


/// Preprocess command line arguments and expand shortcuts. For example, "-7" is expanded to
/// "--tabs=7" and "-1,3" to "--tabs=1 --tabs=3".
/// Whether `arg` is a `-t`/`--tabs` spelling that takes the *next* argument as

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this landed between expand_shortcuts and its doc comment, so rustdoc now attaches that doc to takes_tabs_value. could you please move it above?

/// Preprocess command line arguments and expand shortcuts. For example, "-7" is expanded to
/// "--tabs=7 --first-only" and "-1,3" to "--tabs=1 --tabs=3 --first-only". However, if "-a" or
/// "--all" is provided, "--first-only" is omitted.
/// Whether `arg` is a `-t`/`--tabs` spelling that takes the *next* argument as

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, the doc comment of expand_shortcuts is now glued to takes_tabs_value

/// "--all" is provided, "--first-only" is omitted.
/// Whether `arg` is a `-t`/`--tabs` spelling that takes the *next* argument as
/// its value, so that argument is a tab list rather than an obsolete `-N`.
fn takes_tabs_value(arg: &str) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same function as in expand, could it be shared, maybe in uucore?

// `-1` is the obsolete spelling of `--tabs=1`, but only where an
// option is expected. As the value of `-t` it is a (bad) tab list that
// has to reach the tab-list check, and past `--` it is a file name.
let is_operand = end_of_options || expecting_tabs;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the -t value isn't valid UTF-8, expecting_tabs never gets reset and the next -4 isn't rewritten, no?

expecting_tabs = takes_tabs_value(text);
}
}
if !is_operand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already called to_str() just above, could the two blocks be merged?

// A tab list may start with a hyphen. It is not valid, but the
// check below reports it far better than clap does, so it has to
// reach that check rather than be taken for an option.
.allow_hyphen_values(true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make this comment shorter :)

}

// The obsolete spelling still works where an option is expected.
new_ucmd!().arg("-4").pipe_in("\tx\n").succeeds();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check stdout here, and add a test for -- -1 too

.stderr_contains(format!("tab size contains invalid character(s): '{arg}'"));
}

new_ucmd!().arg("-4").pipe_in(" x\n").succeeds();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check stdout here too
did you run the upstream tests/expand and tests/unexpand? bash util/build-gnu.sh && bash util/run-gnu-test.sh tests/misc/expand.pl

@sylvestre

Copy link
Copy Markdown
Contributor

it has been conflicting for more than 7 days, sorry! please rebase it

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants