aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2013-07-18 01:35:27 -0700
committerPaul Eggert2013-07-18 01:35:27 -0700
commitef30e6382ab0a40bbea9360555e73e1e7f27dc6f (patch)
tree4ce292eaafaec65ce86d6cc494fab321f6ebcd3c /src
parentf4b1eb36186a2f873d84d4c089292f9fb0394d31 (diff)
downloademacs-ef30e6382ab0a40bbea9360555e73e1e7f27dc6f.tar.gz
emacs-ef30e6382ab0a40bbea9360555e73e1e7f27dc6f.zip
* 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.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog4
-rw-r--r--src/term.c43
2 files changed, 23 insertions, 24 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 3f667a7bcc7..6c9cfaa7412 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,9 @@
12013-07-18 Paul Eggert <eggert@cs.ucla.edu> 12013-07-18 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 * term.c: Fix minor fdopen-related file descriptor leaks.
4 * term.c (Fresume_tty) [!MSDOS]: Close fd if fdopen (fd) fails.
5 (init_tty) [!DOS_NT]: Likewise. Also close fd if isatty (fd) fails.
6
3 * charset.c: Fix file descriptor leaks and errno issues. 7 * charset.c: Fix file descriptor leaks and errno issues.
4 Include <errno.h>. 8 Include <errno.h>.
5 (load_charset_map_from_file): Don't leak file descriptor on error. 9 (load_charset_map_from_file): Don't leak file descriptor on error.
diff --git a/src/term.c b/src/term.c
index b6878a0abd1..376d6e7831a 100644
--- a/src/term.c
+++ b/src/term.c
@@ -2416,15 +2416,20 @@ frame's terminal). */)
2416 t->display_info.tty->input = stdin; 2416 t->display_info.tty->input = stdin;
2417#else /* !MSDOS */ 2417#else /* !MSDOS */
2418 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0); 2418 fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
2419 t->display_info.tty->input = t->display_info.tty->output
2420 = fd < 0 ? 0 : fdopen (fd, "w+");
2419 2421
2420 if (fd == -1) 2422 if (! t->display_info.tty->input)
2421 error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno)); 2423 {
2424 int open_errno = errno;
2425 emacs_close (fd);
2426 report_file_errno ("Cannot reopen tty device",
2427 build_string (t->display_info.tty->name),
2428 open_errno);
2429 }
2422 2430
2423 if (!O_IGNORE_CTTY && strcmp (t->display_info.tty->name, DEV_TTY) != 0) 2431 if (!O_IGNORE_CTTY && strcmp (t->display_info.tty->name, DEV_TTY) != 0)
2424 dissociate_if_controlling_tty (fd); 2432 dissociate_if_controlling_tty (fd);
2425
2426 t->display_info.tty->output = fdopen (fd, "w+");
2427 t->display_info.tty->input = t->display_info.tty->output;
2428#endif 2433#endif
2429 2434
2430 add_keyboard_wait_descriptor (fd); 2435 add_keyboard_wait_descriptor (fd);
@@ -2990,7 +2995,6 @@ init_tty (const char *name, const char *terminal_type, bool must_succeed)
2990 2995
2991 { 2996 {
2992 /* Open the terminal device. */ 2997 /* Open the terminal device. */
2993 FILE *file;
2994 2998
2995 /* If !ctty, don't recognize it as our controlling terminal, and 2999 /* If !ctty, don't recognize it as our controlling terminal, and
2996 don't make it the controlling tty if we don't have one now. 3000 don't make it the controlling tty if we don't have one now.
@@ -3001,30 +3005,21 @@ init_tty (const char *name, const char *terminal_type, bool must_succeed)
3001 open a frame on the same terminal. */ 3005 open a frame on the same terminal. */
3002 int flags = O_RDWR | O_NOCTTY | (ctty ? 0 : O_IGNORE_CTTY); 3006 int flags = O_RDWR | O_NOCTTY | (ctty ? 0 : O_IGNORE_CTTY);
3003 int fd = emacs_open (name, flags, 0); 3007 int fd = emacs_open (name, flags, 0);
3008 tty->input = tty->output = fd < 0 || ! isatty (fd) ? 0 : fdopen (fd, "w+");
3004 3009
3005 tty->name = xstrdup (name); 3010 if (! tty->input)
3006 terminal->name = xstrdup (name);
3007
3008 if (fd < 0)
3009 maybe_fatal (must_succeed, terminal,
3010 "Could not open file: %s",
3011 "Could not open file: %s",
3012 name);
3013 if (!isatty (fd))
3014 { 3011 {
3015 emacs_close (fd); 3012 char const *diagnostic
3016 maybe_fatal (must_succeed, terminal, 3013 = tty->input ? "Not a tty device: %s" : "Could not open file: %s";
3017 "Not a tty device: %s", 3014 emacs_close (fd);
3018 "Not a tty device: %s", 3015 maybe_fatal (must_succeed, terminal, diagnostic, diagnostic, name);
3019 name);
3020 } 3016 }
3021 3017
3018 tty->name = xstrdup (name);
3019 terminal->name = xstrdup (name);
3020
3022 if (!O_IGNORE_CTTY && !ctty) 3021 if (!O_IGNORE_CTTY && !ctty)
3023 dissociate_if_controlling_tty (fd); 3022 dissociate_if_controlling_tty (fd);
3024
3025 file = fdopen (fd, "w+");
3026 tty->input = file;
3027 tty->output = file;
3028 } 3023 }
3029 3024
3030 tty->type = xstrdup (terminal_type); 3025 tty->type = xstrdup (terminal_type);