[1/2] openpty: close slave pty fd on error

Message ID 20170826134449.26527-1-christian.brauner@ubuntu.com
State New, archived
Headers

Commit Message

Christian Brauner Aug. 26, 2017, 1:44 p.m. UTC
  When openpty() failed only the master fd was closed so far. Let's close the
slave fd as well.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
---
 ChangeLog       | 4 ++++
 login/openpty.c | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)
  

Comments

Florian Weimer Aug. 29, 2017, 9:02 a.m. UTC | #1
On 08/26/2017 03:44 PM, Christian Brauner wrote:
>   fail:
>    close (master);
> +  if (slave != -1)
> +    close(slave);
>    return -1;

This is inconsistent with how the code frees buf if there is an error:
For buf, the free operation happens before the fail tail.  I think we
should keep this consistent: either free exactly what is needed, or have
a single function exit which checks for initialization and frees what
has been allocated.

Thanks,
Florian
  

Patch

diff --git a/ChangeLog b/ChangeLog
index bc1cf94dc3..bc5fb8e27f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@ 
+2017-08-26  Christian Brauner  <christian.brauner@ubuntu.com>
+
+	* login/openpty.c (openpty): Close slave pty file descriptor on error.
+
 2017-08-25  H.J. Lu  <hongjiu.lu@intel.com>
 
 	* sysdeps/x86/cpu-features.h [__ASSEMBLER__]
diff --git a/login/openpty.c b/login/openpty.c
index 41ab0483e2..8fbc66a3ef 100644
--- a/login/openpty.c
+++ b/login/openpty.c
@@ -92,7 +92,7 @@  openpty (int *amaster, int *aslave, char *name,
   char _buf[512];
 #endif
   char *buf = _buf;
-  int master, slave;
+  int master, slave = -1;
 
   master = getpt ();
   if (master == -1)
@@ -135,6 +135,8 @@  openpty (int *amaster, int *aslave, char *name,
 
  fail:
   close (master);
+  if (slave != -1)
+    close(slave);
   return -1;
 }
 libutil_hidden_def (openpty)