blob: ac08be515bf109b8be1b5fddd1c04799908e57d4 [file] [log] [blame]
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001From 2eaea70111f65b16d55998386e4ceb4273c19eb4 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= <cgzones@googlemail.com>
3Date: Fri, 31 Mar 2023 14:46:50 +0200
4Subject: [PATCH] Overhaul valid_field()
5
6e5905c4b ("Added control character check") introduced checking for
7control characters but had the logic inverted, so it rejects all
8characters that are not control ones.
9
10Cast the character to `unsigned char` before passing to the character
11checking functions to avoid UB.
12
13Use strpbrk(3) for the illegal character test and return early.
14
15Upstream-Status: Backport [https://github.com/shadow-maint/shadow/commit/2eaea70111f65b16d55998386e4ceb4273c19eb4]
16
17Signed-off-by: Xiangyu Chen <xiangyu.chen@windriver.com>
18---
19 lib/fields.c | 24 ++++++++++--------------
20 1 file changed, 10 insertions(+), 14 deletions(-)
21
22diff --git a/lib/fields.c b/lib/fields.c
23index fb51b582..53929248 100644
24--- a/lib/fields.c
25+++ b/lib/fields.c
26@@ -37,26 +37,22 @@ int valid_field (const char *field, const char *illegal)
27
28 /* For each character of field, search if it appears in the list
29 * of illegal characters. */
30+ if (illegal && NULL != strpbrk (field, illegal)) {
31+ return -1;
32+ }
33+
34+ /* Search if there are non-printable or control characters */
35 for (cp = field; '\0' != *cp; cp++) {
36- if (strchr (illegal, *cp) != NULL) {
37+ unsigned char c = *cp;
38+ if (!isprint (c)) {
39+ err = 1;
40+ }
41+ if (iscntrl (c)) {
42 err = -1;
43 break;
44 }
45 }
46
47- if (0 == err) {
48- /* Search if there are non-printable or control characters */
49- for (cp = field; '\0' != *cp; cp++) {
50- if (!isprint (*cp)) {
51- err = 1;
52- }
53- if (!iscntrl (*cp)) {
54- err = -1;
55- break;
56- }
57- }
58- }
59-
60 return err;
61 }
62
63--
642.34.1
65