aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Brauner <christian.brauner@ubuntu.com>2017-10-08 14:09:47 +0200
committerChristian Brauner <christian.brauner@ubuntu.com>2017-10-08 17:46:19 +0200
commit98e0742024d4c13c08a6076b3d119c250e7d0118 (patch)
treefd5901696086595b8b036376c13c8ee1317f0307
parente7a574594931a8221b39432dc0637ec7423b14f6 (diff)
downloadglibc-98e0742024d4c13c08a6076b3d119c250e7d0118.tar
glibc-98e0742024d4c13c08a6076b3d119c250e7d0118.tar.gz
glibc-98e0742024d4c13c08a6076b3d119c250e7d0118.tar.bz2
glibc-98e0742024d4c13c08a6076b3d119c250e7d0118.zip
openpty: close slave pty fd on error
When openpty() failed only the master fd was closed so far. Let's close the slave fd as well. Also, let's unify the error handling. Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
-rw-r--r--ChangeLog4
-rw-r--r--login/openpty.c30
2 files changed, 20 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index deec63d1a8..e1e98adf1f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-10-08 Christian Brauner <christian.brauner@ubuntu.com>
+
+ * login/openpty.c (openpty): Close slave pty file descriptor on error.
+
2017-10-06 Joseph Myers <joseph@codesourcery.com>
* sysdeps/ieee754/ldbl-128/s_fma.c: Include <libm-alias-double.h>.
diff --git a/login/openpty.c b/login/openpty.c
index 41ab0483e2..9e556c27a5 100644
--- a/login/openpty.c
+++ b/login/openpty.c
@@ -92,29 +92,24 @@ openpty (int *amaster, int *aslave, char *name,
char _buf[512];
#endif
char *buf = _buf;
- int master, slave;
+ int master, ret = -1, slave = -1;
master = getpt ();
if (master == -1)
return -1;
if (grantpt (master))
- goto fail;
+ goto on_error;
if (unlockpt (master))
- goto fail;
+ goto on_error;
if (pts_name (master, &buf, sizeof (_buf)))
- goto fail;
+ goto on_error;
slave = open (buf, O_RDWR | O_NOCTTY);
if (slave == -1)
- {
- if (buf != _buf)
- free (buf);
-
- goto fail;
- }
+ goto on_error;
/* XXX Should we ignore errors here? */
if (termp)
@@ -129,12 +124,19 @@ openpty (int *amaster, int *aslave, char *name,
if (name != NULL)
strcpy (name, buf);
+ ret = 0;
+
+ on_error:
+ if (ret == -1) {
+ close (master);
+
+ if (slave != -1)
+ close (slave);
+ }
+
if (buf != _buf)
free (buf);
- return 0;
- fail:
- close (master);
- return -1;
+ return ret;
}
libutil_hidden_def (openpty)