[CRIU] [PATCH 1/2] zdtm: fix gcc-8 warnings

Andrei Vagin avagin at openvz.org
Sun Feb 4 08:22:58 MSK 2018


From: Andrei Vagin <avagin at virtuozzo.com>

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: Andrei Vagin <avagin at virtuozzo.com>
---
 test/zdtm/lib/fs.c                              |  3 ++-
 test/zdtm/lib/zdtmtst.h                         | 10 ++++++++++
 test/zdtm/static/binfmt_misc.c                  |  2 +-
 test/zdtm/static/cgroup01.c                     |  3 ++-
 test/zdtm/static/cgroup02.c                     |  8 ++++----
 test/zdtm/static/cgroup03.c                     |  4 ++--
 test/zdtm/static/cgroup04.c                     |  8 ++++----
 test/zdtm/static/cgroup_stray.c                 |  4 ++--
 test/zdtm/static/cgroupns.c                     | 10 +++++-----
 test/zdtm/static/del_standalone_un.c            |  2 +-
 test/zdtm/static/mntns_overmount.c              |  4 ++--
 test/zdtm/static/mntns_root_bind.c              | 16 ++++++++--------
 test/zdtm/static/mount_paths.c                  |  6 +++---
 test/zdtm/static/overmount_with_shared_parent.c | 10 +++++-----
 test/zdtm/static/shared_mount_propagation.c     | 21 ++++++++++-----------
 test/zdtm/static/sk-unix-rel.c                  |  7 +++++--
 test/zdtm/static/sk-unix-unconn.c               |  6 +++++-
 test/zdtm/static/sockets00.c                    |  7 +++++--
 test/zdtm/static/sockets03.c                    |  7 +++++--
 test/zdtm/static/tempfs.c                       |  6 +++---
 20 files changed, 84 insertions(+), 60 deletions(-)

diff --git a/test/zdtm/lib/fs.c b/test/zdtm/lib/fs.c
index 37f5e4191..0decfc37b 100644
--- a/test/zdtm/lib/fs.c
+++ b/test/zdtm/lib/fs.c
@@ -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;
 			}
 		}
 
diff --git a/test/zdtm/lib/zdtmtst.h b/test/zdtm/lib/zdtmtst.h
index cd8e9592e..dbe825cbc 100644
--- a/test/zdtm/lib/zdtmtst.h
+++ b/test/zdtm/lib/zdtmtst.h
@@ -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_ */
diff --git a/test/zdtm/static/binfmt_misc.c b/test/zdtm/static/binfmt_misc.c
index 26f8b8192..316c4b667 100644
--- a/test/zdtm/static/binfmt_misc.c
+++ b/test/zdtm/static/binfmt_misc.c
@@ -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);
diff --git a/test/zdtm/static/cgroup01.c b/test/zdtm/static/cgroup01.c
index 384f86053..cf54c1d9e 100644
--- a/test/zdtm/static/cgroup01.c
+++ b/test/zdtm/static/cgroup01.c
@@ -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;
diff --git a/test/zdtm/static/cgroup02.c b/test/zdtm/static/cgroup02.c
index ac6e9c0fd..6de22226f 100644
--- a/test/zdtm/static/cgroup02.c
+++ b/test/zdtm/static/cgroup02.c
@@ -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;
 	}
