From: Rahul Sandhu <nvraxn@gmail.com>
To: cgzones@googlemail.com
Cc: nvraxn@gmail.com, selinux@vger.kernel.org,
stephen.smalley.work@gmail.com
Subject: [PATCH v2] libselinux: refactor selinux_getenforcemode
Date: Wed, 30 Jul 2025 08:47:29 +0100 [thread overview]
Message-ID: <20250730074729.936489-1-nvraxn@gmail.com> (raw)
In-Reply-To: <82e217c7-7124-4d49-ab98-074a6c2535c9@googlemail.com>
Invert the check for cfg being a nullptr and early return.
Signed-off-by: Rahul Sandhu <nvraxn@gmail.com>
---
libselinux/src/selinux_config.c | 75 ++++++++++++++++-----------------
1 file changed, 37 insertions(+), 38 deletions(-)
v2: Don't mark len static.
diff --git a/libselinux/src/selinux_config.c b/libselinux/src/selinux_config.c
index 75db14ba..e1bc1b79 100644
--- a/libselinux/src/selinux_config.c
+++ b/libselinux/src/selinux_config.c
@@ -88,47 +88,46 @@ static const uint16_t file_path_suffixes_idx[NEL] = {
int selinux_getenforcemode(int *enforce)
{
- int ret = -1;
FILE *cfg = fopen(SELINUXCONFIG, "re");
- if (cfg) {
- char *buf;
- char *tag;
- int len = sizeof(SELINUXTAG) - 1;
- buf = malloc(selinux_page_size);
- if (!buf) {
- fclose(cfg);
- return -1;
- }
- while (fgets_unlocked(buf, selinux_page_size, cfg)) {
- if (strncmp(buf, SELINUXTAG, len))
- continue;
- tag = buf+len;
- while (isspace((unsigned char)*tag))
- tag++;
- if (!strncasecmp
- (tag, "enforcing", sizeof("enforcing") - 1)) {
- *enforce = 1;
- ret = 0;
- break;
- } else
- if (!strncasecmp
- (tag, "permissive",
- sizeof("permissive") - 1)) {
- *enforce = 0;
- ret = 0;
- break;
- } else
- if (!strncasecmp
- (tag, "disabled",
- sizeof("disabled") - 1)) {
- *enforce = -1;
- ret = 0;
- break;
- }
- }
+ if (!cfg)
+ return -1;
+
+ char *buf = malloc(selinux_page_size);
+ if (!buf) {
fclose(cfg);
- free(buf);
+ return -1;
}
+
+ int ret = -1;
+ const int len = sizeof(SELINUXTAG) - 1;
+ while (fgets_unlocked(buf, selinux_page_size, cfg)) {
+ if (strncmp(buf, SELINUXTAG, len))
+ continue;
+
+ char *tag = buf + len;
+ while (isspace((unsigned char)*tag))
+ tag++;
+
+ if (!strncasecmp(tag, "enforcing", sizeof("enforcing") - 1)) {
+ *enforce = 1;
+ ret = 0;
+ break;
+ } else if (!strncasecmp(tag, "permissive",
+ sizeof("permissive") - 1)) {
+ *enforce = 0;
+ ret = 0;
+ break;
+ } else if (!strncasecmp(tag, "disabled",
+ sizeof("disabled") - 1)) {
+ *enforce = -1;
+ ret = 0;
+ break;
+ }
+ }
+
+ fclose(cfg);
+ free(buf);
+
return ret;
}
--
2.50.1
next prev parent reply other threads:[~2025-07-30 7:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-27 15:32 [PATCH] libselinux: refactor selinux_getenforcemode Rahul Sandhu
2025-07-29 14:33 ` Stephen Smalley
2025-07-29 17:07 ` Christian Göttsche
2025-07-30 7:47 ` Rahul Sandhu [this message]
2025-07-30 12:59 ` [PATCH v2] " Stephen Smalley
2025-07-31 14:17 ` Stephen Smalley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250730074729.936489-1-nvraxn@gmail.com \
--to=nvraxn@gmail.com \
--cc=cgzones@googlemail.com \
--cc=selinux@vger.kernel.org \
--cc=stephen.smalley.work@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).