Skip to content

Commit 7b59b94

Browse files
WavyEbuilderstephensmalley
authored andcommitted
libselinux: refactor selinux_getenforcemode
Invert the check for cfg being a nullptr and early return. Signed-off-by: Rahul Sandhu <[email protected]> Acked-by: Stephen Smalley <[email protected]>
1 parent 0c28219 commit 7b59b94

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

libselinux/src/selinux_config.c

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,47 +88,46 @@ static const uint16_t file_path_suffixes_idx[NEL] = {
8888

8989
int selinux_getenforcemode(int *enforce)
9090
{
91-
int ret = -1;
9291
FILE *cfg = fopen(SELINUXCONFIG, "re");
93-
if (cfg) {
94-
char *buf;
95-
char *tag;
96-
int len = sizeof(SELINUXTAG) - 1;
97-
buf = malloc(selinux_page_size);
98-
if (!buf) {
99-
fclose(cfg);
100-
return -1;
101-
}
102-
while (fgets_unlocked(buf, selinux_page_size, cfg)) {
103-
if (strncmp(buf, SELINUXTAG, len))
104-
continue;
105-
tag = buf+len;
106-
while (isspace((unsigned char)*tag))
107-
tag++;
108-
if (!strncasecmp
109-
(tag, "enforcing", sizeof("enforcing") - 1)) {
110-
*enforce = 1;
111-
ret = 0;
112-
break;
113-
} else
114-
if (!strncasecmp
115-
(tag, "permissive",
116-
sizeof("permissive") - 1)) {
117-
*enforce = 0;
118-
ret = 0;
119-
break;
120-
} else
121-
if (!strncasecmp
122-
(tag, "disabled",
123-
sizeof("disabled") - 1)) {
124-
*enforce = -1;
125-
ret = 0;
126-
break;
127-
}
128-
}
92+
if (!cfg)
93+
return -1;
94+
95+
char *buf = malloc(selinux_page_size);
96+
if (!buf) {
12997
fclose(cfg);
130-
free(buf);
98+
return -1;
13199
}
100+
101+
int ret = -1;
102+
const int len = sizeof(SELINUXTAG) - 1;
103+
while (fgets_unlocked(buf, selinux_page_size, cfg)) {
104+
if (strncmp(buf, SELINUXTAG, len))
105+
continue;
106+
107+
char *tag = buf + len;
108+
while (isspace((unsigned char)*tag))
109+
tag++;
110+
111+
if (!strncasecmp(tag, "enforcing", sizeof("enforcing") - 1)) {
112+
*enforce = 1;
113+
ret = 0;
114+
break;
115+
} else if (!strncasecmp(tag, "permissive",
116+
sizeof("permissive") - 1)) {
117+
*enforce = 0;
118+
ret = 0;
119+
break;
120+
} else if (!strncasecmp(tag, "disabled",
121+
sizeof("disabled") - 1)) {
122+
*enforce = -1;
123+
ret = 0;
124+
break;
125+
}
126+
}
127+
128+
fclose(cfg);
129+
free(buf);
130+
132131
return ret;
133132
}
134133

0 commit comments

Comments
 (0)