Fix fd leak in error condition in grantpt code case.
[kai/samba.git] / source / smbd / chgpasswd.c
index 4369573fe8015d9f89dc3c5f1e0717a231d9f05f..52ca364070a04d68758f624b553c5d2b7fde6759 100644 (file)
@@ -71,6 +71,8 @@ static int findpty(char **slave)
     *slave = ptsname(master);
     if (*slave == NULL) {
       DEBUG(0,("findpty: Unable to create master/slave pty pair.\n"));
+      /* Stop fd leak on error. */
+      close(master);
       return -1;
     } else {
       DEBUG(10, ("findpty: Allocated slave pty %s\n", *slave));
@@ -81,7 +83,8 @@ static int findpty(char **slave)
   fstrcpy( line, "/dev/ptyXX" );
 
   dirp = OpenDir(NULL, "/dev", False);
-  if (!dirp) return(-1);
+  if (!dirp)
+    return(-1);
   while ((dpname = ReadDirName(dirp)) != NULL) {
     if (strncmp(dpname, "pty", 3) == 0 && strlen(dpname) == 5) {
       DEBUG(3,("pty: try to open %s, line was %s\n", dpname, line ) );
@@ -89,10 +92,10 @@ static int findpty(char **slave)
       line[9] = dpname[4];
       if ((master = open(line, O_RDWR)) >= 0) {
         DEBUG(3,("pty: opened %s\n", line ) );
-       line[5] = 't';
-       *slave = line;
-       CloseDir(dirp);
-       return (master);
+        line[5] = 't';
+        *slave = line;
+        CloseDir(dirp);
+        return (master);
       }
     }
   }