emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs-25 4f10c3c: module_format_fun_env fixes


From: Paul Eggert
Subject: [Emacs-diffs] emacs-25 4f10c3c: module_format_fun_env fixes
Date: Mon, 23 Nov 2015 23:46:15 +0000

branch: emacs-25
commit 4f10c3cdefc3d858bf297e55fb4a9ced36721bbb
Author: Paul Eggert <address@hidden>
Commit: Paul Eggert <address@hidden>

    module_format_fun_env fixes
    
    * src/doprnt.c (exprintf) [HAVE_MODULES]: Also define in this case.
    * src/emacs-module.c (module_format_fun_env):
    Convert path and sym to UTF-8.
    Don’t use VLAs, as the C11 standard says they’re optional,
    and anyway they can cause core dumps with large allocations.
    Use exprintf rather than snprintf, as exprintf handles arbitrarily
    long strings.  Simplify the code a bit.
---
 src/doprnt.c       |    2 +-
 src/emacs-module.c |   32 +++++++++++++-------------------
 2 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/src/doprnt.c b/src/doprnt.c
index 51f8fd7..55f249f 100644
--- a/src/doprnt.c
+++ b/src/doprnt.c
@@ -500,7 +500,7 @@ esprintf (char *buf, char const *format, ...)
   return nbytes;
 }
 
-#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
+#if HAVE_MODULES || (defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT)
 
 /* Format to buffer *BUF of positive size *BUFSIZE, reallocating *BUF
    and updating *BUFSIZE if the buffer is too small, and otherwise
diff --git a/src/emacs-module.c b/src/emacs-module.c
index a24d855..1a5e253 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -1039,25 +1039,19 @@ module_format_fun_env (const struct module_fun_env *env)
 {
   /* Try to print a function name if possible.  */
   const char *path, *sym;
-  if (dynlib_addr (env->subr, &path, &sym))
-    {
-      static char const format[] = "#<module function %s from %s>";
-      int size = snprintf (NULL, 0, format, sym, path);
-      eassert (size > 0);
-      char buffer[size + 1];
-      snprintf (buffer, sizeof buffer, format, sym, path);
-      return make_unibyte_string (buffer, size);
-    }
-  else
-    {
-      static char const format[] = "#<module function at %p>";
-      void *subr = env->subr;
-      int size = snprintf (NULL, 0, format, subr);
-      eassert (size > 0);
-      char buffer[size + 1];
-      snprintf (buffer, sizeof buffer, format, subr);
-      return make_unibyte_string (buffer, size);
-    }
+  char buffer[256];
+  char *buf = buffer;
+  ptrdiff_t bufsize = sizeof buffer;
+  ptrdiff_t size
+    = (dynlib_addr (env->subr, &path, &sym)
+       ? exprintf (&buf, &bufsize, buffer, -1,
+                  "#<module function %s from %s>", sym, path)
+       : exprintf (&buf, &bufsize, buffer, -1,
+                  "#<module function at %p>", env->subr));
+  Lisp_Object unibyte_result = make_unibyte_string (buffer, size);
+  if (buf != buffer)
+    xfree (buf);
+  return code_convert_string_norecord (unibyte_result, Qutf_8, false);
 }
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]