Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] string.c: remove use of strcat() after strlen()



---
 Src/string.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/Src/string.c b/Src/string.c
index 5f439926e..99e31d6c3 100644
--- a/Src/string.c
+++ b/Src/string.c
@@ -28,6 +28,8 @@
 
 #include "zsh.mdh"
 
+#define MEMPCPY(dst, src, n) ((char *)memcpy(dst, src, n) + n)
+
 /**/
 mod_export char *
 dupstring(const char *s)
@@ -200,7 +202,10 @@ ztrduppfx(const char *s, int len)
 mod_export char *
 appstr(char *base, char const *append)
 {
-    return strcat(realloc(base, strlen(base) + strlen(append) + 1), append);
+    const size_t base_len = strlen(base);
+    const size_t append_len = strlen(append);
+    *(append = (char *)MEMPCPY((char *)realloc(base, base_len + append_len + 1) + base_len, append, append_len)) = '\0';
+    return append;
 }
 
 /* Return a pointer to the last character of a string,
-- 
2.43.0





Messages sorted by: Reverse Date, Date, Thread, Author