Skip to contentSkip to conversation

selftests: Removing deprecated strncpy()

PATCH New Kevin Paul Reddy Janagari 1 participant 1 message Apr 11, 2025 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: Removing deprecated strncpy()

This patch suggests the replacement of strncpy with strscpy
as per Documentation/process/deprecated.
The strncpy() fails to guarantee NULL termination,
The function adds zero pads which isn't really convenient for short strings as it may cause performance issues.

strscpy() is a preferred replacement because
it overcomes the limitations of strncpy mentioned above.

Compile Tested

---
 tools/testing/selftests/sync/sync.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/sync/sync.c +3 -3
index 7741c0518d18..4b284f517433 100644
@@ -29,8 +29,8 @@
 #include <malloc.h>
 #include <poll.h>
 #include <stdint.h>
-#include <string.h>
 #include <unistd.h>
+#include <linux/string.h>
 
 #include <sys/ioctl.h>
 #include <sys/stat.h>
@@ -71,7 +71,7 @@ int sync_merge(const char *name, int fd1, int fd2)
 	int err;
 
 	data.fd2 = fd2;
-	strncpy(data.name, name, sizeof(data.name) - 1);
+	strscpy(data.name, name, sizeof(data.name) - 1);
 	data.name[sizeof(data.name) - 1] = '\0';
 
 	err = ioctl(fd1, SYNC_IOC_MERGE, &data);
@@ -198,7 +198,7 @@ int sw_sync_fence_create(int fd, const char *name, unsigned int value)
 	int err;
 
 	data.value = value;
-	strncpy(data.name, name, sizeof(data.name) - 1);
+	strscpy(data.name, name, sizeof(data.name) - 1);
 	data.name[sizeof(data.name) - 1] = '\0';
 
 	err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
Signature · 1 line
2.39.5