From ef6a30c9c92f72ed81c584e68ed52b7aa3d84aa8 Mon Sep 17 00:00:00 2001 From: Landon Curt Noll Date: Sat, 3 Dec 2022 10:25:43 -0800 Subject: [PATCH] Fixed how the original TTY state is preserved In find_tty_state(), we changed how we expand fd_orig as an original pre-modified copy of fd_setup. We realloc the fd_orig array and copy the fd_setup into it first, before touching the fd_setup array. --- CHANGES | 4 ++++ lib_calc.c | 17 +++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index db65466..84fdd98 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,10 @@ The following are the changes from calc version 2.14.2.0 to date: in find_tty_state() could move the fd_setup array. Thanks goes to for reporting this bug! + In find_tty_state(), we changed how we expand fd_orig as an original + pre-modified copy of fd_setup. We realloc the fd_orig array and copy + the fd_setup into it first, before touching the fd_setup array. + The following are the changes from calc version 2.14.1.2 to date: diff --git a/lib_calc.c b/lib_calc.c index 073101d..2af283c 100644 --- a/lib_calc.c +++ b/lib_calc.c @@ -796,6 +796,15 @@ find_tty_state(int fd) /* * no empty slots exist, realloc another slot */ + /* expand fd_orig as an original pre-modified copy of fd_setup */ + new_fd_orig = (ttystruct *)realloc(fd_orig, sizeof(fd_orig[0]) * + (fd_setup_len+1)); + if (new_fd_orig == NULL) { + return -1; + } + fd_orig = new_fd_orig; + memcpy(fd_orig, fd_setup, sizeof(fd_orig[0]) * (fd_setup_len+1)); + /* expand fd_setup */ new_fd_setup = (int *)realloc(fd_setup, sizeof(fd_setup[0]) * (fd_setup_len+1)); @@ -805,14 +814,6 @@ find_tty_state(int fd) fd_setup = new_fd_setup; new_fd_setup[fd_setup_len] = -1; - /* expand fd_orig */ - new_fd_orig = (ttystruct *)realloc(fd_orig, sizeof(fd_orig[0]) * - (fd_setup_len+1)); - if (new_fd_orig == NULL) { - return -1; - } - fd_orig = new_fd_orig; - /* expand fd_cur */ new_fd_cur = (ttystruct *)realloc(fd_cur, sizeof(fd_cur[0]) * (fd_setup_len+1));