Skip to content

Commit

Permalink
Revert "tree-wide: use sizeof on static arrays"
Browse files Browse the repository at this point in the history
This reverts commit 81a3bb6.

This commit broke all builders running with pid_max > 32768.

Reverting for now so we can bring the build farm back online.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
  • Loading branch information
stgraber authored and Christian Brauner committed Sep 30, 2018
1 parent 35bd844 commit d3e9119
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/lxc/caps.c
Expand Up @@ -299,7 +299,7 @@ static long int _real_caps_last_cap(void)
char buf[INTTYPE_TO_STRLEN(int)];

again:
n = read(fd, buf, sizeof(buf));
n = read(fd, buf, INTTYPE_TO_STRLEN(int));
if (n < 0 && errno == EINTR) {
goto again;
} else if (n >= 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/lxc/cgroups/cgfsng.c
Expand Up @@ -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, sizeof(numstr), "%zu", i);
if (ret < 0 || (size_t)ret >= sizeof(numstr)) {
ret = snprintf(numstr, INTTYPE_TO_STRLEN(size_t), "%zu", i);
if (ret < 0 || (size_t)ret >= INTTYPE_TO_STRLEN(size_t)) {
lxc_free_array((void **)cpulist, free);
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lxc/log.c
Expand Up @@ -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, sizeof(nanosec), "%"PRId64, (int64_t)time->tv_nsec);
if (ret < 0 || ret >= sizeof(nanosec))
ret = snprintf(nanosec, INTTYPE_TO_STRLEN(int64_t), "%"PRId64, (int64_t)time->tv_nsec);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
return -1;

/* Create final timestamp for the log and shorten nanoseconds to 3
Expand Down
4 changes: 2 additions & 2 deletions src/lxc/lxccontainer.c
Expand Up @@ -1018,8 +1018,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, sizeof(pidstr), "%d", (int)lxc_raw_getpid());
if (w < 0 || (size_t)w >= sizeof(pidstr)) {
w = snprintf(pidstr, INTTYPE_TO_STRLEN(int), "%d", (int)lxc_raw_getpid());
if (w < 0 || (size_t)w >= INTTYPE_TO_STRLEN(int)) {
free_init_cmd(init_cmd);
lxc_free_handler(handler);

Expand Down
4 changes: 2 additions & 2 deletions src/lxc/monitor.c
Expand Up @@ -371,8 +371,8 @@ int lxc_monitord_spawn(const char *lxcpath)

close(pipefd[0]);

ret = snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
if (ret < 0 || ret >= sizeof(pipefd_str)) {
ret = snprintf(pipefd_str, INTTYPE_TO_STRLEN(int), "%d", pipefd[1]);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int)) {
ERROR("Failed to create pid argument to pass to monitord.");
_exit(EXIT_FAILURE);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lxc/tools/lxc_monitor.c
Expand Up @@ -224,8 +224,8 @@ static int lxc_tool_monitord_spawn(const char *lxcpath)

close(pipefd[0]);

ret = snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
if (ret < 0 || ret >= sizeof(pipefd_str)) {
ret = snprintf(pipefd_str, INTTYPE_TO_STRLEN(int), "%d", pipefd[1]);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int)) {
ERROR("Failed to create pid argument to pass to monitord");
_exit(EXIT_FAILURE);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lxc/utils.c
Expand Up @@ -1158,7 +1158,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
return -1;
}

linklen = readlink(path, link, sizeof(link));
linklen = readlink(path, link, INTTYPE_TO_STRLEN(pid_t));

ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs);
if (ret < 0 || ret >= MAXPATHLEN) {
Expand All @@ -1172,7 +1172,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
return -1;

goto domount;
} else if (linklen >= sizeof(link)) {
} else if (linklen >= INTTYPE_TO_STRLEN(pid_t)) {
link[linklen - 1] = '\0';
ERROR("readlink returned truncated content: \"%s\"", link);
return -1;
Expand Down
24 changes: 12 additions & 12 deletions src/tests/lxc-test-utils.c
Expand Up @@ -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, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX);
if (ret < 0 || ret >= sizeof(numstr))
ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)UINT_MAX);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
exit(EXIT_FAILURE);

lxc_test_assert_abort((0 == lxc_safe_uint(numstr, &n)) && n == UINT_MAX);

ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX + 1);
if (ret < 0 || ret >= sizeof(numstr))
ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)UINT_MAX + 1);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
exit(EXIT_FAILURE);

lxc_test_assert_abort((-ERANGE == lxc_safe_uint(numstr, &n)));
Expand All @@ -285,26 +285,26 @@ void test_lxc_safe_int(void)
signed int n;
char numstr[INTTYPE_TO_STRLEN(uint64_t)];

ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX);
if (ret < 0 || ret >= sizeof(numstr))
ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)INT_MAX);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
exit(EXIT_FAILURE);

lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MAX);

ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX + 1);
if (ret < 0 || ret >= sizeof(numstr))
ret = snprintf(numstr, INTTYPE_TO_STRLEN(uint64_t), "%" PRIu64, (uint64_t)INT_MAX + 1);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(uint64_t))
exit(EXIT_FAILURE);

lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));

ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN);
if (ret < 0 || ret >= sizeof(numstr))
ret = snprintf(numstr, INTTYPE_TO_STRLEN(int64_t), "%" PRId64, (int64_t)INT_MIN);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
exit(EXIT_FAILURE);

lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MIN);

ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN - 1);
if (ret < 0 || ret >= sizeof(numstr))
ret = snprintf(numstr, INTTYPE_TO_STRLEN(int64_t), "%" PRId64, (int64_t)INT_MIN - 1);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
exit(EXIT_FAILURE);

lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
Expand Down

0 comments on commit d3e9119

Please sign in to comment.