From dad8c527e0518b1fc9ff3a4da4d1b6020c45ac35 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Fri, 13 Jan 2023 17:56:28 +0100 Subject: [PATCH] read: Error on read-only variables Fixes #9346 --- src/builtins/read.cpp | 5 +++++ tests/checks/read.fish | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/builtins/read.cpp b/src/builtins/read.cpp index 0e7574b41..72b176af8 100644 --- a/src/builtins/read.cpp +++ b/src/builtins/read.cpp @@ -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); 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; diff --git a/tests/checks/read.fish b/tests/checks/read.fish index 709ee977b..1172a6e70 100644 --- a/tests/checks/read.fish +++ b/tests/checks/read.fish @@ -380,3 +380,12 @@ begin end | read -l --line foo bar echo $foo $bar # 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