emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] trunk r113446: * doc.c: Fix minor memory and file descript


From: Paul Eggert
Subject: [Emacs-diffs] trunk r113446: * doc.c: Fix minor memory and file descriptor leaks.
Date: Thu, 18 Jul 2013 09:55:03 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 113446
revision-id: address@hidden
parent: address@hidden
author: Paul Eggert  <address@hidden>
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Thu 2013-07-18 02:55:00 -0700
message:
  * doc.c: Fix minor memory and file descriptor leaks.
  
  * doc.c (get_doc_string): Fix memory leak when doc file absent.
  (get_doc_string, Fsnarf_documentation):
  Fix file descriptor leak on error.
modified:
  src/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1438
  src/doc.c                      doc.c-20091113204419-o5vbwnq5f7feedwu-250
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2013-07-18 08:35:27 +0000
+++ b/src/ChangeLog     2013-07-18 09:55:00 +0000
@@ -1,5 +1,10 @@
 2013-07-18  Paul Eggert  <address@hidden>
 
+       * doc.c: Fix minor memory and file descriptor leaks.
+       * doc.c (get_doc_string): Fix memory leak when doc file absent.
+       (get_doc_string, Fsnarf_documentation):
+       Fix file descriptor leak on error.
+
        * term.c: Fix minor fdopen-related file descriptor leaks.
        * term.c (Fresume_tty) [!MSDOS]: Close fd if fdopen (fd) fails.
        (init_tty) [!DOS_NT]: Likewise.  Also close fd if isatty (fd) fails.

=== modified file 'src/doc.c'
--- a/src/doc.c 2013-07-17 04:37:27 +0000
+++ b/src/doc.c 2013-07-18 09:55:00 +0000
@@ -85,6 +85,7 @@
   int offset;
   EMACS_INT position;
   Lisp_Object file, tem, pos;
+  ptrdiff_t count;
   USE_SAFE_ALLOCA;
 
   if (INTEGERP (filepos))
@@ -144,9 +145,14 @@
        }
 #endif
       if (fd < 0)
-       return concat3 (build_string ("Cannot open doc string file \""),
-                       file, build_string ("\"\n"));
+       {
+         SAFE_FREE ();
+         return concat3 (build_string ("Cannot open doc string file \""),
+                         file, build_string ("\"\n"));
+       }
     }
+  count = SPECPDL_INDEX ();
+  record_unwind_protect_int (close_file_unwind, fd);
 
   /* Seek only to beginning of disk block.  */
   /* Make sure we read at least 1024 bytes before `position'
@@ -154,13 +160,8 @@
   offset = min (position, max (1024, position % (8 * 1024)));
   if (TYPE_MAXIMUM (off_t) < position
       || lseek (fd, position - offset, 0) < 0)
-    {
-      emacs_close (fd);
-      error ("Position %"pI"d out of range in doc string file \"%s\"",
-            position, name);
-    }
-
-  SAFE_FREE ();
+    error ("Position %"pI"d out of range in doc string file \"%s\"",
+          position, name);
 
   /* Read the doc string into get_doc_string_buffer.
      P points beyond the data just read.  */
@@ -190,10 +191,7 @@
        space_left = 1024 * 8;
       nread = emacs_read (fd, p, space_left);
       if (nread < 0)
-       {
-         emacs_close (fd);
-         error ("Read error on documentation file");
-       }
+       report_file_error ("Read error on documentation file", file);
       p[nread] = 0;
       if (!nread)
        break;
@@ -209,7 +207,8 @@
        }
       p += nread;
     }
-  emacs_close (fd);
+  unbind_to (count, Qnil);
+  SAFE_FREE ();
 
   /* Sanity checking.  */
   if (CONSP (filepos))
@@ -574,6 +573,7 @@
   Lisp_Object sym;
   char *p, *name;
   bool skip_file = 0;
+  ptrdiff_t count;
 
   CHECK_STRING (filename);
 
@@ -615,6 +615,8 @@
       report_file_errno ("Opening doc string file", build_string (name),
                         open_errno);
     }
+  count = SPECPDL_INDEX ();
+  record_unwind_protect_int (close_file_unwind, fd);
   Vdoc_file_name = filename;
   filled = 0;
   pos = 0;
@@ -692,8 +694,7 @@
       filled -= end - buf;
       memmove (buf, end, filled);
     }
-  emacs_close (fd);
-  return Qnil;
+  return unbind_to (count, Qnil);
 }
 
 DEFUN ("substitute-command-keys", Fsubstitute_command_keys,


reply via email to

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