abbr: Fix crash when no name has been given

This crashed for

```fish
abbr --add --regex '{\d+..\d+}' --function foo
```

i.e. a regex and a function but no name - that's 0 additional
arguments.
This commit is contained in:
Fabian Boehm 2023-01-15 10:50:09 +01:00
parent f6f10353be
commit 077118d983
2 changed files with 8 additions and 2 deletions

View File

@ -202,11 +202,12 @@ static int abbr_add(const abbr_options_t &opts, io_streams_t &streams) {
streams.err.append_format(_(L"%ls %ls: Requires at least two arguments\n"), CMD, subcmd);
return STATUS_INVALID_ARGS;
}
const wcstring &name = opts.args[0];
if (name.empty()) {
if (opts.args.empty() || opts.args[0].empty()) {
streams.err.append_format(_(L"%ls %ls: Name cannot be empty\n"), CMD, subcmd);
return STATUS_INVALID_ARGS;
}
const wcstring &name = opts.args[0];
if (std::any_of(name.begin(), name.end(), iswspace)) {
streams.err.append_format(
_(L"%ls %ls: Abbreviation '%ls' cannot have spaces in the word\n"), CMD, subcmd,

View File

@ -194,3 +194,8 @@ abbr --add bogus --position never stuff
abbr --add bogus --position anywhere --position command stuff
# CHECKERR: abbr: Cannot specify multiple positions
abbr --add --regex foo --function foo
# CHECKERR: abbr --add: Name cannot be empty
echo foo
# CHECK: foo