From 0719881cad85f837f039ecb378b823306640902a Mon Sep 17 00:00:00 2001 From: Lee Duncan Date: Thu, 25 Jun 2020 08:05:18 -0700 Subject: [PATCH 3/6] Fix more string issues for gcc-10 These issues only surfaced for 64-bit PPC and for s390, having to do with the home-grown strncat() the code was using, which was just a macro. --- cmd/systool.c | 3 ++- lib/sysfs.h | 3 ++- lib/sysfs_utils.c | 29 +++++++++++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cmd/systool.c b/cmd/systool.c index 45ed36db1d54..f4060f57a6ca 100644 --- a/cmd/systool.c +++ b/cmd/systool.c @@ -36,7 +36,8 @@ 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) -#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1) +extern char *my_strncat(char *to, const char *from, size_t max); +#define safestrcat(to, from) my_strncat(to, from, sizeof(to) - strlen(to) - 1) #define safestrcatmax(to, from, max) \ do { \ diff --git a/lib/sysfs.h b/lib/sysfs.h index 481c1e9b7cf2..a9c14317ec4c 100644 --- a/lib/sysfs.h +++ b/lib/sysfs.h @@ -37,7 +37,8 @@ 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) -#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1) +extern char *my_strncat(char *to, const char *from, size_t max); +#define safestrcat(to, from) my_strncat(to, from, sizeof(to) - strlen(to) - 1) #define safestrcatmax(to, from, max) \ do { \ diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c index 43df36fb972c..bd6f9c1b082b 100644 --- a/lib/sysfs_utils.c +++ b/lib/sysfs_utils.c @@ -152,12 +152,10 @@ int sysfs_get_link(const char *path, char *target, size_t len) else if (*(d+1) == '.') goto parse_path; s = strrchr(temp_path, '/'); - if (s != NULL) { - *(s+1) = '\0'; - safestrcat(temp_path, d); - } else { + if (s != NULL) + safestrcpy(s+1, d); + else safestrcpy(temp_path, d); - } safestrcpymax(target, temp_path, len); break; /* @@ -187,12 +185,10 @@ parse_path: /* relative path from this directory */ safestrcpy(temp_path, devdir); s = strrchr(temp_path, '/'); - if (s != NULL) { - *(s+1) = '\0'; - safestrcat(temp_path, linkpath); - } else { + if (s != NULL) + safestrcpy(s+1, linkpath); + else safestrcpy(temp_path, linkpath); - } safestrcpymax(target, temp_path, len); } return 0; @@ -321,3 +317,16 @@ char *my_strncpy(char *to, const char *from, size_t max) to[max-1] = '\0'; return to; } + +/** + * my_strncpy -- a safe strncpy + */ +char *my_strncat(char *to, const char *from, size_t max) +{ + size_t i = 0; + + while (i < max && to[i] != '\0') + i++; + my_strncpy(to+i, from, max-i); + return to; +} -- 2.26.2