Re: [PATCH] libselinux: use designated initializers for label initfuncs

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <CAEjxPJ46i4pXB6N94aDG1SyMXTxfg2x8qPyfUniRcU+v0FpB6g@mail.gmail.com>
On Mon, Jul 13, 2026 at 11:50 PM Thiébaud Weksteen <[email protected]> wrote:
>
> Previously, the initfuncs array relied on implicit positional ordering
> to match backend context constants (e.g., SELABEL_CTX_FILE). If constants
> were reordered or added, array indexing could become misaligned.
>
> Use C99 designated initializers in initfuncs to explicitly bind each
> initialization function to its corresponding context constant.
>
> Additionally, define _SELABEL_CTX_NUM in include/selinux/label.h
> to count total backends, and add a compile-time static_assert ensuring
> initfuncs stays in sync with _SELABEL_CTX_NUM.
>
> Signed-off-by: Thiébaud Weksteen <[email protected]>

This didn't apply cleanly for me on selinux/main and also doesn't pass
make check-format (fix with make format).

> ---
>  libselinux/include/selinux/label.h |  5 +++++
>  libselinux/src/label.c             | 16 ++++++++++------
>  2 files changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h
> index ce189a3a..0c20a4fc 100644
> --- a/libselinux/include/selinux/label.h
> +++ b/libselinux/include/selinux/label.h
> @@ -37,6 +37,11 @@ struct selabel_handle;
>  #define SELABEL_CTX_ANDROID_PROP 4
>  /* Android service contexts */
>  #define SELABEL_CTX_ANDROID_SERVICE 5
> +/*
> + * Total number of context backends. When adding a new backend,
> + * please increase this value.
> + */
> +#define _SELABEL_CTX_NUM       6
>
>  /*
>   * Available options
> diff --git a/libselinux/src/label.c b/libselinux/src/label.c
> index 2c510290..40fbd596 100644
> --- a/libselinux/src/label.c
> +++ b/libselinux/src/label.c
> @@ -4,6 +4,7 @@
>   * Author : Eamon Walsh <[email protected]>
>   */
>
> +#include <assert.h>
>  #include <sys/types.h>
>  #include <ctype.h>
>  #include <errno.h>
> @@ -46,14 +47,17 @@ typedef int (*selabel_initfunc)(struct selabel_handle *rec,
>                                 unsigned nopts);
>
>  static const selabel_initfunc initfuncs[] = {
> -       &selabel_file_init,
> -       CONFIG_MEDIA_BACKEND(selabel_media_init),
> -       CONFIG_X_BACKEND(selabel_x_init),
> -       CONFIG_DB_BACKEND(selabel_db_init),
> -       CONFIG_ANDROID_BACKEND(selabel_property_init),
> -       CONFIG_ANDROID_BACKEND(selabel_service_init),
> +       [SELABEL_CTX_FILE] = &selabel_file_init,
> +       [SELABEL_CTX_MEDIA] = CONFIG_MEDIA_BACKEND(selabel_media_init),
> +       [SELABEL_CTX_X] = CONFIG_X_BACKEND(selabel_x_init),
> +       [SELABEL_CTX_DB] = CONFIG_DB_BACKEND(selabel_db_init),
> +       [SELABEL_CTX_ANDROID_PROP] = CONFIG_ANDROID_BACKEND(selabel_property_init),
> +       [SELABEL_CTX_ANDROID_SERVICE] = CONFIG_ANDROID_BACKEND(selabel_service_init),
>  };
>
> +static_assert(ARRAY_SIZE(initfuncs) == _SELABEL_CTX_NUM,
> +               "initfuncs array size does not match _SELABEL_CTX_NUM");
> +
>  static inline struct selabel_digest *selabel_is_digest_set
>                                     (const struct selinux_opt *opts,
>                                     unsigned n)
> --
> 2.55.0.795.g602f6c329a-goog
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.