emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master 162ba40: Fix unlikely substitute-command-keys memor


From: Paul Eggert
Subject: [Emacs-diffs] master 162ba40: Fix unlikely substitute-command-keys memory leak
Date: Thu, 8 Dec 2016 18:44:02 +0000 (UTC)

branch: master
commit 162ba405ac144c2a0cb6854f791ff7d3203b0e2f
Author: Paul Eggert <address@hidden>
Commit: Paul Eggert <address@hidden>

    Fix unlikely substitute-command-keys memory leak
    
    * src/doc.c (Fsubstitute_command_keys):
    Free buffer when unwinding.
---
 src/doc.c |   17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/doc.c b/src/doc.c
index ce4f89b..6a78ed6 100644
--- a/src/doc.c
+++ b/src/doc.c
@@ -772,6 +772,8 @@ Otherwise, return a new string.  */)
   /* Extra room for expansion due to replacing ‘\[]’ with ‘M-x ’.  */
   enum { EXTRA_ROOM = sizeof "M-x " - sizeof "\\[]" };
 
+  ptrdiff_t count = SPECPDL_INDEX ();
+
   if (bsize <= sizeof sbuf - EXTRA_ROOM)
     {
       abuf = NULL;
@@ -779,7 +781,10 @@ Otherwise, return a new string.  */)
       bsize = sizeof sbuf;
     }
   else
-    buf = abuf = xpalloc (NULL, &bsize, EXTRA_ROOM, STRING_BYTES_BOUND, 1);
+    {
+      buf = abuf = xpalloc (NULL, &bsize, EXTRA_ROOM, STRING_BYTES_BOUND, 1);
+      record_unwind_protect_ptr (xfree, abuf);
+    }
   bufp = buf;
 
   strp = SDATA (str);
@@ -929,7 +934,12 @@ Otherwise, return a new string.  */)
                abuf = xpalloc (abuf, &bsize, need - avail,
                                STRING_BYTES_BOUND, 1);
                if (buf == sbuf)
-                 memcpy (abuf, sbuf, offset);
+                 {
+                   record_unwind_protect_ptr (xfree, abuf);
+                   memcpy (abuf, sbuf, offset);
+                 }
+               else
+                 set_unwind_protect_ptr (count, xfree, abuf);
                buf = abuf;
                bufp = buf + offset;
              }
@@ -988,8 +998,7 @@ Otherwise, return a new string.  */)
     }
   else
     tem = string;
-  xfree (abuf);
-  return tem;
+  return unbind_to (count, tem);
 }
 
 void



reply via email to

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