Commit 7f5e8649 authored by Andrei Vagin's avatar Andrei Vagin

zdtm: fix gcc-8 warnings

fs.c:78:5: error: 'strncpy' specified bound 64 equals destination size [-Werror=stringop-truncation]
     strncpy(m->fsname, fsname, sizeof(m->fsname));
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 7ca30056
......@@ -75,7 +75,8 @@ mnt_info_t *get_cwd_mnt_info(void)
strncpy(m->root, root, sizeof(m->root));
strncpy(m->mountpoint, mountpoint, sizeof(m->mountpoint));
strncpy(m->fsname, fsname, sizeof(m->fsname));
strncpy(m->fsname, fsname, sizeof(m->fsname) - 1);
m->fsname[sizeof(m->fsname) - 1] = 0;
}
}
......
......@@ -4,6 +4,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#define INPROGRESS ".inprogress"
......@@ -154,4 +155,13 @@ extern int tcp_init_server_with_opts(int family, int *port, struct zdtm_tcp_opts
extern pid_t sys_clone_unified(unsigned long flags, void *child_stack, void *parent_tid,
void *child_tid, unsigned long newtls);
#define ssprintf(s, fmt, ...) ({ \
int ___ret; \
\
___ret = snprintf(s, sizeof(s), fmt, ##__VA_ARGS__); \
if (___ret >= sizeof(s)) \
abort(); \
___ret; \
})
#endif /* _VIMITESU_H_ */
......@@ -143,7 +143,7 @@ int main(int argc, char **argv)
close(fd);
/* Disable one of the entries */
sprintf(path, "%s/%s", dirname, NAME[0]);
ssprintf(path, "%s/%s", dirname, NAME[0]);
fd = open(path, O_WRONLY);
if (fd < 0 || write(fd, "0", 1) != 1) {
fail("Can't disable %s\n", path);
......
......@@ -83,7 +83,8 @@ int main(int argc, char **argv)
test_msg("found cgroup at %s\n", aux);
for (i = 0; i < 2; i++) {
sprintf(paux, "%s/%s/%s.%d", aux, subname, empty, i);
ssprintf(paux, "%s/%s/%s.%d", aux, subname, empty, i);
if (stat(paux, &st)) {
fail("couldn't stat %s\n", paux);
ret = -1;
......
......@@ -39,14 +39,14 @@ int mount_and_add(const char *controller, const char *prefix, const char *path)
goto err_rd;
}
sprintf(paux, "%s/%s", subdir, prefix);
ssprintf(paux, "%s/%s", subdir, prefix);
mkdir(paux, 0600);
sprintf(paux, "%s/%s/%s", subdir, prefix, path);
ssprintf(paux, "%s/%s/%s", subdir, prefix, path);
mkdir(paux, 0600);
l = sprintf(aux, "%d", getpid());
sprintf(paux, "%s/%s/%s/tasks", subdir, prefix, path);
ssprintf(paux, "%s/%s/%s/tasks", subdir, prefix, path);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
......@@ -78,7 +78,7 @@ bool test_exists(char *mountinfo_line, char *path)
sscanf(mountinfo_line, "%*d %*d %*d:%*d %*s %s", aux);
test_msg("found cgroup at %s\n", aux);
sprintf(paux, "%s/%s", aux, path);
ssprintf(paux, "%s/%s", aux, path);
if (stat(paux, &st)) {
return false;
}
......
......@@ -38,11 +38,11 @@ int mount_and_add(const char *controller, const char *path)
goto err_rd;
}
sprintf(paux, "%s/%s", subdir, path);
ssprintf(paux, "%s/%s", subdir, path);
mkdir(paux, 0600);
l = sprintf(aux, "%d", getpid());
sprintf(paux, "%s/%s/tasks", subdir, path);
ssprintf(paux, "%s/%s/tasks", subdir, path);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
......
......@@ -59,19 +59,19 @@ int mount_and_add(const char *controller, const char *path, const char *prop, co
goto err_rd;
}
sprintf(paux, "%s/%s", subdir, path);
ssprintf(paux, "%s/%s", subdir, path);
mkdir(paux, 0600);
sprintf(paux, "%s/%s/%s", subdir, path, prop);
ssprintf(paux, "%s/%s/%s", subdir, path, prop);
if (write_value(paux, value) < 0)
goto err_rs;
sprintf(aux, "%d", getpid());
sprintf(paux, "%s/%s/tasks", subdir, path);
ssprintf(paux, "%s/%s/tasks", subdir, path);
if (write_value(paux, aux) < 0)
goto err_rs;
sprintf(paux, "%s/%s/special_prop_check", subdir, path);
ssprintf(paux, "%s/%s/special_prop_check", subdir, path);
mkdir(paux, 0600);
return 0;
......
......@@ -53,11 +53,11 @@ static int add_to_cg(const char *controller, const char *path)
int cgfd, l;
sprintf(subdir, "%s/%s", dirname, controller);
sprintf(paux, "%s/%s", subdir, path);
ssprintf(paux, "%s/%s", subdir, path);
mkdir(paux, 0600);
l = sprintf(aux, "%d", getpid());
sprintf(paux, "%s/%s/tasks", subdir, path);
ssprintf(paux, "%s/%s/tasks", subdir, path);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
......
......@@ -34,23 +34,23 @@ int mount_and_add(const char *controller, const char *path)
return -1;
}
sprintf(subdir, "%s/%s", dirname, controller);
ssprintf(subdir, "%s/%s", dirname, controller);
if (mkdir(subdir, 0700) < 0) {
pr_perror("Can't make dir");
return -1;
}
sprintf(aux, "none,name=%s", controller);
ssprintf(aux, "none,name=%s", controller);
if (mount("none", subdir, "cgroup", 0, aux)) {
pr_perror("Can't mount cgroups");
goto err_rd;
}
sprintf(paux, "%s/%s", subdir, path);
ssprintf(paux, "%s/%s", subdir, path);
mkdir(paux, 0600);
l = sprintf(aux, "%d", getpid());
sprintf(paux, "%s/%s/tasks", subdir, path);
l = ssprintf(aux, "%d", getpid());
ssprintf(paux, "%s/%s/tasks", subdir, path);
cgfd = open(paux, O_WRONLY);
if (cgfd < 0) {
......
......@@ -25,7 +25,7 @@ static int fill_sock_name(struct sockaddr_un *name, const char *filename)
return -1;
name->sun_family = AF_LOCAL;
sprintf(name->sun_path, "%s/%s", cwd, filename);
ssprintf(name->sun_path, "%s/%s", cwd, filename);
return 0;
}
......
......@@ -42,8 +42,8 @@ int main(int argc, char **argv)
return 1;
}
snprintf(f1, sizeof(f1), "%s/devices", d1);
snprintf(f2, sizeof(f2), "%s/devices", d2);
ssprintf(f1, "%s/devices", d1);
ssprintf(f2, "%s/devices", d2);
if (mount("zdtm_d1", d1, "tmpfs", 0, NULL)) {
pr_perror("mount");
......
......@@ -38,14 +38,14 @@ int main(int argc, char **argv)
mount(NULL, "/", NULL, MS_SHARED, NULL);
snprintf(subdir1, sizeof(subdir1), "%s/subdir1", dirname);
snprintf(path, sizeof(path), "%s/test", subdir1);
snprintf(bpath, sizeof(bpath), "%s/test.bind", subdir1);
snprintf(spath, sizeof(spath), "%s/test/sub", subdir1);
snprintf(bspath, sizeof(bspath), "%s/test.bind/sub", subdir1);
snprintf(subdir2, sizeof(subdir2), "%s/subdir2", dirname);
snprintf(bsubdir2, sizeof(bsubdir2), "%s/bsubdir2", dirname);
ssprintf(subdir1, "%s/subdir1", dirname);
ssprintf(path, "%s/test", subdir1);
ssprintf(bpath, "%s/test.bind", subdir1);
ssprintf(spath, "%s/test/sub", subdir1);
ssprintf(bspath, "%s/test.bind/sub", subdir1);
ssprintf(subdir2, "%s/subdir2", dirname);
ssprintf(bsubdir2, "%s/bsubdir2", dirname);
if (mkdir(dirname, 0700) ||
mkdir(subdir1, 0777) ||
......
......@@ -19,13 +19,13 @@ TEST_OPTION(dirname, string, "directory name", 1);
int main(int argc, char **argv)
{
int ret = 1;
char buf[1024], test_dir[PATH_MAX], fname[PATH_MAX];
char test_dir[PATH_MAX], fname[PATH_MAX];
test_init(argc, argv);
mkdir(dirname, 0700);
snprintf(test_dir, sizeof(test_dir), "%s/%s", dirname, TEST_DIR);
ssprintf(test_dir, "%s/%s", dirname, TEST_DIR);
mkdir(test_dir, 0700);
if (mount("", test_dir, "tmpfs", 0, NULL)) {
......@@ -33,7 +33,7 @@ int main(int argc, char **argv)
return 1;
}
snprintf(fname, sizeof(buf), "%s/\\\t \\\\ \\tt", test_dir);
ssprintf(fname, "%s/\\\t \\\\ \\tt", test_dir);
if (mkdir(fname, 0700)) {
pr_perror("mkdir");
return 1;
......
......@@ -29,13 +29,13 @@ int main(int argc, char **argv)
return 1;
}
snprintf(dir_a, sizeof(dir_a), "%s/a", dirname);
snprintf(dir_d, sizeof(dir_d), "%s/d", dirname);
ssprintf(dir_a, "%s/a", dirname);
ssprintf(dir_d, "%s/d", dirname);
mkdir(dir_a, 0700);
mkdir(dir_d, 0700);
snprintf(dir_b, sizeof(dir_a), "%s/b", dir_a);
snprintf(dir_c, sizeof(dir_c), "%s/c", dir_b);
ssprintf(dir_b, "%s/b", dir_a);
ssprintf(dir_c, "%s/c", dir_b);
mkdir(dir_b, 0700);
mkdir(dir_c, 0700);
......@@ -44,7 +44,7 @@ int main(int argc, char **argv)
return 1;
}
snprintf(dir_a_c, sizeof(dir_a_c), "%s/c", dir_a);
ssprintf(dir_a_c, "%s/c", dir_a);
if (mount(dir_d, dir_a_c, NULL, MS_BIND, NULL)) {
pr_perror("Unable to bind mount %s to %s", dir_d, dir_a_c);
......
......@@ -35,17 +35,17 @@ int main(int argc, char **argv)
return 1;
}
snprintf(dir_a, sizeof(dir_a), "%s/a", dirname);
snprintf(dir_d, sizeof(dir_d), "%s/d", dirname);
snprintf(dir_e, sizeof(dir_e), "%s/e", dirname);
snprintf(dir_f, sizeof(dir_f), "%s/f", dirname);
ssprintf(dir_a, "%s/a", dirname);
ssprintf(dir_d, "%s/d", dirname);
ssprintf(dir_e, "%s/e", dirname);
ssprintf(dir_f, "%s/f", dirname);
mkdir(dir_a, 0700);
mkdir(dir_d, 0700);
mkdir(dir_e, 0700);
mkdir(dir_f, 0700);
snprintf(dir_b, sizeof(dir_a), "%s/b", dir_a);
snprintf(dir_c, sizeof(dir_c), "%s/c", dir_b);
ssprintf(dir_b, "%s/b", dir_a);
ssprintf(dir_c, "%s/c", dir_b);
mkdir(dir_b, 0700);
mkdir(dir_c, 0700);
......@@ -64,7 +64,7 @@ int main(int argc, char **argv)
return 1;
}
snprintf(test_file, sizeof(test_file), "%s/file", dir_f);
ssprintf(test_file, "%s/file", dir_f);
fd = open(test_file, O_CREAT | O_WRONLY | O_EXCL, 0600);
if (fd < 0) {
pr_perror("Unable to open %s", test_file);
......@@ -75,10 +75,9 @@ int main(int argc, char **argv)
test_daemon();
test_waitsig();
snprintf(test_bind_file1, sizeof(test_bind_file1), "%s/file", dir_c);
snprintf(test_bind_file2, sizeof(test_bind_file2), "%s/b/c/file", dir_d);
snprintf(test_bind_file3, sizeof(test_bind_file3), "%s/c/file", dir_e);
ssprintf(test_bind_file1, "%s/file", dir_c);
ssprintf(test_bind_file2, "%s/b/c/file", dir_d);
ssprintf(test_bind_file3, "%s/c/file", dir_e);
if (access(test_file, F_OK)) {
pr_perror("%s doesn't exist", test_file);
......
......@@ -48,8 +48,11 @@ int main(int argc, char *argv[])
unlink(path);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
addrlen = sizeof(addr.sun_family) + strlen(filename);
addrlen = strlen(filename);
if (addrlen > sizeof(addr.sun_path))
return 1;
memcpy(addr.sun_path, filename, addrlen);
addrlen += sizeof(addr.sun_family);
sock[0] = socket(AF_UNIX, SOCK_STREAM, 0);
sock[1] = socket(AF_UNIX, SOCK_STREAM, 0);
......
......@@ -33,8 +33,12 @@ int main(int argc, char ** argv)
len = snprintf(path, sizeof(path), "X/zdtm-%s-%d/X", argv[0], getpid());
if (len >= sizeof(addr.sun_path)) {
pr_err("%s\n", path);
return 1;
}
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path));
memcpy(addr.sun_path, path, len);
addrlen = sizeof(addr.sun_family) + len;
addr.sun_path[0] = 0;
addr.sun_path[len - 1] = 0;
......
......@@ -52,8 +52,11 @@ int main(int argc, char *argv[])
unlink(path);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path));
addrlen = sizeof(addr.sun_family) + strlen(path);
addrlen = strlen(path);
if (addrlen >= sizeof(addr.sun_path))
return 1;
memcpy(addr.sun_path, path, addrlen);
addrlen += sizeof(addr.sun_family);
ssk_icon[0] = socket(AF_UNIX, SOCK_STREAM, 0);
ssk_icon[1] = socket(AF_UNIX, SOCK_STREAM, 0);
......
......@@ -46,8 +46,11 @@ int main(int argc, char *argv[])
unlink(path);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path));
addrlen = sizeof(addr.sun_family) + strlen(path);
addrlen = strlen(path);
if (addrlen >= sizeof(addr.sun_path))
return 1;
memcpy(addr.sun_path, path, addrlen);
addrlen += sizeof(addr.sun_family);
sk[0] = socket(AF_UNIX, SOCK_STREAM, 0);
sk[1] = socket(AF_UNIX, SOCK_STREAM, 0);
......
......@@ -30,7 +30,7 @@ int main(int argc, char **argv)
return 1;
}
snprintf(fname, sizeof(buf), "%s/test.file", dirname);
ssprintf(fname, "%s/test.file", dirname);
fdo = open(fname, O_RDWR | O_CREAT, 0644);
if (fdo < 0) {
pr_perror("open failed");
......@@ -42,10 +42,10 @@ int main(int argc, char **argv)
goto err;
}
snprintf(overmount, sizeof(buf), "%s/test", dirname);
ssprintf(overmount, "%s/test", dirname);
mkdir(overmount, 0700);
snprintf(fname, sizeof(buf), "%s/test.file", overmount);
ssprintf(fname, "%s/test.file", overmount);
fd = open(fname, O_RDWR | O_CREAT, 0644);
if (fd < 0) {
pr_perror("open failed");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment