Skip to content

Commit 3dad44a

Browse files
cgzonesjwcart2
authored andcommitted
libselinux: cast to unsigned char for character handling function
Character handling functions, like isspace(3), expect a value representable as unsigned char or equal to EOF. Otherwise the behavior is undefined. See https://wiki.sei.cmu.edu/confluence/display/c/STR37-C.+Arguments+to+character-handling+functions+must+be+representable+as+an+unsigned+char Signed-off-by: Christian Göttsche <[email protected]> Acked-by: James Carter <[email protected]>
1 parent 674470f commit 3dad44a

13 files changed

+37
-37
lines changed

libselinux/src/booleans.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,27 @@ char *selinux_boolean_sub(const char *name)
107107
char *ptr;
108108
char *src = line_buf;
109109
char *dst;
110-
while (*src && isspace(*src))
110+
while (*src && isspace((unsigned char)*src))
111111
src++;
112112
if (!*src)
113113
continue;
114114
if (src[0] == '#')
115115
continue;
116116

117117
ptr = src;
118-
while (*ptr && !isspace(*ptr))
118+
while (*ptr && !isspace((unsigned char)*ptr))
119119
ptr++;
120120
*ptr++ = '\0';
121121
if (strcmp(src, name) != 0)
122122
continue;
123123

124124
dst = ptr;
125-
while (*dst && isspace(*dst))
125+
while (*dst && isspace((unsigned char)*dst))
126126
dst++;
127127
if (!*dst)
128128
continue;
129129
ptr = dst;
130-
while (*ptr && !isspace(*ptr))
130+
while (*ptr && !isspace((unsigned char)*ptr))
131131
ptr++;
132132
*ptr = '\0';
133133

libselinux/src/compute_create.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
static int object_name_encode(const char *objname, char *buffer, size_t buflen)
1515
{
16-
int code;
16+
unsigned char code;
1717
size_t offset = 0;
1818

1919
if (buflen - offset < 1)

libselinux/src/get_context_list.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,27 @@ static int get_context_user(FILE * fp,
170170

171171
/* Skip leading whitespace. */
172172
start = line;
173-
while (*start && isspace(*start))
173+
while (*start && isspace((unsigned char)*start))
174174
start++;
175175
if (!(*start))
176176
continue;
177177

178178
/* Find the end of the (partial) fromcon in the line. */
179179
end = start;
180-
while (*end && !isspace(*end))
180+
while (*end && !isspace((unsigned char)*end))
181181
end++;
182182
if (!(*end))
183183
continue;
184184

185185
/* Check for a match. */
186186
linerole = start;
187-
while (*start && !isspace(*start) && *start != ':')
187+
while (*start && !isspace((unsigned char)*start) && *start != ':')
188188
start++;
189189
if (*start != ':')
190190
continue;
191191
*start = 0;
192192
linetype = ++start;
193-
while (*start && !isspace(*start) && *start != ':')
193+
while (*start && !isspace((unsigned char)*start) && *start != ':')
194194
start++;
195195
if (!(*start))
196196
continue;
@@ -210,14 +210,14 @@ static int get_context_user(FILE * fp,
210210
start = ++end;
211211
while (*start) {
212212
/* Skip leading whitespace */
213-
while (*start && isspace(*start))
213+
while (*start && isspace((unsigned char)*start))
214214
start++;
215215
if (!(*start))
216216
break;
217217

218218
/* Find the end of this partial context. */
219219
end = start;
220-
while (*end && !isspace(*end))
220+
while (*end && !isspace((unsigned char)*end))
221221
end++;
222222
if (*end)
223223
*end++ = 0;

libselinux/src/get_default_type.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int find_default_type(FILE * fp, const char *role, char **type)
4242
buf[strlen(buf) - 1] = 0;
4343

4444
ptr = buf;
45-
while (*ptr && isspace(*ptr))
45+
while (*ptr && isspace((unsigned char)*ptr))
4646
ptr++;
4747
if (!(*ptr))
4848
continue;

libselinux/src/label_file.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,20 +708,20 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
708708
char *src = buf;
709709
char *dst = NULL;
710710

711-
while (*src && isspace(*src))
711+
while (*src && isspace((unsigned char)*src))
712712
src++;
713713
if (src[0] == '#') continue;
714714
ptr = src;
715-
while (*ptr && ! isspace(*ptr))
715+
while (*ptr && ! isspace((unsigned char)*ptr))
716716
ptr++;
717717
*ptr++ = '\0';
718718
if (! *src) continue;
719719

720720
dst = ptr;
721-
while (*dst && isspace(*dst))
721+
while (*dst && isspace((unsigned char)*dst))
722722
dst++;
723723
ptr = dst;
724-
while (*ptr && ! isspace(*ptr))
724+
while (*ptr && ! isspace((unsigned char)*ptr))
725725
ptr++;
726726
*ptr = '\0';
727727
if (! *dst)

libselinux/src/label_media.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static int process_line(const char *path, char *line_buf, int pass,
3939
char *key, *context;
4040

4141
buf_p = line_buf;
42-
while (isspace(*buf_p))
42+
while (isspace((unsigned char)*buf_p))
4343
buf_p++;
4444
/* Skip comment lines and empty lines. */
4545
if (*buf_p == '#' || *buf_p == 0)

libselinux/src/label_support.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ static inline int read_spec_entry(char **entry, char **ptr, int *len, const char
2626
*entry = NULL;
2727
char *tmp_buf = NULL;
2828

29-
while (isspace(**ptr) && **ptr != '\0')
29+
while (isspace((unsigned char)**ptr) && **ptr != '\0')
3030
(*ptr)++;
3131

3232
tmp_buf = *ptr;
3333
*len = 0;
3434

35-
while (!isspace(**ptr) && **ptr != '\0') {
36-
if (!isascii(**ptr)) {
35+
while (!isspace((unsigned char)**ptr) && **ptr != '\0') {
36+
if (!isascii((unsigned char)**ptr)) {
3737
errno = EINVAL;
3838
*errbuf = "Non-ASCII characters found";
3939
return -1;
@@ -80,7 +80,7 @@ int read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
8080
len++;
8181

8282
buf_p = line_buf;
83-
while (isspace(*buf_p))
83+
while (isspace((unsigned char)*buf_p))
8484
buf_p++;
8585

8686
/* Skip comment lines and empty lines. */

libselinux/src/label_x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static int process_line(const char *path, char *line_buf, int pass,
4141
char *type, *key, *context;
4242

4343
buf_p = line_buf;
44-
while (isspace(*buf_p))
44+
while (isspace((unsigned char)*buf_p))
4545
buf_p++;
4646
/* Skip comment lines and empty lines. */
4747
if (*buf_p == '#' || *buf_p == 0)

libselinux/src/load_policy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ int selinux_init_load_policy(int *enforce)
252252
}
253253
if (fgets(buf, selinux_page_size, cfg) &&
254254
(tmp = strstr(buf, "enforcing="))) {
255-
if (tmp == buf || isspace(*(tmp - 1))) {
255+
if (tmp == buf || isspace((unsigned char)*(tmp - 1))) {
256256
secmdline =
257257
atoi(tmp + sizeof("enforcing=") - 1);
258258
}

libselinux/src/matchmediacon.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ int matchmediacon(const char *media, char ** con)
2929
current_line[strlen(current_line) - 1] = 0;
3030
/* Skip leading whitespace before the partial context. */
3131
ptr = current_line;
32-
while (*ptr && isspace(*ptr))
32+
while (*ptr && isspace((unsigned char)*ptr))
3333
ptr++;
3434

3535
if (!(*ptr))
3636
continue;
3737

3838
/* Find the end of the media context. */
3939
ptr2 = ptr;
40-
while (*ptr2 && !isspace(*ptr2))
40+
while (*ptr2 && !isspace((unsigned char)*ptr2))
4141
ptr2++;
4242
if (!(*ptr2))
4343
continue;
@@ -53,7 +53,7 @@ int matchmediacon(const char *media, char ** con)
5353
return -1;
5454

5555
/* Skip whitespace. */
56-
while (*ptr2 && isspace(*ptr2))
56+
while (*ptr2 && isspace((unsigned char)*ptr2))
5757
ptr2++;
5858
if (!(*ptr2)) {
5959
return -1;

0 commit comments

Comments
 (0)