commit:     d874f5ca008b2838925194347f76139acdcc43ba
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Mon Feb 13 23:00:00 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Feb 14 00:08:13 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=d874f5ca

Have is_older_than() check the argument count and guard against errexit

If fewer than 2 arguments are provided, print a diagnostic message to
STDERR before immediately returning. Doing so allows for an
unconditional shift.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 functions.sh | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/functions.sh b/functions.sh
index 51c6dbd..8a2a788 100644
--- a/functions.sh
+++ b/functions.sh
@@ -416,12 +416,15 @@ is_older_than()
 {
        local ref has_gfind
 
-       if [ -e "$1" ]; then
+       if [ "$#" -lt 2 ]; then
+               printf 'Too few arguments for is_older_than (got %d, expected 
at least 2)\n' "$#" >&2
+               return 1
+       elif [ -e "$1" ]; then
                ref=$1
        else
                ref=
        fi
-       [ "$#" -gt 0 ] && shift
+       shift
 
        # Consult the hash table in the present shell, prior to forking.
        hash gfind 2>/dev/null; has_gfind=$(( $? == 0 ))

Reply via email to