[prev in list] [next in list] [prev in thread] [next in thread]
List: lxc-devel
Subject: [lxc-devel] [lxc/master] tree-wide: use sizeof on static arrays
From: Blub on Github <lxc-bot () linuxcontainers ! org>
Date: 2018-08-29 19:52:16
Message-ID: 20180829195216.A017F5A5DC () mailman01 ! srv ! dcmtl ! stgraber ! net
[Download RAW message or body]
[Attachment #2 (text/x-mailbox)]
The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxc/pull/2577
This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.
=== Description (from pull-request) ===
I think this way it's easier with future changes, unless someone changes an array to \
a pointer, but then all uses must be checked anyway, and we already have both \
versions in the code.
[Attachment #3 (text/plain)]
From 81a3bb64b4147ac6da3087cb77ac67828a2f2b76 Mon Sep 17 00:00:00 2001
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
Date: Wed, 29 Aug 2018 21:45:45 +0200
Subject: [PATCH] tree-wide: use sizeof on static arrays
Instead of duplicating their lengths in read/snprintf/...
calls.
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
src/lxc/caps.c | 2 +-
src/lxc/cgroups/cgfsng.c | 4 ++--
src/lxc/log.c | 4 ++--
src/lxc/lxccontainer.c | 4 ++--
src/lxc/monitor.c | 4 ++--
src/lxc/tools/lxc_monitor.c | 4 ++--
src/lxc/utils.c | 4 ++--
src/tests/lxc-test-utils.c | 24 ++++++++++++------------
8 files changed, 25 insertions(+), 25 deletions(-)
diff --git a/src/lxc/caps.c b/src/lxc/caps.c
index c56fe732e..8ca47bf66 100644
--- a/src/lxc/caps.c
+++ b/src/lxc/caps.c
@@ -299,7 +299,7 @@ static long int _real_caps_last_cap(void)
char buf[INTTYPE_TO_STRLEN(int)];
again:
- n = read(fd, buf, INTTYPE_TO_STRLEN(int));
+ n = read(fd, buf, sizeof(buf));
if (n < 0 && errno == EINTR) {
goto again;
} else if (n >= 0) {
diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
index 82eb46a3b..1fe561498 100644
--- a/src/lxc/cgroups/cgfsng.c
+++ b/src/lxc/cgroups/cgfsng.c
@@ -321,8 +321,8 @@ static char *lxc_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits)
if (!is_set(i, bitarr))
continue;
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(size_t), "%zu", i);
- if (ret < 0 || (size_t)ret >= INTTYPE_TO_STRLEN(size_t)) {
+ ret = snprintf(numstr, sizeof(numstr), "%zu", i);
+ if (ret < 0 || (size_t)ret >= sizeof(numstr)) {
lxc_free_array((void **)cpulist, free);
return NULL;
}
diff --git a/src/lxc/log.c b/src/lxc/log.c
index 38f7c889c..2a7ee1eab 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -247,8 +247,8 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
seconds = (time->tv_sec - d_in_s - h_in_s - (minutes * 60));
/* Make string from nanoseconds. */
- ret = snprintf(nanosec, INTTYPE_TO_STRLEN(int64_t), "%"PRId64, (int64_t)time->tv_nsec);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
+ ret = snprintf(nanosec, sizeof(nanosec), "%"PRId64, (int64_t)time->tv_nsec);
+ if (ret < 0 || ret >= sizeof(nanosec))
return -1;
/* Create final timestamp for the log and shorten nanoseconds to 3
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 002c4ec70..3ea864bbc 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1039,8 +1039,8 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
int ret, w;
char pidstr[INTTYPE_TO_STRLEN(int)];
- w = snprintf(pidstr, INTTYPE_TO_STRLEN(int), "%d", (int)lxc_raw_getpid());
- if (w < 0 || (size_t)w >= INTTYPE_TO_STRLEN(int)) {
+ w = snprintf(pidstr, sizeof(pidstr), "%d", (int)lxc_raw_getpid());
+ if (w < 0 || (size_t)w >= sizeof(pidstr)) {
free_init_cmd(init_cmd);
lxc_free_handler(handler);
diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c
index cdf15b659..4cca9002c 100644
--- a/src/lxc/monitor.c
+++ b/src/lxc/monitor.c
@@ -371,8 +371,8 @@ int lxc_monitord_spawn(const char *lxcpath)
close(pipefd[0]);
- ret = snprintf(pipefd_str, INTTYPE_TO_STRLEN(int), "%d", pipefd[1]);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int)) {
+ ret = snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
+ if (ret < 0 || ret >= sizeof(pipefd_str)) {
ERROR("Failed to create pid argument to pass to monitord.");
_exit(EXIT_FAILURE);
}
diff --git a/src/lxc/tools/lxc_monitor.c b/src/lxc/tools/lxc_monitor.c
index 5e296082e..86a77c3af 100644
--- a/src/lxc/tools/lxc_monitor.c
+++ b/src/lxc/tools/lxc_monitor.c
@@ -224,8 +224,8 @@ static int lxc_tool_monitord_spawn(const char *lxcpath)
close(pipefd[0]);
- ret = snprintf(pipefd_str, INTTYPE_TO_STRLEN(int), "%d", pipefd[1]);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int)) {
+ ret = snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
+ if (ret < 0 || ret >= sizeof(pipefd_str)) {
ERROR("Failed to create pid argument to pass to monitord");
_exit(EXIT_FAILURE);
}
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 1f0ba8971..77ad76b49 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -1165,7 +1165,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
return -1;
}
- linklen = readlink(path, link, INTTYPE_TO_STRLEN(pid_t));
+ linklen = readlink(path, link, sizeof(link));
ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs);
if (ret < 0 || ret >= MAXPATHLEN) {
@@ -1179,7 +1179,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
return -1;
goto domount;
- } else if (linklen >= INTTYPE_TO_STRLEN(pid_t)) {
+ } else if (linklen >= sizeof(link)) {
link[linklen - 1] = '\0';
ERROR("readlink returned truncated content: \"%s\"", link);
return -1;
diff --git a/src/tests/lxc-test-utils.c b/src/tests/lxc-test-utils.c
index 20f71b9a5..d0c07dc59 100644
--- a/src/tests/lxc-test-utils.c
+++ b/src/tests/lxc-test-utils.c
@@ -252,14 +252,14 @@ void test_lxc_safe_uint(void)
lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" -123", &n)));
lxc_test_assert_abort((-EINVAL == lxc_safe_uint("-123", &n)));
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)UINT_MAX);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
+ ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX);
+ if (ret < 0 || ret >= sizeof(numstr))
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_uint(numstr, &n)) && n == UINT_MAX);
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)UINT_MAX + 1);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
+ ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX + 1);
+ if (ret < 0 || ret >= sizeof(numstr))
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_uint(numstr, &n)));
@@ -285,26 +285,26 @@ void test_lxc_safe_int(void)
signed int n;
char numstr[INTTYPE_TO_STRLEN(uint64_t)];
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)INT_MAX);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
+ ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX);
+ if (ret < 0 || ret >= sizeof(numstr))
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MAX);
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)INT_MAX + 1);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
+ ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX + 1);
+ if (ret < 0 || ret >= sizeof(numstr))
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(int64_t), "%" PRId64, (int64_t)INT_MIN);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
+ ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN);
+ if (ret < 0 || ret >= sizeof(numstr))
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MIN);
- ret = snprintf(numstr, INTTYPE_TO_STRLEN(int64_t), "%" PRId64, (int64_t)INT_MIN - 1);
- if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
+ ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN - 1);
+ if (ret < 0 || ret >= sizeof(numstr))
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
[Attachment #4 (text/plain)]
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic