commit:     1c1a37045e0e1b5719874a89520899f86f03dac0
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Feb  9 03:02:35 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Feb  9 03:14:24 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1c1a3704

Don't unconditionally shift 2 in esyslog()

Where esyslog() decides that it is appropriate to log a message, it
unconditionally tries to shift two of of the positional parameters,
regardless of how many were given. As has been seen before, this will
cause a non-interactive instance of sh(1) to immediately exit.

The other functions that call into it are not able to trigger this bug.
Still, it's possible for esyslog() to be directly invoked with too few
arguments. Fix it by testing whether a minimum of two arguments were
given. Should the test fail, print an error message and return 1.

Also, improve the method by which superfluous messages are ignored. The
prior strategy was to join the messages by the first character of IFS,
then check that the length of the resulting string is greater than 0.
Instead, check that the resulting string contains at least one visible
character.

Finally, mention that the invocation of logger(1) is not portable.

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

 functions.sh | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/functions.sh b/functions.sh
index 5b1b9bf..f756a9f 100644
--- a/functions.sh
+++ b/functions.sh
@@ -68,20 +68,23 @@ yesno()
 #
 esyslog()
 {
-       local pri=
-       local tag=
-
-       if [ -n "${EINFO_LOG}" ] && hash logger 2>/dev/null; then
-               pri="$1"
-               tag="$2"
+       local pri tag msg
 
+       if [ "$#" -lt 2 ]; then
+               printf 'Too few arguments for esyslog (got %d, expected at 
least 2)\n' "$#" >&2
+               return 1
+       elif [ -n "${EINFO_LOG}" ] && hash logger 2>/dev/null; then
+               pri=$1
+               tag=$2
                shift 2
-               [ -z "$*" ] && return 0
-
-               logger -p "${pri}" -t "${tag}" -- "$*"
+               msg=$*
+               case ${msg} in
+                       *[[:graph:]]*)
+                               # This is not strictly portable because POSIX
+                               # defines no options whatsoever for logger(1).
+                               logger -p "${pri}" -t "${tag}" -- "${msg}"
+               esac
        fi
-
-       return 0
 }
 
 #

Reply via email to