Skip to content

Commit

Permalink
TTY: n_tty, simplify read_buf+echo_buf allocation
Browse files Browse the repository at this point in the history
ldisc->open and close are called only once and cannot cross. So the
tests in open and close are superfluous. Remove them. (But leave sets
to NULL to ensure there is not a bug somewhere.)

And when the tests are gone, handle properly failures in open. We
leaked read_buf if allocation of echo_buf failed before. Now this is
not the case anymore.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Acked-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Jiri Slaby authored and Greg Kroah-Hartman committed Oct 22, 2012
1 parent f327b34 commit b91939f
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions drivers/tty/n_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -1561,14 +1561,10 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
static void n_tty_close(struct tty_struct *tty)
{
n_tty_flush_buffer(tty);
if (tty->read_buf) {
kfree(tty->read_buf);
tty->read_buf = NULL;
}
if (tty->echo_buf) {
kfree(tty->echo_buf);
tty->echo_buf = NULL;
}
kfree(tty->read_buf);
kfree(tty->echo_buf);
tty->read_buf = NULL;
tty->echo_buf = NULL;
}

/**
Expand All @@ -1587,24 +1583,23 @@ static int n_tty_open(struct tty_struct *tty)
return -EINVAL;

/* These are ugly. Currently a malloc failure here can panic */
if (!tty->read_buf) {
tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
if (!tty->read_buf)
return -ENOMEM;
}
if (!tty->echo_buf) {
tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
if (!tty->read_buf || !tty->echo_buf)
goto err_free_bufs;

if (!tty->echo_buf)
return -ENOMEM;
}
reset_buffer_flags(tty);
tty_unthrottle(tty);
tty->column = 0;
n_tty_set_termios(tty, NULL);
tty->minimum_to_wake = 1;
tty->closing = 0;
return 0;
err_free_bufs:
kfree(tty->read_buf);
kfree(tty->echo_buf);

return -ENOMEM;
}

static inline int input_available_p(struct tty_struct *tty, int amt)
Expand Down

0 comments on commit b91939f

Please sign in to comment.