From d423ec258c4bddd292760fb294bb209b0db385cb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 27 Feb 2026 23:34:58 +0100 Subject: [PATCH] nss-systemd,strv: use a common definition of a non-NULL but empty strv Follow-up for: 239903d44c12f10b5fe7c1f8457ae5203e47d8cc --- src/basic/macro.h | 1 - src/basic/strv.c | 2 ++ src/basic/strv.h | 6 ++++++ src/nss-systemd/nss-systemd.c | 12 ++++++------ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/basic/macro.h b/src/basic/macro.h index b53805d5b8..7001c33139 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -152,7 +152,6 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) { } while (false) #define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL })) -#define STRV_EMPTY ((char*[1]) { NULL }) #define STRV_MAKE_CONST(...) ((const char* const*) ((const char*[]) { __VA_ARGS__, NULL })) /* Pointers range from NULL to POINTER_MAX */ diff --git a/src/basic/strv.c b/src/basic/strv.c index d930770f9e..6cbc9633ae 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -1297,3 +1297,5 @@ char** strv_filter_prefix(char * const *l, const char *prefix) { return TAKE_PTR(f); } + +const char* const strv_empty[] = { NULL }; diff --git a/src/basic/strv.h b/src/basic/strv.h index 4f7d5a82a4..7249d8a311 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -221,3 +221,9 @@ int string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const c int strv_rebreak_lines(char **l, size_t width, char ***ret); char** strv_filter_prefix(char * const *l, const char *prefix); + +/* whenever we need to initialize something with a constant non-NULL, but empty strv, we can use this shared + * one */ +extern const char* const strv_empty[1]; + +#define STRV_EMPTY ((char**) strv_empty) diff --git a/src/nss-systemd/nss-systemd.c b/src/nss-systemd/nss-systemd.c index 11384dbad8..6ed97f31a6 100644 --- a/src/nss-systemd/nss-systemd.c +++ b/src/nss-systemd/nss-systemd.c @@ -69,28 +69,28 @@ static const struct group root_group = { .gr_name = (char*) "root", .gr_gid = 0, .gr_passwd = (char*) PASSWORD_SEE_SHADOW, - .gr_mem = (char*[]) { NULL }, + .gr_mem = STRV_EMPTY, }; static const struct sgrp root_sgrp = { .sg_namp = (char*) "root", .sg_passwd = (char*) PASSWORD_LOCKED_AND_INVALID, - .sg_adm = (char*[]) { NULL }, - .sg_mem = (char*[]) { NULL }, + .sg_adm = STRV_EMPTY, + .sg_mem = STRV_EMPTY, }; static const struct group nobody_group = { .gr_name = (char*) NOBODY_GROUP_NAME, .gr_gid = GID_NOBODY, .gr_passwd = (char*) PASSWORD_LOCKED_AND_INVALID, - .gr_mem = (char*[]) { NULL }, + .gr_mem = STRV_EMPTY, }; static const struct sgrp nobody_sgrp = { .sg_namp = (char*) NOBODY_GROUP_NAME, .sg_passwd = (char*) PASSWORD_LOCKED_AND_INVALID, - .sg_adm = (char*[]) { NULL }, - .sg_mem = (char*[]) { NULL }, + .sg_adm = STRV_EMPTY, + .sg_mem = STRV_EMPTY, }; typedef struct GetentData {