]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Overhaul valid_field()
authorChristian Göttsche <cgzones@googlemail.com>
Fri, 31 Mar 2023 12:46:50 +0000 (14:46 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 31 Mar 2023 14:53:40 +0000 (09:53 -0500)
e5905c4b ("Added control character check") introduced checking for
control characters but had the logic inverted, so it rejects all
characters that are not control ones.

Cast the character to `unsigned char` before passing to the character
checking functions to avoid UB.

Use strpbrk(3) for the illegal character test and return early.

lib/fields.c

index fb51b5829a31f690bc29cca0889e3abfbc0c8628..539292485dda8a9293448437bde81acfd45c2119 100644 (file)
@@ -37,26 +37,22 @@ int valid_field (const char *field, const char *illegal)
 
        /* For each character of field, search if it appears in the list
         * of illegal characters. */
+       if (illegal && NULL != strpbrk (field, illegal)) {
+               return -1;
+       }
+
+       /* Search if there are non-printable or control characters */
        for (cp = field; '\0' != *cp; cp++) {
-               if (strchr (illegal, *cp) != NULL) {
+               unsigned char c = *cp;
+               if (!isprint (c)) {
+                       err = 1;
+               }
+               if (iscntrl (c)) {
                        err = -1;
                        break;
                }
        }
 
-       if (0 == err) {
-               /* Search if there are non-printable or control characters */
-               for (cp = field; '\0' != *cp; cp++) {
-                       if (!isprint (*cp)) {
-                               err = 1;
-                       }
-                       if (!iscntrl (*cp)) {
-                               err = -1;
-                               break;
-                       }
-               }
-       }
-
        return err;
 }