diff mbox series

[RFC,v2,02/27] libselinux: misc label cleanup

Message ID 20230814132025.45364-3-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit 6ec7a49c3b43
Delegated to: Petr Lautrbach
Headers show
Series libselinux: rework selabel_file(5) database | expand

Commit Message

Christian Göttsche Aug. 14, 2023, 1:20 p.m. UTC
Drop unused parameter from selabel_is_digest_set().  It is only written
to but writes to the function local copy of the pointer are void.

Constify read-only handle parameter of selabel_validate() and
compat_validate().

Constify read-only from-address parameter of digest_add_specfile().

Constify read-only function pointer array initfuncs.

Merge malloc(3) and memset(3) calls into calloc(3).

Simplify boolean assignment.

Drop duplicate include file.

Drop return at end of void function.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/label.c          | 17 +++++++----------
 libselinux/src/label_internal.h |  6 +++---
 libselinux/src/label_support.c  |  4 +---
 libselinux/src/matchpathcon.c   |  2 +-
 4 files changed, 12 insertions(+), 17 deletions(-)

Comments

James Carter Oct. 2, 2023, 5:13 p.m. UTC | #1
On Mon, Aug 14, 2023 at 9:41 AM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> Drop unused parameter from selabel_is_digest_set().  It is only written
> to but writes to the function local copy of the pointer are void.
>
> Constify read-only handle parameter of selabel_validate() and
> compat_validate().
>
> Constify read-only from-address parameter of digest_add_specfile().
>
> Constify read-only function pointer array initfuncs.
>
> Merge malloc(3) and memset(3) calls into calloc(3).
>
> Simplify boolean assignment.
>
> Drop duplicate include file.
>
> Drop return at end of void function.
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  libselinux/src/label.c          | 17 +++++++----------
>  libselinux/src/label_internal.h |  6 +++---
>  libselinux/src/label_support.c  |  4 +---
>  libselinux/src/matchpathcon.c   |  2 +-
>  4 files changed, 12 insertions(+), 17 deletions(-)
>
> diff --git a/libselinux/src/label.c b/libselinux/src/label.c
> index 586e5e5e..a2efa99c 100644
> --- a/libselinux/src/label.c
> +++ b/libselinux/src/label.c
> @@ -45,7 +45,7 @@ typedef int (*selabel_initfunc)(struct selabel_handle *rec,
>                                 const struct selinux_opt *opts,
>                                 unsigned nopts);
>
> -static selabel_initfunc initfuncs[] = {
> +static const selabel_initfunc initfuncs[] = {
>         &selabel_file_init,
>         CONFIG_MEDIA_BACKEND(selabel_media_init),
>         CONFIG_X_BACKEND(selabel_x_init),
> @@ -56,8 +56,7 @@ static selabel_initfunc initfuncs[] = {
>
>  static inline struct selabel_digest *selabel_is_digest_set
>                                     (const struct selinux_opt *opts,
> -                                   unsigned n,
> -                                   struct selabel_digest *entry)
> +                                   unsigned n)
>  {
>         struct selabel_digest *digest = NULL;
>
> @@ -77,8 +76,7 @@ static inline struct selabel_digest *selabel_is_digest_set
>                         if (!digest->specfile_list)
>                                 goto err;
>
> -                       entry = digest;
> -                       return entry;
> +                       return digest;
>                 }
>         }
>         return NULL;
> @@ -121,7 +119,7 @@ static inline int selabel_is_validate_set(const struct selinux_opt *opts,
>         return 0;
>  }
>
> -int selabel_validate(struct selabel_handle *rec,
> +int selabel_validate(const struct selabel_handle *rec,
>                      struct selabel_lookup_rec *contexts)
>  {
>         int rc = 0;
> @@ -133,7 +131,7 @@ int selabel_validate(struct selabel_handle *rec,
>         if (rc < 0)
>                 goto out;
>
> -       contexts->validated = 1;
> +       contexts->validated = true;
>  out:
>         return rc;
>  }
> @@ -215,15 +213,14 @@ struct selabel_handle *selabel_open(unsigned int backend,
>                 goto out;
>         }
>
> -       rec = (struct selabel_handle *)malloc(sizeof(*rec));
> +       rec = (struct selabel_handle *)calloc(1, sizeof(*rec));
>         if (!rec)
>                 goto out;
>
> -       memset(rec, 0, sizeof(*rec));
>         rec->backend = backend;
>         rec->validating = selabel_is_validate_set(opts, nopts);
>
> -       rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
> +       rec->digest = selabel_is_digest_set(opts, nopts);
>
>         if ((*initfuncs[backend])(rec, opts, nopts)) {
>                 if (rec->digest)
> diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h
> index 782c6aa8..273a630a 100644
> --- a/libselinux/src/label_internal.h
> +++ b/libselinux/src/label_internal.h
> @@ -63,7 +63,7 @@ struct selabel_digest {
>  };
>
>  extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp,
> -                                                   char *from_addr,
> +                                                   const char *from_addr,
>                                                     size_t buf_len,
>                                                     const char *path);
>  extern void digest_gen_hash(struct selabel_digest *digest);
> @@ -118,7 +118,7 @@ struct selabel_handle {
>   * Validation function
>   */
>  extern int
> -selabel_validate(struct selabel_handle *rec,
> +selabel_validate(const struct selabel_handle *rec,
>                  struct selabel_lookup_rec *contexts) ;
>
>  /*
> @@ -136,7 +136,7 @@ extern void __attribute__ ((format(printf, 1, 2)))
>         } while (0)
>
>  extern int
> -compat_validate(struct selabel_handle *rec,
> +compat_validate(const struct selabel_handle *rec,
>                 struct selabel_lookup_rec *contexts,
>                 const char *path, unsigned lineno) ;
>
> diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c
> index 54fd49a5..df474cba 100644
> --- a/libselinux/src/label_support.c
> +++ b/libselinux/src/label_support.c
> @@ -10,7 +10,6 @@
>  #include <string.h>
>  #include <stdio.h>
>  #include <errno.h>
> -#include <errno.h>
>  #include "label_internal.h"
>
>  /*
> @@ -138,7 +137,6 @@ void  digest_gen_hash(struct selabel_digest *digest)
>         Sha1Finalise(&context, (SHA1_HASH *)digest->digest);
>         free(digest->hashbuf);
>         digest->hashbuf = NULL;
> -       return;
>  }
>
>  /**
> @@ -154,7 +152,7 @@ void  digest_gen_hash(struct selabel_digest *digest)
>   * Return %0 on success, -%1 with @errno set on failure.
>   */
>  int  digest_add_specfile(struct selabel_digest *digest, FILE *fp,
> -                                   char *from_addr, size_t buf_len,
> +                                   const char *from_addr, size_t buf_len,
>                                     const char *path)
>  {
>         unsigned char *tmp_buf;
> diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c
> index bf2da083..9a9bafb0 100644
> --- a/libselinux/src/matchpathcon.c
> +++ b/libselinux/src/matchpathcon.c
> @@ -35,7 +35,7 @@ void set_matchpathcon_printf(void (*f) (const char *fmt, ...))
>         myprintf_compat = 1;
>  }
>
> -int compat_validate(struct selabel_handle *rec,
> +int compat_validate(const struct selabel_handle *rec,
>                     struct selabel_lookup_rec *contexts,
>                     const char *path, unsigned lineno)
>  {
> --
> 2.40.1
>
James Carter Oct. 12, 2023, 5:52 p.m. UTC | #2
On Mon, Oct 2, 2023 at 1:13 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Mon, Aug 14, 2023 at 9:41 AM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > Drop unused parameter from selabel_is_digest_set().  It is only written
> > to but writes to the function local copy of the pointer are void.
> >
> > Constify read-only handle parameter of selabel_validate() and
> > compat_validate().
> >
> > Constify read-only from-address parameter of digest_add_specfile().
> >
> > Constify read-only function pointer array initfuncs.
> >
> > Merge malloc(3) and memset(3) calls into calloc(3).
> >
> > Simplify boolean assignment.
> >
> > Drop duplicate include file.
> >
> > Drop return at end of void function.
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>
Merged.
Thanks,
Jim

> > ---
> >  libselinux/src/label.c          | 17 +++++++----------
> >  libselinux/src/label_internal.h |  6 +++---
> >  libselinux/src/label_support.c  |  4 +---
> >  libselinux/src/matchpathcon.c   |  2 +-
> >  4 files changed, 12 insertions(+), 17 deletions(-)
> >
> > diff --git a/libselinux/src/label.c b/libselinux/src/label.c
> > index 586e5e5e..a2efa99c 100644
> > --- a/libselinux/src/label.c
> > +++ b/libselinux/src/label.c
> > @@ -45,7 +45,7 @@ typedef int (*selabel_initfunc)(struct selabel_handle *rec,
> >                                 const struct selinux_opt *opts,
> >                                 unsigned nopts);
> >
> > -static selabel_initfunc initfuncs[] = {
> > +static const selabel_initfunc initfuncs[] = {
> >         &selabel_file_init,
> >         CONFIG_MEDIA_BACKEND(selabel_media_init),
> >         CONFIG_X_BACKEND(selabel_x_init),
> > @@ -56,8 +56,7 @@ static selabel_initfunc initfuncs[] = {
> >
> >  static inline struct selabel_digest *selabel_is_digest_set
> >                                     (const struct selinux_opt *opts,
> > -                                   unsigned n,
> > -                                   struct selabel_digest *entry)
> > +                                   unsigned n)
> >  {
> >         struct selabel_digest *digest = NULL;
> >
> > @@ -77,8 +76,7 @@ static inline struct selabel_digest *selabel_is_digest_set
> >                         if (!digest->specfile_list)
> >                                 goto err;
> >
> > -                       entry = digest;
> > -                       return entry;
> > +                       return digest;
> >                 }
> >         }
> >         return NULL;
> > @@ -121,7 +119,7 @@ static inline int selabel_is_validate_set(const struct selinux_opt *opts,
> >         return 0;
> >  }
> >
> > -int selabel_validate(struct selabel_handle *rec,
> > +int selabel_validate(const struct selabel_handle *rec,
> >                      struct selabel_lookup_rec *contexts)
> >  {
> >         int rc = 0;
> > @@ -133,7 +131,7 @@ int selabel_validate(struct selabel_handle *rec,
> >         if (rc < 0)
> >                 goto out;
> >
> > -       contexts->validated = 1;
> > +       contexts->validated = true;
> >  out:
> >         return rc;
> >  }
> > @@ -215,15 +213,14 @@ struct selabel_handle *selabel_open(unsigned int backend,
> >                 goto out;
> >         }
> >
> > -       rec = (struct selabel_handle *)malloc(sizeof(*rec));
> > +       rec = (struct selabel_handle *)calloc(1, sizeof(*rec));
> >         if (!rec)
> >                 goto out;
> >
> > -       memset(rec, 0, sizeof(*rec));
> >         rec->backend = backend;
> >         rec->validating = selabel_is_validate_set(opts, nopts);
> >
> > -       rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
> > +       rec->digest = selabel_is_digest_set(opts, nopts);
> >
> >         if ((*initfuncs[backend])(rec, opts, nopts)) {
> >                 if (rec->digest)
> > diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h
> > index 782c6aa8..273a630a 100644
> > --- a/libselinux/src/label_internal.h
> > +++ b/libselinux/src/label_internal.h
> > @@ -63,7 +63,7 @@ struct selabel_digest {
> >  };
> >
> >  extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp,
> > -                                                   char *from_addr,
> > +                                                   const char *from_addr,
> >                                                     size_t buf_len,
> >                                                     const char *path);
> >  extern void digest_gen_hash(struct selabel_digest *digest);
> > @@ -118,7 +118,7 @@ struct selabel_handle {
> >   * Validation function
> >   */
> >  extern int
> > -selabel_validate(struct selabel_handle *rec,
> > +selabel_validate(const struct selabel_handle *rec,
> >                  struct selabel_lookup_rec *contexts) ;
> >
> >  /*
> > @@ -136,7 +136,7 @@ extern void __attribute__ ((format(printf, 1, 2)))
> >         } while (0)
> >
> >  extern int
> > -compat_validate(struct selabel_handle *rec,
> > +compat_validate(const struct selabel_handle *rec,
> >                 struct selabel_lookup_rec *contexts,
> >                 const char *path, unsigned lineno) ;
> >
> > diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c
> > index 54fd49a5..df474cba 100644
> > --- a/libselinux/src/label_support.c
> > +++ b/libselinux/src/label_support.c
> > @@ -10,7 +10,6 @@
> >  #include <string.h>
> >  #include <stdio.h>
> >  #include <errno.h>
> > -#include <errno.h>
> >  #include "label_internal.h"
> >
> >  /*
> > @@ -138,7 +137,6 @@ void  digest_gen_hash(struct selabel_digest *digest)
> >         Sha1Finalise(&context, (SHA1_HASH *)digest->digest);
> >         free(digest->hashbuf);
> >         digest->hashbuf = NULL;
> > -       return;
> >  }
> >
> >  /**
> > @@ -154,7 +152,7 @@ void  digest_gen_hash(struct selabel_digest *digest)
> >   * Return %0 on success, -%1 with @errno set on failure.
> >   */
> >  int  digest_add_specfile(struct selabel_digest *digest, FILE *fp,
> > -                                   char *from_addr, size_t buf_len,
> > +                                   const char *from_addr, size_t buf_len,
> >                                     const char *path)
> >  {
> >         unsigned char *tmp_buf;
> > diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c
> > index bf2da083..9a9bafb0 100644
> > --- a/libselinux/src/matchpathcon.c
> > +++ b/libselinux/src/matchpathcon.c
> > @@ -35,7 +35,7 @@ void set_matchpathcon_printf(void (*f) (const char *fmt, ...))
> >         myprintf_compat = 1;
> >  }
> >
> > -int compat_validate(struct selabel_handle *rec,
> > +int compat_validate(const struct selabel_handle *rec,
> >                     struct selabel_lookup_rec *contexts,
> >                     const char *path, unsigned lineno)
> >  {
> > --
> > 2.40.1
> >
diff mbox series

Patch

diff --git a/libselinux/src/label.c b/libselinux/src/label.c
index 586e5e5e..a2efa99c 100644
--- a/libselinux/src/label.c
+++ b/libselinux/src/label.c
@@ -45,7 +45,7 @@  typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts,
 				unsigned nopts);
 
-static selabel_initfunc initfuncs[] = {
+static const selabel_initfunc initfuncs[] = {
 	&selabel_file_init,
 	CONFIG_MEDIA_BACKEND(selabel_media_init),
 	CONFIG_X_BACKEND(selabel_x_init),
@@ -56,8 +56,7 @@  static selabel_initfunc initfuncs[] = {
 
 static inline struct selabel_digest *selabel_is_digest_set
 				    (const struct selinux_opt *opts,
-				    unsigned n,
-				    struct selabel_digest *entry)
+				    unsigned n)
 {
 	struct selabel_digest *digest = NULL;
 
@@ -77,8 +76,7 @@  static inline struct selabel_digest *selabel_is_digest_set
 			if (!digest->specfile_list)
 				goto err;
 
-			entry = digest;
-			return entry;
+			return digest;
 		}
 	}
 	return NULL;
@@ -121,7 +119,7 @@  static inline int selabel_is_validate_set(const struct selinux_opt *opts,
 	return 0;
 }
 
-int selabel_validate(struct selabel_handle *rec,
+int selabel_validate(const struct selabel_handle *rec,
 		     struct selabel_lookup_rec *contexts)
 {
 	int rc = 0;
@@ -133,7 +131,7 @@  int selabel_validate(struct selabel_handle *rec,
 	if (rc < 0)
 		goto out;
 
-	contexts->validated = 1;
+	contexts->validated = true;
 out:
 	return rc;
 }
@@ -215,15 +213,14 @@  struct selabel_handle *selabel_open(unsigned int backend,
 		goto out;
 	}
 
-	rec = (struct selabel_handle *)malloc(sizeof(*rec));
+	rec = (struct selabel_handle *)calloc(1, sizeof(*rec));
 	if (!rec)
 		goto out;
 
-	memset(rec, 0, sizeof(*rec));
 	rec->backend = backend;
 	rec->validating = selabel_is_validate_set(opts, nopts);
 
-	rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
+	rec->digest = selabel_is_digest_set(opts, nopts);
 
 	if ((*initfuncs[backend])(rec, opts, nopts)) {
 		if (rec->digest)
diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h
index 782c6aa8..273a630a 100644
--- a/libselinux/src/label_internal.h
+++ b/libselinux/src/label_internal.h
@@ -63,7 +63,7 @@  struct selabel_digest {
 };
 
 extern int digest_add_specfile(struct selabel_digest *digest, FILE *fp,
-						    char *from_addr,
+						    const char *from_addr,
 						    size_t buf_len,
 						    const char *path);
 extern void digest_gen_hash(struct selabel_digest *digest);
@@ -118,7 +118,7 @@  struct selabel_handle {
  * Validation function
  */
 extern int
-selabel_validate(struct selabel_handle *rec,
+selabel_validate(const struct selabel_handle *rec,
 		 struct selabel_lookup_rec *contexts) ;
 
 /*
@@ -136,7 +136,7 @@  extern void __attribute__ ((format(printf, 1, 2)))
 	} while (0)
 
 extern int
-compat_validate(struct selabel_handle *rec,
+compat_validate(const struct selabel_handle *rec,
 		struct selabel_lookup_rec *contexts,
 		const char *path, unsigned lineno) ;
 
diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c
index 54fd49a5..df474cba 100644
--- a/libselinux/src/label_support.c
+++ b/libselinux/src/label_support.c
@@ -10,7 +10,6 @@ 
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
-#include <errno.h>
 #include "label_internal.h"
 
 /*
@@ -138,7 +137,6 @@  void  digest_gen_hash(struct selabel_digest *digest)
 	Sha1Finalise(&context, (SHA1_HASH *)digest->digest);
 	free(digest->hashbuf);
 	digest->hashbuf = NULL;
-	return;
 }
 
 /**
@@ -154,7 +152,7 @@  void  digest_gen_hash(struct selabel_digest *digest)
  * Return %0 on success, -%1 with @errno set on failure.
  */
 int  digest_add_specfile(struct selabel_digest *digest, FILE *fp,
-				    char *from_addr, size_t buf_len,
+				    const char *from_addr, size_t buf_len,
 				    const char *path)
 {
 	unsigned char *tmp_buf;
diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c
index bf2da083..9a9bafb0 100644
--- a/libselinux/src/matchpathcon.c
+++ b/libselinux/src/matchpathcon.c
@@ -35,7 +35,7 @@  void set_matchpathcon_printf(void (*f) (const char *fmt, ...))
 	myprintf_compat = 1;
 }
 
-int compat_validate(struct selabel_handle *rec,
+int compat_validate(const struct selabel_handle *rec,
 		    struct selabel_lookup_rec *contexts,
 		    const char *path, unsigned lineno)
 {