Commit ef30e638 authored by Paul Eggert's avatar Paul Eggert

* 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.
parent f4b1eb36
2013-07-18 Paul Eggert <eggert@cs.ucla.edu> 2013-07-18 Paul Eggert <eggert@cs.ucla.edu>
* 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.
* charset.c: Fix file descriptor leaks and errno issues. * charset.c: Fix file descriptor leaks and errno issues.
Include <errno.h>. Include <errno.h>.
(load_charset_map_from_file): Don't leak file descriptor on error. (load_charset_map_from_file): Don't leak file descriptor on error.
......
...@@ -2416,15 +2416,20 @@ frame's terminal). */) ...@@ -2416,15 +2416,20 @@ frame's terminal). */)
t->display_info.tty->input = stdin; t->display_info.tty->input = stdin;
#else /* !MSDOS */ #else /* !MSDOS */
fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0); fd = emacs_open (t->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
t->display_info.tty->input = t->display_info.tty->output
= fd < 0 ? 0 : fdopen (fd, "w+");
if (fd == -1) if (! t->display_info.tty->input)
error ("Can not reopen tty device %s: %s", t->display_info.tty->name, strerror (errno)); {
int open_errno = errno;
emacs_close (fd);
report_file_errno ("Cannot reopen tty device",
build_string (t->display_info.tty->name),
open_errno);
}
if (!O_IGNORE_CTTY && strcmp (t->display_info.tty->name, DEV_TTY) != 0) if (!O_IGNORE_CTTY && strcmp (t->display_info.tty->name, DEV_TTY) != 0)
dissociate_if_controlling_tty (fd); dissociate_if_controlling_tty (fd);
t->display_info.tty->output = fdopen (fd, "w+");
t->display_info.tty->input = t->display_info.tty->output;
#endif #endif
add_keyboard_wait_descriptor (fd); add_keyboard_wait_descriptor (fd);
...@@ -2990,7 +2995,6 @@ init_tty (const char *name, const char *terminal_type, bool must_succeed) ...@@ -2990,7 +2995,6 @@ init_tty (const char *name, const char *terminal_type, bool must_succeed)
{ {
/* Open the terminal device. */ /* Open the terminal device. */
FILE *file;
/* If !ctty, don't recognize it as our controlling terminal, and /* If !ctty, don't recognize it as our controlling terminal, and
don't make it the controlling tty if we don't have one now. 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,30 +3005,21 @@ init_tty (const char *name, const char *terminal_type, bool must_succeed)
open a frame on the same terminal. */ open a frame on the same terminal. */
int flags = O_RDWR | O_NOCTTY | (ctty ? 0 : O_IGNORE_CTTY); int flags = O_RDWR | O_NOCTTY | (ctty ? 0 : O_IGNORE_CTTY);
int fd = emacs_open (name, flags, 0); int fd = emacs_open (name, flags, 0);
tty->input = tty->output = fd < 0 || ! isatty (fd) ? 0 : fdopen (fd, "w+");
tty->name = xstrdup (name); if (! tty->input)
terminal->name = xstrdup (name);
if (fd < 0)
maybe_fatal (must_succeed, terminal,
"Could not open file: %s",
"Could not open file: %s",
name);
if (!isatty (fd))
{ {
emacs_close (fd); char const *diagnostic
maybe_fatal (must_succeed, terminal, = tty->input ? "Not a tty device: %s" : "Could not open file: %s";
"Not a tty device: %s", emacs_close (fd);
"Not a tty device: %s", maybe_fatal (must_succeed, terminal, diagnostic, diagnostic, name);
name);
} }
tty->name = xstrdup (name);
terminal->name = xstrdup (name);
if (!O_IGNORE_CTTY && !ctty) if (!O_IGNORE_CTTY && !ctty)
dissociate_if_controlling_tty (fd); dissociate_if_controlling_tty (fd);
file = fdopen (fd, "w+");
tty->input = file;
tty->output = file;
} }
tty->type = xstrdup (terminal_type); tty->type = xstrdup (terminal_type);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment