zsh-workers
 help / color / mirror / code / Atom feed
From: James Tirta Halim <tirtajames45@gmail.com>
To: zsh-workers@zsh.org
Cc: James Tirta Halim <tirtajames45@gmail.com>
Subject: [PATCH v2] string.c: remove use of strcat() after strlen()
Date: Tue, 26 Dec 2023 13:01:59 +0700	[thread overview]
Message-ID: <20231226060159.182340-1-tirtajames45@gmail.com> (raw)

Changes in v2:
return the realloc'd string

---
 Src/string.c | 8 ++++++-
 1 file changed, 7 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);
+    base = realloc(base, base_len + append_len + 1);
+    *(char *)MEMPCPY(base + base_len, append, append_len) = '\0';
+    return base;
 }
 
 /* Return a pointer to the last character of a string,
-- 
2.43.0



             reply	other threads:[~2023-12-26  6:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-26  6:01 James Tirta Halim [this message]
2023-12-31  4:22 ` Bart Schaefer
2023-12-31  5:10   ` James
2023-12-31  5:20     ` Bart Schaefer
2024-01-28  0:57     ` Oliver Kiddle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231226060159.182340-1-tirtajames45@gmail.com \
    --to=tirtajames45@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).