Overhaul valid_field()

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.
This commit is contained in:
Christian Göttsche 2023-03-31 14:46:50 +02:00 committed by Serge Hallyn
parent a5f9ef8b7f
commit 2eaea70111

View File

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