Commit 43277d8f authored by Ihor Solodrai's avatar Ihor Solodrai Committed by Alexei Starovoitov
Browse files

selftests/bpf: Replace strncpy() with strscpy()

strncpy() does not guarantee NULL-termination and is considered
deprecated [1]. Replace strncpy() calls with strscpy().

[1] https://docs.kernel.org/process/deprecated.html#strncpy-on-nul-terminated-strings



Signed-off-by: default avatarIhor Solodrai <ihor.solodrai@linux.dev>
Link: https://lore.kernel.org/r/20260223190736.649171-4-ihor.solodrai@linux.dev


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 6a087ac2
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -581,8 +581,7 @@ int open_tuntap(const char *dev_name, bool need_mac)
		return -1;

	ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN);
	strncpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1);
	ifr.ifr_name[IFNAMSIZ - 1] = '\0';
	strscpy(ifr.ifr_name, dev_name);

	err = ioctl(fd, TUNSETIFF, &ifr);
	if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) {
+1 −2
Original line number Diff line number Diff line
@@ -346,8 +346,7 @@ static void test_task_sleepable(void)
		close(finish_pipe[1]);

		test_data = malloc(sizeof(char) * 10);
		strncpy(test_data, "test_data", 10);
		test_data[9] = '\0';
		strscpy(test_data, "test_data", 10);

		test_data_long = malloc(sizeof(char) * 5000);
		for (int i = 0; i < 5000; ++i) {
+2 −2
Original line number Diff line number Diff line
@@ -570,7 +570,7 @@ static int create_tap(const char *ifname)
	};
	int fd, ret;

	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	strscpy(ifr.ifr_name, ifname);

	fd = open("/dev/net/tun", O_RDWR);
	if (fd < 0)
@@ -599,7 +599,7 @@ static int ifup(const char *ifname)
	struct ifreq ifr = {};
	int sk, ret;

	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	strscpy(ifr.ifr_name, ifname);

	sk = socket(PF_INET, SOCK_DGRAM, 0);
	if (sk < 0)
+2 −2
Original line number Diff line number Diff line
@@ -28,9 +28,9 @@ static void test_queue_stack_map_by_type(int type)
		vals[i] = rand();

	if (type == QUEUE)
		strncpy(file, "./test_queue_map.bpf.o", sizeof(file));
		strscpy(file, "./test_queue_map.bpf.o");
	else if (type == STACK)
		strncpy(file, "./test_stack_map.bpf.o", sizeof(file));
		strscpy(file, "./test_stack_map.bpf.o");
	else
		return;

+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ void test_skc_to_unix_sock(void)

	memset(&sockaddr, 0, sizeof(sockaddr));
	sockaddr.sun_family = AF_UNIX;
	strncpy(sockaddr.sun_path, sock_path, strlen(sock_path));
	strscpy(sockaddr.sun_path, sock_path);
	sockaddr.sun_path[0] = '\0';

	err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
Loading