aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2013-07-18 02:55:00 -0700
committerPaul Eggert2013-07-18 02:55:00 -0700
commita8cd4836451373f56b719cf3b80fb3344a3422db (patch)
tree393e995b334065f71d867bfcc2e0e2714ecdf2c1 /src
parentef30e6382ab0a40bbea9360555e73e1e7f27dc6f (diff)
downloademacs-a8cd4836451373f56b719cf3b80fb3344a3422db.tar.gz
emacs-a8cd4836451373f56b719cf3b80fb3344a3422db.zip
* 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.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog5
-rw-r--r--src/doc.c33
2 files changed, 22 insertions, 16 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 6c9cfaa7412..cdc56419f63 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
12013-07-18 Paul Eggert <eggert@cs.ucla.edu> 12013-07-18 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 * doc.c: Fix minor memory and file descriptor leaks.
4 * doc.c (get_doc_string): Fix memory leak when doc file absent.
5 (get_doc_string, Fsnarf_documentation):
6 Fix file descriptor leak on error.
7
3 * term.c: Fix minor fdopen-related file descriptor leaks. 8 * term.c: Fix minor fdopen-related file descriptor leaks.
4 * term.c (Fresume_tty) [!MSDOS]: Close fd if fdopen (fd) fails. 9 * term.c (Fresume_tty) [!MSDOS]: Close fd if fdopen (fd) fails.
5 (init_tty) [!DOS_NT]: Likewise. Also close fd if isatty (fd) fails. 10 (init_tty) [!DOS_NT]: Likewise. Also close fd if isatty (fd) fails.
diff --git a/src/doc.c b/src/doc.c
index 168af6da94a..009616f4f87 100644
--- a/src/doc.c
+++ b/src/doc.c
@@ -85,6 +85,7 @@ get_doc_string (Lisp_Object filepos, bool unibyte, bool definition)
85 int offset; 85 int offset;
86 EMACS_INT position; 86 EMACS_INT position;
87 Lisp_Object file, tem, pos; 87 Lisp_Object file, tem, pos;
88 ptrdiff_t count;
88 USE_SAFE_ALLOCA; 89 USE_SAFE_ALLOCA;
89 90
90 if (INTEGERP (filepos)) 91 if (INTEGERP (filepos))
@@ -144,9 +145,14 @@ get_doc_string (Lisp_Object filepos, bool unibyte, bool definition)
144 } 145 }
145#endif 146#endif
146 if (fd < 0) 147 if (fd < 0)
147 return concat3 (build_string ("Cannot open doc string file \""), 148 {
148 file, build_string ("\"\n")); 149 SAFE_FREE ();
150 return concat3 (build_string ("Cannot open doc string file \""),
151 file, build_string ("\"\n"));
152 }
149 } 153 }
154 count = SPECPDL_INDEX ();
155 record_unwind_protect_int (close_file_unwind, fd);
150 156
151 /* Seek only to beginning of disk block. */ 157 /* Seek only to beginning of disk block. */
152 /* Make sure we read at least 1024 bytes before `position' 158 /* Make sure we read at least 1024 bytes before `position'
@@ -154,13 +160,8 @@ get_doc_string (Lisp_Object filepos, bool unibyte, bool definition)
154 offset = min (position, max (1024, position % (8 * 1024))); 160 offset = min (position, max (1024, position % (8 * 1024)));
155 if (TYPE_MAXIMUM (off_t) < position 161 if (TYPE_MAXIMUM (off_t) < position
156 || lseek (fd, position - offset, 0) < 0) 162 || lseek (fd, position - offset, 0) < 0)
157 { 163 error ("Position %"pI"d out of range in doc string file \"%s\"",
158 emacs_close (fd); 164 position, name);
159 error ("Position %"pI"d out of range in doc string file \"%s\"",
160 position, name);
161 }
162
163 SAFE_FREE ();
164 165
165 /* Read the doc string into get_doc_string_buffer. 166 /* Read the doc string into get_doc_string_buffer.
166 P points beyond the data just read. */ 167 P points beyond the data just read. */
@@ -190,10 +191,7 @@ get_doc_string (Lisp_Object filepos, bool unibyte, bool definition)
190 space_left = 1024 * 8; 191 space_left = 1024 * 8;
191 nread = emacs_read (fd, p, space_left); 192 nread = emacs_read (fd, p, space_left);
192 if (nread < 0) 193 if (nread < 0)
193 { 194 report_file_error ("Read error on documentation file", file);
194 emacs_close (fd);
195 error ("Read error on documentation file");
196 }
197 p[nread] = 0; 195 p[nread] = 0;
198 if (!nread) 196 if (!nread)
199 break; 197 break;
@@ -209,7 +207,8 @@ get_doc_string (Lisp_Object filepos, bool unibyte, bool definition)
209 } 207 }
210 p += nread; 208 p += nread;
211 } 209 }
212 emacs_close (fd); 210 unbind_to (count, Qnil);
211 SAFE_FREE ();
213 212
214 /* Sanity checking. */ 213 /* Sanity checking. */
215 if (CONSP (filepos)) 214 if (CONSP (filepos))
@@ -574,6 +573,7 @@ the same file name is found in the `doc-directory'. */)
574 Lisp_Object sym; 573 Lisp_Object sym;
575 char *p, *name; 574 char *p, *name;
576 bool skip_file = 0; 575 bool skip_file = 0;
576 ptrdiff_t count;
577 577
578 CHECK_STRING (filename); 578 CHECK_STRING (filename);
579 579
@@ -615,6 +615,8 @@ the same file name is found in the `doc-directory'. */)
615 report_file_errno ("Opening doc string file", build_string (name), 615 report_file_errno ("Opening doc string file", build_string (name),
616 open_errno); 616 open_errno);
617 } 617 }
618 count = SPECPDL_INDEX ();
619 record_unwind_protect_int (close_file_unwind, fd);
618 Vdoc_file_name = filename; 620 Vdoc_file_name = filename;
619 filled = 0; 621 filled = 0;
620 pos = 0; 622 pos = 0;
@@ -692,8 +694,7 @@ the same file name is found in the `doc-directory'. */)
692 filled -= end - buf; 694 filled -= end - buf;
693 memmove (buf, end, filled); 695 memmove (buf, end, filled);
694 } 696 }
695 emacs_close (fd); 697 return unbind_to (count, Qnil);
696 return Qnil;
697} 698}
698 699
699DEFUN ("substitute-command-keys", Fsubstitute_command_keys, 700DEFUN ("substitute-command-keys", Fsubstitute_command_keys,