diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5496896f4..e7f33da03 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,6 @@ fish next-minor Notable improvements and fixes ------------------------------ - Syntax changes and new commands ------------------------------- @@ -184,6 +183,8 @@ Scripting improvements - ``fish -c`` now reads the remaining arguments into $argv (:issue:`2314`). - The ``pwd`` command supports the long options ``--logical`` and ``--physical``, matching other implementations (:issue:`6787`). - ``fish --profile`` now only starts the profile after fish's startup (including config.fish) is done. For profiling startup there is a new ``--profile-startup`` option that profiles only startup (:issue:`7648`). +- ``set --query``'s $status will now saturate at 255 instead of wrapping around when checking more than 255 variables at once (:issue:`7698`). + Interactive improvements ------------------------ diff --git a/doc_src/cmds/set.rst b/doc_src/cmds/set.rst index 8fab9f770..c7fa3e88e 100644 --- a/doc_src/cmds/set.rst +++ b/doc_src/cmds/set.rst @@ -49,7 +49,7 @@ The following options are available: - ``-e`` or ``--erase`` causes the specified shell variables to be erased -- ``-q`` or ``--query`` test if the specified variable names are defined. Does not output anything, but the builtins exit status is the number of variables specified that were not defined. +- ``-q`` or ``--query`` test if the specified variable names are defined. Does not output anything, but the builtins exit status is the number of variables specified that were not defined, saturating at 255 if more than 255 variables are not defined. - ``-n`` or ``--names`` List only the names of all defined variables, not their value. The names are guaranteed to be sorted. diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index 7c76dafe6..229b3e9b9 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -565,6 +565,13 @@ static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, free(dest); } + // The return value is cast to an 8-bit unsigned integer, + // so saturate the error count to 255. + // Otherwise 256 errors is reported as 0 errors. + if (retval > 255) { + retval = 255; + } + return retval; } diff --git a/tests/checks/set.fish b/tests/checks/set.fish index c53388ff9..e586aff53 100644 --- a/tests/checks/set.fish +++ b/tests/checks/set.fish @@ -682,4 +682,11 @@ echo $foo echo $bar #CHECK: 1 3 + +# Test that `set -q` does not return 0 if there are 256 missing variables + +set -lq a(seq 1 256) +echo $status +#CHECK: 255 + true