diff --git a/ptyprocess/ptyprocess.py b/ptyprocess/ptyprocess.py index daf20b1..faf9347 100644 --- a/ptyprocess/ptyprocess.py +++ b/ptyprocess/ptyprocess.py @@ -123,6 +123,10 @@ def _setecho(fd, state): raise def _setwinsize(fd, rows, cols): + if not (0 <= rows <= 65535 and 0 <= cols <= 65535): + raise ValueError( + "rows ({}) and cols ({}) must be between 0 and 65535".format(rows, cols) + ) # Some very old platforms have a bug that causes the value for # termios.TIOCSWINSZ to be truncated. There was a hack here to work # around this, but it caused problems with newer platforms so has been @@ -813,6 +817,8 @@ def setwinsize(self, rows, cols): TTY-aware applications like vi or curses -- applications that respond to the SIGWINCH signal. """ + if self.closed or self.fd < 0: + raise PtyProcessError("Cannot set window size on a closed PtyProcess.") return _setwinsize(self.fd, rows, cols) diff --git a/tests/test_spawn.py b/tests/test_spawn.py index 8aefff1..568e9a0 100755 --- a/tests/test_spawn.py +++ b/tests/test_spawn.py @@ -152,3 +152,15 @@ def test_pass_fds(self): with open(temp_file_name, 'r') as temp_file_r: assert temp_file_r.read() == 'hello' + + def test_setwinsize_bounds(self): + p = PtyProcess.spawn(['true']) + p.setwinsize(24, 80) + self.assertEqual(p.getwinsize(), (24, 80)) + with self.assertRaises(ValueError): + p.setwinsize(-1, 80) + with self.assertRaises(ValueError): + p.setwinsize(24, 70000) + p.close() + with self.assertRaises(Exception): + p.setwinsize(24, 80)