read: Error on read-only variables

Fixes #9346
This commit is contained in:
Fabian Boehm 2023-01-13 17:56:28 +01:00
parent 1b1cf73b60
commit dad8c527e0
2 changed files with 14 additions and 0 deletions

View File

@ -424,6 +424,11 @@ static int validate_read_args(const wchar_t *cmd, read_cmd_opts_t &opts, int arg
builtin_print_error_trailer(parser, streams.err, cmd); builtin_print_error_trailer(parser, streams.err, cmd);
return STATUS_INVALID_ARGS; return STATUS_INVALID_ARGS;
} }
if (env_var_t::flags_for(argv[i]) & env_var_t::flag_read_only) {
streams.err.append_format(_(L"%ls: %ls: cannot overwrite read-only variable"), cmd, argv[i]);
builtin_print_error_trailer(parser, streams.err, cmd);
return STATUS_INVALID_ARGS;
}
} }
return STATUS_CMD_OK; return STATUS_CMD_OK;

View File

@ -380,3 +380,12 @@ begin
end | read -l --line foo bar end | read -l --line foo bar
echo $foo $bar echo $foo $bar
# CHECK: 1 2 # CHECK: 1 2
echo foo | read status
# CHECKERR: read: status: cannot overwrite read-only variable
# CHECKERR: {{.*}}read.fish (line {{\d+}}):
# CHECKERR: echo foo | read status
# CHECKERR: ^
# CHECKERR: (Type 'help read' for related documentation)
echo read $status
# CHECK: read 2