diff --git a/test/zdtm/static/cgroup03.c b/test/zdtm/static/cgroup03.c
index c6c4938ba..0b5db2345 100644
--- a/test/zdtm/static/cgroup03.c
+++ b/test/zdtm/static/cgroup03.c
@@ -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) {
diff --git a/test/zdtm/static/cgroup04.c b/test/zdtm/static/cgroup04.c
index 6998358ef..8ec0cff37 100644
--- a/test/zdtm/static/cgroup04.c
+++ b/test/zdtm/static/cgroup04.c
@@ -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;
diff --git a/test/zdtm/static/cgroup_stray.c b/test/zdtm/static/cgroup_stray.c
index 235434ed2..16db9ba54 100644
--- a/test/zdtm/static/cgroup_stray.c
+++ b/test/zdtm/static/cgroup_stray.c
@@ -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) {
diff --git a/test/zdtm/static/cgroupns.c b/test/zdtm/static/cgroupns.c
index cee704353..7bb0b1fda 100644
--- a/test/zdtm/static/cgroupns.c
+++ b/test/zdtm/static/cgroupns.c
@@ -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) {
diff --git a/test/zdtm/static/del_standalone_un.c b/test/zdtm/static/del_standalone_un.c
index f62edb2ed..d8200068b 100644
--- a/test/zdtm/static/del_standalone_un.c
+++ b/test/zdtm/static/del_standalone_un.c
@@ -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;
 }
 
diff --git a/test/zdtm/static/mntns_overmount.c b/test/zdtm/static/mntns_overmount.c
index aa297a055..ba23afca8 100644
--- a/test/zdtm/static/mntns_overmount.c
+++ b/test/zdtm/static/mntns_overmount.c
@@ -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");
diff --git a/test/zdtm/static/mntns_root_bind.c b/test/zdtm/static/mntns_root_bind.c
index 3f15a2726..978143ec2 100644
--- a/test/zdtm/static/mntns_root_bind.c
+++ b/test/zdtm/static/mntns_root_bind.c
@@ -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) ||
diff --git a/test/zdtm/static/mount_paths.c b/test/zdtm/static/mount_paths.c
index a42b0eb3d..ee05adb46 100644
--- a/test/zdtm/static/mount_paths.c
+++ b/test/zdtm/static/mount_paths.c
@@ -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;
diff --git a/test/zdtm/static/overmount_with_shared_parent.c b/test/zdtm/static/overmount_with_shared_parent.c
index 8261fb969..1fcb5a5cb 100644
--- a/test/zdtm/static/overmount_with_shared_parent.c
+++ b/test/zdtm/static/overmount_with_shared_parent.c
@@ -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);
diff --git a/test/zdtm/static/shared_mount_propagation.c b/test/zdtm/static/shared_mount_propagation.c
index 9fe58487f..4e81b9ec6 100644
--- a/test/zdtm/static/shared_mount_propagation.c
+++ b/test/zdtm/static/shared_mount_propagation.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);
diff --git a/test/zdtm/static/sk-unix-rel.c b/test/zdtm/static/sk-unix-rel.c
index 4f0b186d1..5b3e85289 100644
--- a/test/zdtm/static/sk-unix-rel.c
+++ b/test/zdtm/static/sk-unix-rel.c
@@ -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);
diff --git a/test/zdtm/static/sk-unix-unconn.c b/test/zdtm/static/sk-unix-unconn.c
index abf59e39b..72d1348a8 100644
--- a/test/zdtm/static/sk-unix-unconn.c
+++ b/test/zdtm/static/sk-unix-unconn.c
@@ -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;
diff --git a/test/zdtm/static/sockets00.c b/test/zdtm/static/sockets00.c
index ab5d2e450..41c64c717 100644
--- a/test/zdtm/static/sockets00.c
+++ b/test/zdtm/static/sockets00.c
@@ -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);
diff --git a/test/zdtm/static/sockets03.c b/test/zdtm/static/sockets03.c
index f62f565c0..e4c647d4c 100644
--- a/test/zdtm/static/sockets03.c
+++ b/test/zdtm/static/sockets03.c
@@ -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);
diff --git a/test/zdtm/static/tempfs.c b/test/zdtm/static/tempfs.c
index 66c51a5eb..8a103be6d 100644
--- a/test/zdtm/static/tempfs.c
+++ b/test/zdtm/static/tempfs.c
@@ -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");
-- 
2.13.6



More information about the CRIU mailing list