[lxc-devel] [lxc/master] macro: add SIZEOF2STRLEN()

brauner on Github lxc-bot at linuxcontainers.org
Sat Sep 22 09:22:37 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180922/c292a0c5/attachment.bin>
-------------- next part --------------
From fb6d180cb5cc1567e527921e5aca78c79963da63 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 22 Sep 2018 10:53:52 +0200
Subject: [PATCH 1/4] compiler: s/__fallthrough__/__fallthrough/g

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/compiler.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/lxc/compiler.h b/src/lxc/compiler.h
index f29ecbed2..c76b9317e 100644
--- a/src/lxc/compiler.h
+++ b/src/lxc/compiler.h
@@ -33,9 +33,9 @@
 #endif
 
 #if defined(__GNUC__) && __GNUC__ >= 7 || defined(__clang__)
-#define __fallthrough__ __attribute__((fallthrough))
+#define __fallthrough __attribute__((fallthrough))
 #else
-#define __fallthrough__
+#define __fallthrough
 #endif
 
 #ifndef _noreturn_

From 57893315ad9c9c96e9e6c19930714f4715b5661b Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 22 Sep 2018 10:56:09 +0200
Subject: [PATCH 2/4] compiler: s/__noreturn__/__noreturn/g

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/cmd/lxc_init.c    | 4 ++--
 src/lxc/compiler.h        | 4 ++--
 src/lxc/tools/arguments.c | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/lxc/cmd/lxc_init.c b/src/lxc/cmd/lxc_init.c
index 388b82173..c052fee80 100644
--- a/src/lxc/cmd/lxc_init.c
+++ b/src/lxc/cmd/lxc_init.c
@@ -467,7 +467,7 @@ int main(int argc, char *argv[])
 	exit(exit_with);
 }
 
-__noreturn__ static void print_usage_exit(const struct option longopts[])
+__noreturn static void print_usage_exit(const struct option longopts[])
 
 {
 	fprintf(stderr, "Usage: lxc-init [-n|--name=NAME] [-h|--help] [--usage] [--version]\n\
@@ -475,7 +475,7 @@ __noreturn__ static void print_usage_exit(const struct option longopts[])
 	exit(0);
 }
 
-__noreturn__ static void print_version_exit(void)
+__noreturn static void print_version_exit(void)
 {
 	printf("%s\n", LXC_VERSION);
 	exit(0);
diff --git a/src/lxc/compiler.h b/src/lxc/compiler.h
index c76b9317e..bbb99d8e3 100644
--- a/src/lxc/compiler.h
+++ b/src/lxc/compiler.h
@@ -40,9 +40,9 @@
 
 #ifndef _noreturn_
 #if __STDC_VERSION__ >= 201112L
-#define __noreturn__ _Noreturn
+#define __noreturn _Noreturn
 #else
-#define __noreturn__ __attribute__((noreturn))
+#define __noreturn __attribute__((noreturn))
 #endif
 #endif
 
diff --git a/src/lxc/tools/arguments.c b/src/lxc/tools/arguments.c
index e1e4c6e49..b47df0ecb 100644
--- a/src/lxc/tools/arguments.c
+++ b/src/lxc/tools/arguments.c
@@ -88,7 +88,7 @@ static int build_shortopts(const struct option *a_options, char *a_shortopts,
 	return -1;
 }
 
-__noreturn__ static void print_usage_exit(const struct option longopts[],
+__noreturn static void print_usage_exit(const struct option longopts[],
 					  const struct lxc_arguments *a_args)
 
 {
@@ -135,13 +135,13 @@ __noreturn__ static void print_usage_exit(const struct option longopts[],
 	exit(0);
 }
 
-__noreturn__ static void print_version_exit()
+__noreturn static void print_version_exit()
 {
 	printf("%s\n", lxc_get_version());
 	exit(0);
 }
 
-__noreturn__ static void print_help_exit(const struct lxc_arguments *args,
+__noreturn static void print_help_exit(const struct lxc_arguments *args,
 					 int code)
 {
 	fprintf(stderr, "\

From 7c79fde4ed5285819374d5197da51eb17d0585ee Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 22 Sep 2018 11:14:08 +0200
Subject: [PATCH 3/4] macro: add SIZEOF2STRLEN()

sizeof() implementation for string literals that makes it behave like
strlen() for strings.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/macro.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/lxc/macro.h b/src/lxc/macro.h
index 3ff40648a..8d13e65bf 100644
--- a/src/lxc/macro.h
+++ b/src/lxc/macro.h
@@ -348,4 +348,6 @@ extern int __build_bug_on_failed;
 #define LXC_INVALID_UID ((uid_t)-1)
 #define LXC_INVALID_GID ((gid_t)-1)
 
+#define SIZEOF2STRLEN(x) (sizeof(""x"") - 1)
+
 #endif /* __LXC_MACRO_H */

From 0439e428f3d93a9fe3480071094d36ee246d25ad Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 22 Sep 2018 11:21:04 +0200
Subject: [PATCH 4/4] tree-wide: replace sizeof() with SIZEOF2STRLEN()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/cgroups/cgfsng.c  |  9 +++++----
 src/lxc/conf.c            | 39 +++++++++++++++++++-------------------
 src/lxc/confile.c         | 40 +++++++++++++++++++--------------------
 src/lxc/confile_utils.c   |  4 ++--
 src/lxc/lsm/apparmor.c    | 18 +++++++++---------
 src/lxc/lxclock.c         |  2 +-
 src/lxc/storage/overlay.c | 25 ++++++++++++------------
 7 files changed, 70 insertions(+), 67 deletions(-)

diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
index 0f3296919..32bbba3b1 100644
--- a/src/lxc/cgroups/cgfsng.c
+++ b/src/lxc/cgroups/cgfsng.c
@@ -136,8 +136,9 @@ static char *cg_legacy_must_prefix_named(char *entry)
 	len = strlen(entry);
 	prefixed = must_alloc(len + 6);
 
-	memcpy(prefixed, "name=", sizeof("name=") - 1);
-	memcpy(prefixed + sizeof("name=") - 1, entry, len);
+
+	memcpy(prefixed, "name=", SIZEOF2STRLEN("name="));
+	memcpy(prefixed + SIZEOF2STRLEN("name="), entry, len);
 	prefixed[len + 5] = '\0';
 	return prefixed;
 }
@@ -1963,8 +1964,8 @@ static int __cg_unified_attach(const struct hierarchy *h, const char *name,
 
 	free(full_path);
 
-	len = strlen(base_path) + sizeof("/lxc-1000") - 1 +
-	      sizeof("/cgroup-procs") - 1;
+	len = strlen(base_path) + SIZEOF2STRLEN("/lxc-1000") +
+	      SIZEOF2STRLEN("/cgroup-procs");
 	full_path = must_alloc(len + 1);
 	do {
 		if (idx)
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 997588a9e..abdd44d46 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -372,7 +372,8 @@ int run_script_argv(const char *name, unsigned int hook_version,
 	for (i = 0; argv && argv[i]; i++)
 		size += strlen(argv[i]) + 1;
 
-	size += sizeof("exec");
+	size += SIZEOF2STRLEN("exec");
+	size++;
 	size += strlen(script);
 	size++;
 
@@ -1650,13 +1651,13 @@ static int lxc_setup_devpts(struct lxc_conf *conf)
 	mntopt_sets[0] = devpts_mntopts;
 
 	/* !gid=5 && max= */
-	mntopt_sets[1] = devpts_mntopts + sizeof("gid=5");
+	mntopt_sets[1] = devpts_mntopts + SIZEOF2STRLEN("gid=5") + 1;
 
 	/* gid=5 && !max= */
 	mntopt_sets[2] = default_devpts_mntopts;
 
 	/* !gid=5 && !max= */
-	mntopt_sets[3] = default_devpts_mntopts + sizeof("gid=5");
+	mntopt_sets[3] = default_devpts_mntopts + SIZEOF2STRLEN("gid=5") + 1;
 
 	/* end */
 	mntopt_sets[4] = NULL;
@@ -2440,8 +2441,8 @@ FILE *make_anonymous_mount_file(struct lxc_list *mount,
 
 	if (include_nesting_helpers) {
 		ret = lxc_write_nointr(fd, nesting_helpers,
-				       sizeof(nesting_helpers) - 1);
-		if (ret != sizeof(nesting_helpers) - 1)
+				       SIZEOF2STRLEN(nesting_helpers));
+		if (ret != SIZEOF2STRLEN(nesting_helpers))
 			goto on_error;
 	}
 
@@ -2821,7 +2822,7 @@ int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
 		}
 
 		if (fd >= 0) {
-			buflen = sizeof("deny\n") - 1;
+			buflen = SIZEOF2STRLEN("deny\n");
 			errno = 0;
 			ret = lxc_write_nointr(fd, "deny\n", buflen);
 			close(fd);
@@ -3850,12 +3851,12 @@ int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version)
 	if (version == CGROUP2_SUPER_MAGIC) {
 		global_token = "lxc.cgroup2";
 		namespaced_token = "lxc.cgroup2.";
-		namespaced_token_len = sizeof("lxc.cgroup2.") - 1;
+		namespaced_token_len = SIZEOF2STRLEN("lxc.cgroup2.");
 		list = &c->cgroup2;
 	} else if (version == CGROUP_SUPER_MAGIC) {
 		global_token = "lxc.cgroup";
 		namespaced_token = "lxc.cgroup.";
-		namespaced_token_len = sizeof("lxc.cgroup.") - 1;
+		namespaced_token_len = SIZEOF2STRLEN("lxc.cgroup.");
 		list = &c->cgroup;
 	} else {
 		return -EINVAL;
@@ -3863,7 +3864,7 @@ int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version)
 
 	if (strcmp(key, global_token) == 0)
 		all = true;
-	else if (strncmp(key, namespaced_token, sizeof(namespaced_token) - 1) == 0)
+	else if (strncmp(key, namespaced_token, SIZEOF2STRLEN(namespaced_token)) == 0)
 		k += namespaced_token_len;
 	else
 		return -EINVAL;
@@ -3892,10 +3893,10 @@ int lxc_clear_limits(struct lxc_conf *c, const char *key)
 
 	if (strcmp(key, "lxc.limit") == 0 || strcmp(key, "lxc.prlimit") == 0)
 		all = true;
-	else if (strncmp(key, "lxc.limit.", sizeof("lxc.limit.") - 1) == 0)
-		k = key + sizeof("lxc.limit.") - 1;
-	else if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) == 0)
-		k = key + sizeof("lxc.prlimit.") - 1;
+	else if (strncmp(key, "lxc.limit.", SIZEOF2STRLEN("lxc.limit.")) == 0)
+		k = key + SIZEOF2STRLEN("lxc.limit.");
+	else if (strncmp(key, "lxc.prlimit.", SIZEOF2STRLEN("lxc.prlimit.")) == 0)
+		k = key + SIZEOF2STRLEN("lxc.prlimit.");
 	else
 		return -1;
 
@@ -3922,8 +3923,8 @@ int lxc_clear_sysctls(struct lxc_conf *c, const char *key)
 
 	if (strcmp(key, "lxc.sysctl") == 0)
 		all = true;
-	else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
-		k = key + sizeof("lxc.sysctl.") - 1;
+	else if (strncmp(key, "lxc.sysctl.", SIZEOF2STRLEN("lxc.sysctl.")) == 0)
+		k = key + SIZEOF2STRLEN("lxc.sysctl.");
 	else
 		return -1;
 
@@ -3951,8 +3952,8 @@ int lxc_clear_procs(struct lxc_conf *c, const char *key)
 
 	if (strcmp(key, "lxc.proc") == 0)
 		all = true;
-	else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
-		k = key + sizeof("lxc.proc.") - 1;
+	else if (strncmp(key, "lxc.proc.", SIZEOF2STRLEN("lxc.proc.")) == 0)
+		k = key + SIZEOF2STRLEN("lxc.proc.");
 	else
 		return -1;
 
@@ -4026,8 +4027,8 @@ int lxc_clear_hooks(struct lxc_conf *c, const char *key)
 
 	if (strcmp(key, "lxc.hook") == 0)
 		all = true;
-	else if (strncmp(key, "lxc.hook.", sizeof("lxc.hook.") - 1) == 0)
-		k = key + sizeof("lxc.hook.") - 1;
+	else if (strncmp(key, "lxc.hook.", SIZEOF2STRLEN("lxc.hook.")) == 0)
+		k = key + SIZEOF2STRLEN("lxc.hook.");
 	else
 		return -1;
 
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index b6b3032bb..1b4dc0242 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -1437,10 +1437,10 @@ static int set_config_prlimit(const char *key, const char *value,
 	if (lxc_config_value_empty(value))
 		return lxc_clear_limits(lxc_conf, key);
 
-	if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
+	if (strncmp(key, "lxc.prlimit.", SIZEOF2STRLEN("lxc.prlimit.")) != 0)
 		return -1;
 
-	key += sizeof("lxc.prlimit.") - 1;
+	key += SIZEOF2STRLEN("lxc.prlimit.");
 
 	/* soft limit comes first in the value */
 	if (!parse_limit_value(&value, &limit_value))
@@ -1526,10 +1526,10 @@ static int set_config_sysctl(const char *key, const char *value,
 	if (lxc_config_value_empty(value))
 		return clr_config_sysctl(key, lxc_conf, NULL);
 
-	if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
+	if (strncmp(key, "lxc.sysctl.", SIZEOF2STRLEN("lxc.sysctl.")) != 0)
 		return -1;
 
-	key += sizeof("lxc.sysctl.") - 1;
+	key += SIZEOF2STRLEN("lxc.sysctl.");
 
 	/* find existing list element */
 	lxc_list_for_each(iter, &lxc_conf->sysctls) {
@@ -1591,10 +1591,10 @@ static int set_config_proc(const char *key, const char *value,
 	if (lxc_config_value_empty(value))
 		return clr_config_proc(key, lxc_conf, NULL);
 
-	if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
+	if (strncmp(key, "lxc.proc.", SIZEOF2STRLEN("lxc.proc.")) != 0)
 		return -1;
 
-	subkey = key + sizeof("lxc.proc.") - 1;
+	subkey = key + SIZEOF2STRLEN("lxc.proc.");
 	if (*subkey == '\0')
 		return -EINVAL;
 
@@ -1761,7 +1761,7 @@ static int set_config_mount_auto(const char *key, const char *value,
 				break;
 
 			if (strcmp("shmounts:", allowed_auto_mounts[i].token) == 0 &&
-			    strncmp("shmounts:", token, sizeof("shmounts:") - 1) == 0) {
+			    strncmp("shmounts:", token, SIZEOF2STRLEN("shmounts:")) == 0) {
 				is_shmounts = true;
 				break;
 			}
@@ -1775,7 +1775,7 @@ static int set_config_mount_auto(const char *key, const char *value,
 		lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
 		lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
 		if (is_shmounts) {
-			lxc_conf->shmount.path_host = strdup(token + (sizeof("shmounts:") - 1));
+			lxc_conf->shmount.path_host = strdup(token + SIZEOF2STRLEN("shmounts:"));
 			if (!lxc_conf->shmount.path_host) {
 				SYSERROR("Failed to copy shmounts host path");
 				goto on_error;
@@ -2331,7 +2331,7 @@ static int set_config_namespace_share(const char *key, const char *value,
 	if (lxc_config_value_empty(value))
 		return clr_config_namespace_share(key, lxc_conf, data);
 
-	namespace = key + sizeof("lxc.namespace.share.") - 1;
+	namespace = key + SIZEOF2STRLEN("lxc.namespace.share.");
 	ns_idx = lxc_namespace_2_ns_idx(namespace);
 	if (ns_idx < 0)
 		return ns_idx;
@@ -3177,11 +3177,11 @@ static int __get_config_cgroup_controller(const char *key, char *retv,
 	if (version == CGROUP2_SUPER_MAGIC) {
 		global_token = "lxc.cgroup2";
 		namespaced_token = "lxc.cgroup2.";
-		namespaced_token_len = sizeof("lxc.cgroup2.") - 1;;
+		namespaced_token_len = SIZEOF2STRLEN("lxc.cgroup2.");
 	} else if (version == CGROUP_SUPER_MAGIC) {
 		global_token = "lxc.cgroup";
 		namespaced_token = "lxc.cgroup.";
-		namespaced_token_len = sizeof("lxc.cgroup.") - 1;;
+		namespaced_token_len = SIZEOF2STRLEN("lxc.cgroup.");
 	} else {
 		return -1;
 	}
@@ -3743,8 +3743,8 @@ static int get_config_prlimit(const char *key, char *retv, int inlen,
 		struct lxc_limit *lim = it->elem;
 
 		if (lim->limit.rlim_cur == RLIM_INFINITY) {
-			memcpy(buf, "unlimited", sizeof("unlimited"));
-			partlen = sizeof("unlimited") - 1;
+			memcpy(buf, "unlimited", SIZEOF2STRLEN("unlimited") + 1);
+			partlen = SIZEOF2STRLEN("unlimited");
 		} else {
 			partlen = sprintf(buf, "%" PRIu64,
 					  (uint64_t)lim->limit.rlim_cur);
@@ -3752,7 +3752,7 @@ static int get_config_prlimit(const char *key, char *retv, int inlen,
 		if (lim->limit.rlim_cur != lim->limit.rlim_max) {
 			if (lim->limit.rlim_max == RLIM_INFINITY)
 				memcpy(buf + partlen, ":unlimited",
-				       sizeof(":unlimited"));
+				       SIZEOF2STRLEN(":unlimited") + 1);
 			else
 				sprintf(buf + partlen, ":%" PRIu64,
 					(uint64_t)lim->limit.rlim_max);
@@ -3788,8 +3788,8 @@ static int get_config_sysctl(const char *key, char *retv, int inlen,
 
 	if (strcmp(key, "lxc.sysctl") == 0)
 		get_all = true;
-	else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
-		key += sizeof("lxc.sysctl.") - 1;
+	else if (strncmp(key, "lxc.sysctl.", SIZEOF2STRLEN("lxc.sysctl.")) == 0)
+		key += SIZEOF2STRLEN("lxc.sysctl.");
 	else
 		return -1;
 
@@ -3821,8 +3821,8 @@ static int get_config_proc(const char *key, char *retv, int inlen,
 
 	if (strcmp(key, "lxc.proc") == 0)
 		get_all = true;
-	else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
-		key += sizeof("lxc.proc.") - 1;
+	else if (strncmp(key, "lxc.proc.", SIZEOF2STRLEN("lxc.proc.")) == 0)
+		key += SIZEOF2STRLEN("lxc.proc.");
 	else
 		return -1;
 
@@ -3890,7 +3890,7 @@ static int get_config_namespace_share(const char *key, char *retv, int inlen,
 	else
 		memset(retv, 0, inlen);
 
-	namespace = key + sizeof("lxc.namespace.share.") - 1;
+	namespace = key + SIZEOF2STRLEN("lxc.namespace.share.");
 	ns_idx = lxc_namespace_2_ns_idx(namespace);
 	if (ns_idx < 0)
 		return ns_idx;
@@ -4335,7 +4335,7 @@ static int clr_config_namespace_share(const char *key,
 	int ns_idx;
 	const char *namespace;
 
-	namespace = key + sizeof("lxc.namespace.share.") - 1;
+	namespace = key + SIZEOF2STRLEN("lxc.namespace.share.");
 	ns_idx = lxc_namespace_2_ns_idx(namespace);
 	if (ns_idx < 0)
 		return ns_idx;
diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c
index 312f6b759..924a2b6be 100644
--- a/src/lxc/confile_utils.c
+++ b/src/lxc/confile_utils.c
@@ -736,9 +736,9 @@ bool parse_limit_value(const char **value, rlim_t *res)
 {
 	char *endptr = NULL;
 
-	if (strncmp(*value, "unlimited", sizeof("unlimited") - 1) == 0) {
+	if (strncmp(*value, "unlimited", SIZEOF2STRLEN("unlimited")) == 0) {
 		*res = RLIM_INFINITY;
-		*value += sizeof("unlimited") - 1;
+		*value += SIZEOF2STRLEN("unlimited");
 		return true;
 	}
 
diff --git a/src/lxc/lsm/apparmor.c b/src/lxc/lsm/apparmor.c
index 88643e26d..6aa39b939 100644
--- a/src/lxc/lsm/apparmor.c
+++ b/src/lxc/lsm/apparmor.c
@@ -667,21 +667,21 @@ static char *get_apparmor_profile_content(struct lxc_conf *conf, const char *lxc
 	size = strlen(profile);
 
 	must_append_sized(&profile, &size, AA_PROFILE_BASE,
-	                  sizeof(AA_PROFILE_BASE) - 1);
+	                  SIZEOF2STRLEN(AA_PROFILE_BASE));
 
 	if (aa_supports_unix)
 		must_append_sized(&profile, &size, AA_PROFILE_UNIX_SOCKETS,
-		                  sizeof(AA_PROFILE_UNIX_SOCKETS) - 1);
+		                  SIZEOF2STRLEN(AA_PROFILE_UNIX_SOCKETS));
 
 	if (file_exists("/proc/self/ns/cgroup"))
 		must_append_sized(&profile, &size, AA_PROFILE_CGROUP_NAMESPACES,
-		                  sizeof(AA_PROFILE_CGROUP_NAMESPACES) - 1);
+		                  SIZEOF2STRLEN(AA_PROFILE_CGROUP_NAMESPACES));
 
 	if (aa_can_stack && !aa_is_stacked) {
 		char *namespace, *temp;
 
 		must_append_sized(&profile, &size, AA_PROFILE_STACKING_BASE,
-		                  sizeof(AA_PROFILE_STACKING_BASE) - 1);
+		                  SIZEOF2STRLEN(AA_PROFILE_STACKING_BASE));
 
 		namespace = apparmor_namespace(conf->name, lxcpath);
 		temp = must_concat("  change_profile -> \":", namespace, ":*\",\n"
@@ -693,12 +693,12 @@ static char *get_apparmor_profile_content(struct lxc_conf *conf, const char *lxc
 		free(temp);
 	} else {
 		must_append_sized(&profile, &size, AA_PROFILE_NO_STACKING,
-		                  sizeof(AA_PROFILE_NO_STACKING) - 1);
+		                  SIZEOF2STRLEN(AA_PROFILE_NO_STACKING));
 	}
 
 	if (conf->lsm_aa_allow_nesting) {
 		must_append_sized(&profile, &size, AA_PROFILE_NESTING_BASE,
-		                  sizeof(AA_PROFILE_NESTING_BASE) - 1);
+		                  SIZEOF2STRLEN(AA_PROFILE_NESTING_BASE));
 
 		if (!aa_can_stack || aa_is_stacked) {
 			char *temp;
@@ -712,7 +712,7 @@ static char *get_apparmor_profile_content(struct lxc_conf *conf, const char *lxc
 
 	if (!is_privileged(conf) || am_host_unpriv())
 		must_append_sized(&profile, &size, AA_PROFILE_UNPRIVILEGED,
-		                  sizeof(AA_PROFILE_UNPRIVILEGED) - 1);
+		                  SIZEOF2STRLEN(AA_PROFILE_UNPRIVILEGED));
 
 	lxc_list_for_each(it, &conf->lsm_aa_raw) {
 		const char *line = it->elem;
@@ -999,9 +999,9 @@ static int apparmor_prepare(struct lxc_conf *conf, const char *lxcpath)
 		if (aa_can_stack && !aa_is_stacked) {
 			char *namespace = apparmor_namespace(conf->name, lxcpath);
 			size_t llen = strlen(genlabel);
-			must_append_sized(&genlabel, &llen, "//&:", sizeof("//&:") - 1);
+			must_append_sized(&genlabel, &llen, "//&:", SIZEOF2STRLEN("//&:"));
 			must_append_sized(&genlabel, &llen, namespace, strlen(namespace));
-			must_append_sized(&genlabel, &llen, ":", sizeof(":")); /* with the nul byte */
+			must_append_sized(&genlabel, &llen, ":", SIZEOF2STRLEN(":") + 1); /* with the nul byte */
 			free(namespace);
 		}
 
diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
index 62fe2127f..63e09301d 100644
--- a/src/lxc/lxclock.c
+++ b/src/lxc/lxclock.c
@@ -106,7 +106,7 @@ static char *lxclock_name(const char *p, const char *n)
 	 */
 
 	/* length of "/lxc/lock/" + $lxcpath + "/" + "." + $lxcname + '\0' */
-	len = (sizeof("/lxc/lock/") - 1) + strlen(n) + strlen(p) + 3;
+	len = SIZEOF2STRLEN("/lxc/lock/") + strlen(n) + strlen(p) + 3;
 
 	rundir = get_rundir();
 	if (!rundir)
diff --git a/src/lxc/storage/overlay.c b/src/lxc/storage/overlay.c
index a426a0219..a66df6bba 100644
--- a/src/lxc/storage/overlay.c
+++ b/src/lxc/storage/overlay.c
@@ -31,6 +31,7 @@
 #include "confile.h"
 #include "log.h"
 #include "lxccontainer.h"
+#include "macro.h"
 #include "overlay.h"
 #include "rsync.h"
 #include "storage.h"
@@ -94,7 +95,7 @@ int ovl_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char
 			return -22;
 		}
 
-		if (strlen(lastslash) < (sizeof("/rootfs") - 1)) {
+		if (strlen(lastslash) < SIZEOF2STRLEN("/rootfs")) {
 			ERROR("Failed to detect \"/rootfs\" in string \"%s\"",
 			      new->dest);
 			return -22;
@@ -110,8 +111,8 @@ int ovl_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char
 		}
 
 		memcpy(delta, new->dest, lastslashidx + 1);
-		memcpy(delta + lastslashidx, "delta0", sizeof("delta0") - 1);
-		delta[lastslashidx + sizeof("delta0") - 1] = '\0';
+		memcpy(delta + lastslashidx, "delta0", SIZEOF2STRLEN("delta0"));
+		delta[lastslashidx + SIZEOF2STRLEN("delta0")] = '\0';
 
 		ret = mkdir(delta, 0755);
 		if (ret < 0 && errno != EEXIST) {
@@ -142,8 +143,8 @@ int ovl_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char
 		}
 
 		memcpy(work, new->dest, lastslashidx + 1);
-		memcpy(work + lastslashidx, "olwork", sizeof("olwork") - 1);
-		work[lastslashidx + sizeof("olwork") - 1] = '\0';
+		memcpy(work + lastslashidx, "olwork", SIZEOF2STRLEN("olwork"));
+		work[lastslashidx + SIZEOF2STRLEN("olwork")] = '\0';
 
 		ret = mkdir(work, 0755);
 		if (ret < 0) {
@@ -253,8 +254,8 @@ int ovl_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char
 		}
 
 		memcpy(work, ndelta, lastslashidx + 1);
-		memcpy(work + lastslashidx, "olwork", sizeof("olwork") - 1);
-		work[lastslashidx + sizeof("olwork") - 1] = '\0';
+		memcpy(work + lastslashidx, "olwork", SIZEOF2STRLEN("olwork"));
+		work[lastslashidx + SIZEOF2STRLEN("olwork")] = '\0';
 
 		ret = mkdir(work, 0755);
 		if (ret < 0 && errno != EEXIST) {
@@ -335,11 +336,11 @@ int ovl_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char
 			return -1;
 		}
 
-		if (!strncmp(s1, "/snaps", sizeof("/snaps") - 1)) {
+		if (!strncmp(s1, "/snaps", SIZEOF2STRLEN("/snaps"))) {
 			s1 = clean_new_path;
 			s2 = clean_old_path;
 			s3 = (char *)cname;
-		} else if (!strncmp(s2, "/snaps", sizeof("/snaps") - 1)) {
+		} else if (!strncmp(s2, "/snaps", SIZEOF2STRLEN("/snaps"))) {
 			s1 = clean_old_path;
 			s2 = clean_new_path;
 			s3 = (char *)oldname;
@@ -415,7 +416,7 @@ int ovl_create(struct lxc_storage *bdev, const char *dest, const char *n,
 		ERROR("Failed to allocate memory");
 		return -1;
 	}
-	memcpy(delta + len - 6, "delta0", sizeof("delta0") - 1);
+	memcpy(delta + len - 6, "delta0", SIZEOF2STRLEN("delta0"));
 
 	ret = mkdir_p(delta, 0755);
 	if (ret < 0) {
@@ -568,8 +569,8 @@ int ovl_mount(struct lxc_storage *bdev)
 	}
 
 	memcpy(work, upper, lastslashidx + 1);
-	memcpy(work + lastslashidx, "olwork", sizeof("olwork") - 1);
-	work[lastslashidx + sizeof("olwork") - 1] = '\0';
+	memcpy(work + lastslashidx, "olwork", SIZEOF2STRLEN("olwork"));
+	work[lastslashidx + SIZEOF2STRLEN("olwork")] = '\0';
 
 	ret = parse_mntopts(bdev->mntopts, &mntflags, &mntdata);
 	if (ret < 0) {


More information about the lxc-devel mailing list