From c7efbf590e9c9e622b722d4309e00a39655df5f5 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Mon, 17 Mar 2025 19:52:09 +0100 Subject: [PATCH] function: Also error for read-only var in positional arg We have this hack where any positional arguments are taken as argument names if "--argument-names" is given, and that didn't check for read-only variables. Fixes #11295 (cherry picked from commit d203ee4d531b10cb6204d7b327b0ecd88fa30d1b) --- src/builtins/function.rs | 8 ++++++++ tests/checks/function.fish | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/builtins/function.rs b/src/builtins/function.rs index 4bf4a81b2..28b64a4f8 100644 --- a/src/builtins/function.rs +++ b/src/builtins/function.rs @@ -92,6 +92,14 @@ fn parse_cmd_opts( // A positional argument we got because we use RETURN_IN_ORDER. let woptarg = w.woptarg.unwrap().to_owned(); if handling_named_arguments { + if is_read_only(&woptarg) { + streams.err.append(wgettext_fmt!( + "%ls: variable '%ls' is read-only\n", + cmd, + woptarg + )); + return STATUS_INVALID_ARGS; + } opts.named_arguments.push(woptarg); } else { streams.err.append(wgettext_fmt!( diff --git a/tests/checks/function.fish b/tests/checks/function.fish index bf71723be..058a036d3 100644 --- a/tests/checks/function.fish +++ b/tests/checks/function.fish @@ -183,6 +183,11 @@ function foo --argument-names status; end echo status $status # CHECK: status 2 +function foo --argument-names foo status; end +# CHECKERR: {{.*}}function.fish (line {{\d+}}): function: variable 'status' is read-only +# CHECKERR: function foo --argument-names foo status; end +# CHECKERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ + functions -q foo echo exists $status # CHECK: exists 1