libselinux: refactor selinux_check_securetty_context
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
@@ -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
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
@@ -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
Stephen Smalley
maintainerQuoted 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
Signed-off-by Rahul Sandhu <[email protected]>--- .../src/selinux_check_securetty_context.c | 80 +++++++++++-------- 1 file changed, 46 insertions(+), 34 deletions(-)libselinux/src/selinux_check_securetty_context.c +1
-0@@ -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
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
@@ -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
Quoted 4 lines · Rahul Sandhu
Invert the checks for fp and con.
Signed-off-by Rahul Sandhu <[email protected]>
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@@ -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
Stephen Smalley
maintainerQuoted 8 lines · Stephen Smalley
Quoted 4 lines · Rahul Sandhu
Invert the checks for fp and con.
Signed-off-by Rahul Sandhu <[email protected]>Acked-by Stephen Smalley <[email protected]>
Thanks, merged.