From 1df46fe47869244db44e90d2ced5685a4c921775 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 11 Mar 2015 15:42:20 +0100 Subject: [PATCH] unix: don't clobber errno in uv_tty_reset_mode() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit uv_tty_reset_mode() is designed to be async signal-safe and is therefore not allowed to clobber errno. PR-URL: https://github.com/libuv/libuv/pull/259 Reviewed-By: Saúl Ibarra Corretgé --- src/unix/tty.c | 4 ++++ test/test-tty.c | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/unix/tty.c b/src/unix/tty.c index b1782df9..9038d85a 100644 --- a/src/unix/tty.c +++ b/src/unix/tty.c @@ -237,8 +237,10 @@ uv_handle_type uv_guess_handle(uv_file file) { * critical section when the signal was raised. */ int uv_tty_reset_mode(void) { + int saved_errno; int err; + saved_errno = errno; if (!uv_spinlock_trylock(&termios_spinlock)) return -EBUSY; /* In uv_tty_set_mode(). */ @@ -248,5 +250,7 @@ int uv_tty_reset_mode(void) { err = -errno; uv_spinlock_unlock(&termios_spinlock); + errno = saved_errno; + return err; } diff --git a/test/test-tty.c b/test/test-tty.c index cfcb7702..cb742fe1 100644 --- a/test/test-tty.c +++ b/test/test-tty.c @@ -118,6 +118,13 @@ TEST_IMPL(tty) { r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_NORMAL); ASSERT(r == 0); + /* Calling uv_tty_reset_mode() repeatedly should not clobber errno. */ + errno = 0; + ASSERT(0 == uv_tty_reset_mode()); + ASSERT(0 == uv_tty_reset_mode()); + ASSERT(0 == uv_tty_reset_mode()); + ASSERT(0 == errno); + /* TODO check the actual mode! */ uv_close((uv_handle_t*) &tty_in, NULL);