[PATCH] selftests: Removing deprecated strncpy()
From: Kevin Paul Reddy Janagari
Date: Fri Apr 11 2025 - 09:57:05 EST
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
Signed-off-by: Kevin Paul Reddy Janagari <kevinpaul468@xxxxxxxxx>
---
tools/testing/selftests/sync/sync.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/sync/sync.c b/tools/testing/selftests/sync/sync.c
index 7741c0518d18..4b284f517433 100644
--- a/tools/testing/selftests/sync/sync.c
+++ b/tools/testing/selftests/sync/sync.c
@@ -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);
--
2.39.5