When the source length is already known, it is faster to copy memory
without re-scanning for the length.

* m4/gnulib-cache.m4: Import xmemdup0 module.
* src/m4.h: Include "xmemdup0.h" for all files.
* src/builtin.c (define_user_macro): Use it.
* src/symtab.c (free_symbol, lookup_symbol): Likewise.
---
 m4/gnulib-cache.m4 | 2 ++
 src/builtin.c      | 2 +-
 src/m4.h           | 1 +
 src/symtab.c       | 6 +++---
 4 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/m4/gnulib-cache.m4 b/m4/gnulib-cache.m4
index acebc83f..c26c3516 100644
--- a/m4/gnulib-cache.m4
+++ b/m4/gnulib-cache.m4
@@ -106,6 +106,7 @@
 #  version-etc-fsf \
 #  wait-process \
 #  xalloc \
+#  xmemdup0 \
 #  xoset \
 #  xprintf \
 #  xvasprintf-posix
@@ -175,6 +176,7 @@ gl_MODULES([
   version-etc-fsf
   wait-process
   xalloc
+  xmemdup0
   xoset
   xprintf
   xvasprintf-posix
diff --git a/src/builtin.c b/src/builtin.c
index 907efc9d..2a8c888e 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -291,7 +291,7 @@ define_user_macro (const char *name, int name_len, const 
char *text,
                    size_t text_len, symbol_lookup mode)
 {
   symbol *s;
-  char *defn = xstrdup (text ? text : "");
+  char *defn = xmemdup0 (text ? text : "", text_len);

   if (text_len > INT_MAX)
     {
diff --git a/src/m4.h b/src/m4.h
index 6c2791cd..5a705f08 100644
--- a/src/m4.h
+++ b/src/m4.h
@@ -53,6 +53,7 @@
 #include "unistd--.h"
 #include "verify.h"
 #include "xalloc.h"
+#include "xmemdup0.h"
 #include "xprintf.h"
 #include "xvasprintf.h"

diff --git a/src/symtab.c b/src/symtab.c
index e18fc97d..0f048e7d 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -144,7 +144,7 @@ free_symbol (symbol *sym)
     {
       SYMBOL_DELETED (sym) = true;
       if (SYMBOL_STACK (sym)) {
-        SYMBOL_NAME (sym) = xstrdup (SYMBOL_NAME (sym));
+        SYMBOL_NAME (sym) = xmemdup0 (SYMBOL_NAME (sym), SYMBOL_NAME_LEN 
(sym));
         SYMBOL_STACK (sym) = NULL;
       }
     }
@@ -272,7 +272,7 @@ lookup_symbol (const char *name, int len, symbol_lookup 
mode)
         }
       else
         {
-          SYMBOL_NAME (sym) = xstrdup (name);
+          SYMBOL_NAME (sym) = xmemdup0 (name, len);
           SYMBOL_NAME_LEN (sym) = len;
         }
       return sym;
@@ -319,7 +319,7 @@ lookup_symbol (const char *name, int len, symbol_lookup 
mode)
             SYMBOL_TYPE (sym) = TOKEN_VOID;
             SYMBOL_TRACED (sym) = true;
             sym->hash = h;
-            SYMBOL_NAME (sym) = xstrdup (name);
+            SYMBOL_NAME (sym) = xmemdup0 (name, len);
             SYMBOL_NAME_LEN (sym) = len;
             SYMBOL_MACRO_ARGS (sym) = false;
             SYMBOL_BLIND_NO_ARGS (sym) = false;
-- 
2.48.1


_______________________________________________
M4-patches mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/m4-patches

Reply via email to