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
1 changed files with 11 additions and 15 deletions

View File

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