Saturate return value in builtin_set_query

builtin_set_query returns the number of missing variables. Because the
return value passed to the shell is an 8-bit unsigned integer, if the
number of missing variables is a multiple of 256, it would overflow to 0.

This commit saturates the return value at 255 if there are more than 255
missing variables.
This commit is contained in:
Ethel Morgan 2021-02-08 15:19:21 +00:00 committed by Fabian Homborg
parent 42c75111c8
commit 6dd6a57c60
4 changed files with 17 additions and 2 deletions

View File

@ -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
------------------------

View File

@ -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.

View File

@ -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;
}

View File

@ -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