unix: don't clobber errno in uv_tty_reset_mode()

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é <saghul@gmail.com>
This commit is contained in:
Ben Noordhuis 2015-03-11 15:42:20 +01:00
parent 25d4abbabb
commit 1df46fe478
2 changed files with 11 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);