commit: e650022a1291ab98efd2af02a4c83c395e2d5fa1 Author: Kerin Millar <kfm <AT> plushkava <DOT> net> AuthorDate: Tue Feb 14 00:29:41 2023 +0000 Commit: Sam James <sam <AT> gentoo <DOT> org> CommitDate: Tue Feb 14 03:39:49 2023 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=e650022a
Validate the exit status given to veend() and vewend() as an argument Presently, both functions trust that the first argument is either empty or a valid exit status code. If given a bogus value, the call to return may cause sh(1) to unceremoniously exit, as demonstrated below. $ dash -c 'return junk; echo done' dash: 1: return: Illegal number: junk Instead, validate that the argument is an integer whose value is greater than or equal to 0. Should validation fail, a diagnostic mesage will be printed to STDERR and the return value will be 0, provided that the call to print succeeded. Signed-off-by: Kerin Millar <kfm <AT> plushkava.net> Signed-off-by: Sam James <sam <AT> gentoo.org> functions.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/functions.sh b/functions.sh index a5d6502..28764d8 100644 --- a/functions.sh +++ b/functions.sh @@ -346,8 +346,10 @@ veend() { if yesno "${EINFO_VERBOSE}"; then eend "$@" + elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then + printf 'Invalid argument given to veend (the exit status code must be an integer >= 0)\n' >&2 else - return "${1:-0}" + return "$1" fi } @@ -355,8 +357,10 @@ vewend() { if yesno "${EINFO_VERBOSE}"; then ewend "$@" + elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then + printf 'Invalid argument given to vewend (the exit status code must be an integer >= 0)\n' >&2 else - return "${1:-0}" + return "$1" fi }