[Nasm-commits] [nasm:extnames] nasmlib: add nasm_strappend()

nasm-bot for H. Peter Anvin (Intel) hpa at zytor.com
Thu Jun 4 19:56:46 PDT 2020


Commit-ID:  a227b43b8a20f79d322249e29d24fbb4b1dd9904
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=a227b43b8a20f79d322249e29d24fbb4b1dd9904
Author:     H. Peter Anvin (Intel) <hpa at zytor.com>
AuthorDate: Mon, 25 Jun 2018 22:13:42 -0700
Committer:  H. Peter Anvin (Intel) <hpa at zytor.com>
CommitDate: Mon, 25 Jun 2018 22:13:42 -0700

nasmlib: add nasm_strappend()

nasm_strappend() is similar to nasm_strcat() for the case where the
first string isn't useful and should be freed.  Furthermore, one or
both of the arguments are allowed to be NULL.

Signed-off-by: H. Peter Anvin (Intel) <hpa at zytor.com>


---
 include/nasmlib.h |  1 +
 nasmlib/malloc.c  | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/include/nasmlib.h b/include/nasmlib.h
index e57d0e6d..202c9f03 100644
--- a/include/nasmlib.h
+++ b/include/nasmlib.h
@@ -80,6 +80,7 @@ void nasm_free(void *);
 char * safe_alloc nasm_strdup(const char *);
 char * safe_alloc nasm_strndup(const char *, size_t);
 char * safe_alloc nasm_strcat(const char *one, const char *two);
+char * never_null nasm_strappend(char *one, const char *two);
 char * safe_alloc end_with_null nasm_strcatn(const char *one, ...);
 
 /* Assert the argument is a pointer without evaluating it */
diff --git a/nasmlib/malloc.c b/nasmlib/malloc.c
index ccbc0c75..5903f9a5 100644
--- a/nasmlib/malloc.c
+++ b/nasmlib/malloc.c
@@ -110,6 +110,25 @@ char *nasm_strcat(const char *one, const char *two)
     return rslt;
 }
 
+/* Unlike nasm_strcat() this frees the initial string, which can be NULL */
+char *nasm_strappend(char *one, const char *two)
+{
+    size_t l1, l2;
+
+    if (!two)
+        return one;
+
+    if (!one)
+        return nasm_strdup(two);
+
+    l1 = strlen(one);
+    l2 = strlen(two) + 1;
+    one = nasm_realloc(one, l1+l2);
+    memcpy(one + l1, two, l2);
+
+    return one;
+}
+
 char *nasm_strcatn(const char *str1, ...)
 {
     va_list ap;


More information about the Nasm-commits mailing list