From 904296fbead9abe3369c3e455cd5cbf8c03419eb Mon Sep 17 00:00:00 2001 From: Lee Duncan Date: Wed, 24 Jun 2020 11:09:30 -0700 Subject: [PATCH 2/6] Fix compiler complaint about string truncation. The compiler didn't like having to my_strncat()s defined, so define it in one place, and refer to it elsewhere. The string copying in this is still kind of crazy, but it seems to work, and if the compiler can be happy, so can I. --- cmd/systool.c | 12 +----------- lib/sysfs.h | 12 +----------- lib/sysfs_utils.c | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/cmd/systool.c b/cmd/systool.c index e273e6bf92ec..45ed36db1d54 100644 --- a/cmd/systool.c +++ b/cmd/systool.c @@ -32,17 +32,7 @@ #include "libsysfs.h" #include "names.h" -inline void my_strncpy(char *to, const char *from, size_t max) -{ - size_t i; - - for (i = 0; i < max && from[i] != '\0'; i++) - to[i] = from[i]; - if (i < max) - to[i] = '\0'; - else - to[max-1] = '\0'; -} +extern char *my_strncpy(char *to, const char *from, size_t max); #define safestrcpy(to, from) my_strncpy(to, from, sizeof(to)) #define safestrcpymax(to, from, max) my_strncpy(to, from, max) diff --git a/lib/sysfs.h b/lib/sysfs.h index ec9ba36b6eed..481c1e9b7cf2 100644 --- a/lib/sysfs.h +++ b/lib/sysfs.h @@ -33,17 +33,7 @@ #include #include -inline void my_strncpy(char *to, const char *from, size_t max) -{ - size_t i; - - for (i = 0; i < max && from[i] != '\0'; i++) - to[i] = from[i]; - if (i < max) - to[i] = '\0'; - else - to[max-1] = '\0'; -} +extern char *my_strncpy(char *to, const char *from, size_t max); #define safestrcpy(to, from) my_strncpy(to, from, sizeof(to)) #define safestrcpymax(to, from, max) my_strncpy(to, from, max) diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c index 9ca207c7f1f0..43df36fb972c 100644 --- a/lib/sysfs_utils.c +++ b/lib/sysfs_utils.c @@ -305,3 +305,19 @@ int sysfs_path_is_file(const char *path) return 1; } + +/** + * my_strncpy -- a safe strncpy + */ +char *my_strncpy(char *to, const char *from, size_t max) +{ + size_t i; + + for (i = 0; i < max && from[i] != '\0'; i++) + to[i] = from[i]; + if (i < max) + to[i] = '\0'; + else + to[max-1] = '\0'; + return to; +} -- 2.26.2