From 7a31fa98d6d5cd508a3784f972bc5b26f4f6f342 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Mon, 30 Jan 2012 22:01:32 +0100 Subject: [PATCH] pty: avoid 4KB buffers on the stack Allocate the IO-buffer dynamically on the heap for every pty object to avoid 4KB objects on the stack. This may not be a problem now but we might get stack overflows later if we continue to use such huge arrays on the stack. This doesn't affect the runtime performance as the buffer is still allocated only once on pty-creation. Signed-off-by: David Herrmann --- src/pty.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/pty.c b/src/pty.c index 043aeb1..c88d603 100644 --- a/src/pty.c +++ b/src/pty.c @@ -42,6 +42,9 @@ #include "misc.h" #include "pty.h" +/* Match N_TTY_BUF_SIZE from the kernel to read as much as we can. */ +#define KMSCON_NREAD 4096 + struct kmscon_pty { unsigned long ref; struct kmscon_eloop *eloop; @@ -49,6 +52,7 @@ struct kmscon_pty { int fd; struct kmscon_fd *efd; struct kmscon_ring *msgbuf; + char io_buf[KMSCON_NREAD]; kmscon_pty_input_cb input_cb; void *data; @@ -281,15 +285,11 @@ static int send_buf(struct kmscon_pty *pty) return 0; } -/* Match N_TTY_BUF_SIZE from the kernel to read as much as we can. */ -#define KMSCON_NREAD 4096 - static void pty_input(struct kmscon_fd *fd, int mask, void *data) { int ret; ssize_t len; struct kmscon_pty *pty = data; - char u8[KMSCON_NREAD]; if (!pty || pty->fd < 0) return; @@ -310,10 +310,10 @@ static void pty_input(struct kmscon_fd *fd, int mask, void *data) } if (mask & KMSCON_READABLE) { - len = read(pty->fd, u8, KMSCON_NREAD); + len = read(pty->fd, pty->io_buf, sizeof(pty->io_buf)); if (len > 0) { if (pty->input_cb) - pty->input_cb(pty, u8, len, pty->data); + pty->input_cb(pty, pty->io_buf, len, pty->data); } else if (len == 0) { log_debug("pty: child closed remote end\n"); goto err;