Skip to contentSkip to conversation

libselinux: refactor selinux_check_securetty_context

PATCH Superseded Rahul Sandhu 2 participants 6 messages Jul 31, 2025 – Oct 7, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: refactor selinux_check_securetty_context

Invert the checks for fp and con, and initalise char *end as NULL.

---
 .../src/selinux_check_securetty_context.c     | 80 +++++++++++--------
 1 file changed, 46 insertions(+), 34 deletions(-)
libselinux/src/selinux_check_securetty_context.c +46 -34
index 7609752e..f04b66e0 100644
@@ -1,3 +1,4 @@
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
@@ -8,45 +9,56 @@
 
 int selinux_check_securetty_context(const char * tty_context)
 {
+	context_t con = context_new(tty_context);
+	if (!con) {
+		return -1;
+	}
+
+	FILE *fp = fopen(selinux_securetty_types_path(), "re");
+	if (!fp) {
+		context_free(con);
+		return -1;
+	}
+
+	const char *type = context_type_get(con);
+
 	char *line = NULL;
-	char *start, *end = NULL;
+	char *start = NULL;
+	char *end = NULL;
 	size_t line_len = 0;
-	ssize_t len;
 	int found = -1;
-	FILE *fp;
-	fp = fopen(selinux_securetty_types_path(), "re");
-	if (fp) {
-		context_t con = context_new(tty_context);
-		if (con) {
-			const char *type = context_type_get(con);
-			while ((len = getline(&line, &line_len, fp)) != -1) {
-
-				if (line[len - 1] == '\n')
-					line[len - 1] = 0;
-
-				/* Skip leading whitespace. */
-				start = line;
-				while (*start && isspace((unsigned char)*start))
-					start++;
-				if (!(*start))
-					continue;
-
-				end = start;
-				while (*end && !isspace((unsigned char)*end))
-					end++;
-				if (*end)
-					*end++ = 0;
-				if (!strcmp(type, start)) {
-					found = 0;
-					break;
-				}
-			}
-			free(line);
-			context_free(con);
+	ssize_t len;
+	while ((len = getline(&line, &line_len, fp)) != -1) {
+
+		if (line[len - 1] == '\n') {
+			line[len - 1] = 0;
+		}
+
+		/* Skip leading whitespace. */
+		start = line;
+		while (*start && isspace((unsigned char)*start)) {
+			start++;
+		}
+		if (!(*start)) {
+			continue;
+		}
+
+		end = start;
+		while (*end && !isspace((unsigned char)*end)) {
+			end++;
+		}
+		if (*end) {
+			*end++ = 0;
+		}
+		if (!strcmp(type, start)) {
+			found = 0;
+			break;
 		}
-		fclose(fp);
 	}
 
+	free(line);
+	context_free(con);
+	fclose(fp);
+
 	return found;
 }
-
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/1 libselinux: refactor selinux_check_securetty_context

Invert the checks for fp and con, and initalise char *end as NULL.

---
 .../src/selinux_check_securetty_context.c     | 79 +++++++++++--------
 1 file changed, 45 insertions(+), 34 deletions(-)

v2: oops, didn't mean to include fcntl.h

libselinux/src/selinux_check_securetty_context.c +45 -34
index 7609752e..6d039f8f 100644
@@ -8,45 +8,56 @@
 
 int selinux_check_securetty_context(const char * tty_context)
 {
+	context_t con = context_new(tty_context);
+	if (!con) {
+		return -1;
+	}
+
+	FILE *fp = fopen(selinux_securetty_types_path(), "re");
+	if (!fp) {
+		context_free(con);
+		return -1;
+	}
+
+	const char *type = context_type_get(con);
+
 	char *line = NULL;
-	char *start, *end = NULL;
+	char *start = NULL;
+	char *end = NULL;
 	size_t line_len = 0;
-	ssize_t len;
 	int found = -1;
-	FILE *fp;
-	fp = fopen(selinux_securetty_types_path(), "re");
-	if (fp) {
-		context_t con = context_new(tty_context);
-		if (con) {
-			const char *type = context_type_get(con);
-			while ((len = getline(&line, &line_len, fp)) != -1) {
-
-				if (line[len - 1] == '\n')
-					line[len - 1] = 0;
-
-				/* Skip leading whitespace. */
-				start = line;
-				while (*start && isspace((unsigned char)*start))
-					start++;
-				if (!(*start))
-					continue;
-
-				end = start;
-				while (*end && !isspace((unsigned char)*end))
-					end++;
-				if (*end)
-					*end++ = 0;
-				if (!strcmp(type, start)) {
-					found = 0;
-					break;
-				}
-			}
-			free(line);
-			context_free(con);
+	ssize_t len;
+	while ((len = getline(&line, &line_len, fp)) != -1) {
+
+		if (line[len - 1] == '\n') {
+			line[len - 1] = 0;
+		}
+
+		/* Skip leading whitespace. */
+		start = line;
+		while (*start && isspace((unsigned char)*start)) {
+			start++;
+		}
+		if (!(*start)) {
+			continue;
+		}
+
+		end = start;
+		while (*end && !isspace((unsigned char)*end)) {
+			end++;
+		}
+		if (*end) {
+			*end++ = 0;
+		}
+		if (!strcmp(type, start)) {
+			found = 0;
+			break;
 		}
-		fclose(fp);
 	}
 
+	free(line);
+	context_free(con);
+	fclose(fp);
+
 	return found;
 }
-
Signature · 1 line
2.50.1
maintainer
#2 lore raw
In reply to Rahul Sandhu
Quoted 2 lines · Rahul Sandhu

Invert the checks for fp and con, and initalise char *end as NULL.

spelling (initalise) and *end was already initialized, also see below.

Quoted 16 lines
---
 .../src/selinux_check_securetty_context.c     | 80 +++++++++++--------
 1 file changed, 46 insertions(+), 34 deletions(-)
libselinux/src/selinux_check_securetty_context.c +1 -0
index 7609752e..f04b66e0 100644
@@ -1,3 +1,4 @@
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>

libselinux/src/selinux_check_securetty_context.c line 12

Quoted 4 lines
libselinux/src/selinux_check_securetty_context.c +1 -0
@@ -8,45 +9,56 @@
 
 int selinux_check_securetty_context(const char * tty_context)
 {
+       context_t con = context_new(tty_context);

This means we allocate con before we even know if we will ever reach code that will need it.

libselinux/src/selinux_check_securetty_context.c line 13

Quoted 1 line
libselinux/src/selinux_check_securetty_context.c +1 -0
+       if (!con) {

No need for { } for single-statement bodies.

Quoted 4 lines
libselinux/src/selinux_check_securetty_context.c +4 -0
+               return -1;
+       }
+
+       FILE *fp = fopen(selinux_securetty_types_path(), "re");

libselinux/src/selinux_check_securetty_context.c line 26

Quoted 10 lines
libselinux/src/selinux_check_securetty_context.c +8 -1
+       if (!fp) {
+               context_free(con);
+               return -1;
+       }
+
+       const char *type = context_type_get(con);
+
        char *line = NULL;
-       char *start, *end = NULL;
+       char *start = NULL;

start is only set and used within the loop body so no need to initialize it here.

Quoted 75 lines
libselinux/src/selinux_check_securetty_context.c +31 -34
+       char *end = NULL;
        size_t line_len = 0;
-       ssize_t len;
        int found = -1;
-       FILE *fp;
-       fp = fopen(selinux_securetty_types_path(), "re");
-       if (fp) {
-               context_t con = context_new(tty_context);
-               if (con) {
-                       const char *type = context_type_get(con);
-                       while ((len = getline(&line, &line_len, fp)) != -1) {
-
-                               if (line[len - 1] == '\n')
-                                       line[len - 1] = 0;
-
-                               /* Skip leading whitespace. */
-                               start = line;
-                               while (*start && isspace((unsigned char)*start))
-                                       start++;
-                               if (!(*start))
-                                       continue;
-
-                               end = start;
-                               while (*end && !isspace((unsigned char)*end))
-                                       end++;
-                               if (*end)
-                                       *end++ = 0;
-                               if (!strcmp(type, start)) {
-                                       found = 0;
-                                       break;
-                               }
-                       }
-                       free(line);
-                       context_free(con);
+       ssize_t len;
+       while ((len = getline(&line, &line_len, fp)) != -1) {
+
+               if (line[len - 1] == '\n') {
+                       line[len - 1] = 0;
+               }
+
+               /* Skip leading whitespace. */
+               start = line;
+               while (*start && isspace((unsigned char)*start)) {
+                       start++;
+               }
+               if (!(*start)) {
+                       continue;
+               }
+
+               end = start;
+               while (*end && !isspace((unsigned char)*end)) {
+                       end++;
+               }
+               if (*end) {
+                       *end++ = 0;
+               }
+               if (!strcmp(type, start)) {
+                       found = 0;
+                       break;
                }
-               fclose(fp);
        }
 
+       free(line);
+       context_free(con);
+       fclose(fp);
+
        return found;
 }
-
--

2.50.1

#3 lore raw

PATCH v3 1/1 libselinux: refactor selinux_check_securetty_context

Invert the checks for fp and con.

---
 .../src/selinux_check_securetty_context.c     | 69 ++++++++++---------
 1 file changed, 36 insertions(+), 33 deletions(-)
libselinux/src/selinux_check_securetty_context.c +36 -33
index 7609752e..bf61f53d 100644
@@ -8,45 +8,48 @@
 
 int selinux_check_securetty_context(const char * tty_context)
 {
+	FILE *fp = fopen(selinux_securetty_types_path(), "re");
+	if (!fp)
+		return -1;
+
+	context_t con = context_new(tty_context);
+	if (!con) {
+		fclose(fp);
+		return -1;
+	}
+
+	const char *type = context_type_get(con);
+
 	char *line = NULL;
 	char *start, *end = NULL;
 	size_t line_len = 0;
-	ssize_t len;
 	int found = -1;
-	FILE *fp;
-	fp = fopen(selinux_securetty_types_path(), "re");
-	if (fp) {
-		context_t con = context_new(tty_context);
-		if (con) {
-			const char *type = context_type_get(con);
-			while ((len = getline(&line, &line_len, fp)) != -1) {
-
-				if (line[len - 1] == '\n')
-					line[len - 1] = 0;
-
-				/* Skip leading whitespace. */
-				start = line;
-				while (*start && isspace((unsigned char)*start))
-					start++;
-				if (!(*start))
-					continue;
-
-				end = start;
-				while (*end && !isspace((unsigned char)*end))
-					end++;
-				if (*end)
-					*end++ = 0;
-				if (!strcmp(type, start)) {
-					found = 0;
-					break;
-				}
-			}
-			free(line);
-			context_free(con);
+	ssize_t len;
+	while ((len = getline(&line, &line_len, fp)) != -1) {
+		if (line[len - 1] == '\n')
+			line[len - 1] = 0;
+
+		/* Skip leading whitespace. */
+		start = line;
+		while (*start && isspace((unsigned char)*start))
+			start++;
+		if (!(*start))
+			continue;
+
+		end = start;
+		while (*end && !isspace((unsigned char)*end))
+			end++;
+		if (*end)
+			*end++ = 0;
+		if (!strcmp(type, start)) {
+			found = 0;
+			break;
 		}
-		fclose(fp);
 	}
 
+	free(line);
+	context_free(con);
+	fclose(fp);
+
 	return found;
 }
-
Signature · 1 line
2.50.1
maintainer Acked-by on v3 1/1
#4 lore raw
Quoted 4 lines · Rahul Sandhu

Invert the checks for fp and con.

Quoted 93 lines
---
 .../src/selinux_check_securetty_context.c     | 69 ++++++++++---------
 1 file changed, 36 insertions(+), 33 deletions(-)
libselinux/src/selinux_check_securetty_context.c +36 -34
index 7609752e..bf61f53d 100644
@@ -8,45 +8,48 @@
 
 int selinux_check_securetty_context(const char * tty_context)
 {
+       FILE *fp = fopen(selinux_securetty_types_path(), "re");
+       if (!fp)
+               return -1;
+
+       context_t con = context_new(tty_context);
+       if (!con) {
+               fclose(fp);
+               return -1;
+       }
+
+       const char *type = context_type_get(con);
+
        char *line = NULL;
        char *start, *end = NULL;
        size_t line_len = 0;
-       ssize_t len;
        int found = -1;
-       FILE *fp;
-       fp = fopen(selinux_securetty_types_path(), "re");
-       if (fp) {
-               context_t con = context_new(tty_context);
-               if (con) {
-                       const char *type = context_type_get(con);
-                       while ((len = getline(&line, &line_len, fp)) != -1) {
-
-                               if (line[len - 1] == '\n')
-                                       line[len - 1] = 0;
-
-                               /* Skip leading whitespace. */
-                               start = line;
-                               while (*start && isspace((unsigned char)*start))
-                                       start++;
-                               if (!(*start))
-                                       continue;
-
-                               end = start;
-                               while (*end && !isspace((unsigned char)*end))
-                                       end++;
-                               if (*end)
-                                       *end++ = 0;
-                               if (!strcmp(type, start)) {
-                                       found = 0;
-                                       break;
-                               }
-                       }
-                       free(line);
-                       context_free(con);
+       ssize_t len;
+       while ((len = getline(&line, &line_len, fp)) != -1) {
+               if (line[len - 1] == '\n')
+                       line[len - 1] = 0;
+
+               /* Skip leading whitespace. */
+               start = line;
+               while (*start && isspace((unsigned char)*start))
+                       start++;
+               if (!(*start))
+                       continue;
+
+               end = start;
+               while (*end && !isspace((unsigned char)*end))
+                       end++;
+               if (*end)
+                       *end++ = 0;
+               if (!strcmp(type, start)) {
+                       found = 0;
+                       break;
                }
-               fclose(fp);
        }
 
+       free(line);
+       context_free(con);
+       fclose(fp);
+
        return found;
 }
-
--

2.50.1