This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 46f6316f1d072d38f5b115c126ba1241501d8efe
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 22:39:41 2026 +0100

    drivers/serial/ptmx: Return the error when the new master does not open.
    
    ptmx_open() did not handle a failed open of the new master or a failed
    file_dup2().  The open fails, for example, when the pipes of the pair
    cannot be allocated.  With the debug checks on, the kernel stopped;
    without them, ptmx_open() called file_dup2() with a file that was not
    open.
    
    Remove the pair and return the error instead.  The removal of the master
    frees the pair, its slave and its minor number (pty_unlink() and
    pty_destroy()).
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 drivers/serial/ptmx.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/drivers/serial/ptmx.c b/drivers/serial/ptmx.c
index fd9a1f5ad49..c7d8466395d 100644
--- a/drivers/serial/ptmx.c
+++ b/drivers/serial/ptmx.c
@@ -207,11 +207,17 @@ static int ptmx_open(FAR struct file *filep)
 
   snprintf(devname, sizeof(devname), "/dev/pty%d", minor);
   ret = file_open(&temp, devname, O_RDWR | O_CLOEXEC);
-  DEBUGASSERT(ret >= 0);  /* file_open() should never fail */
+  if (ret < 0)
+    {
+      goto errout_with_pair;
+    }
+
   ret = file_dup2(&temp, filep);
-  DEBUGASSERT(ret >= 0);  /* file_dup2() should never fail) */
-  ret = file_close(&temp);
-  DEBUGASSERT(ret >= 0);  /* file_close() should never fail */
+  file_close(&temp);
+  if (ret < 0)
+    {
+      goto errout_with_pair;
+    }
 
   /* Now unlink the master.  This will remove it from the VFS namespace,
    * the driver will still persist because of the open count on the
@@ -222,6 +228,15 @@ static int ptmx_open(FAR struct file *filep)
   DEBUGASSERT(ret >= 0 || ret == -EBUSY);  /* unregister_driver() should never 
fail */
 
   return OK;
+
+errout_with_pair:
+
+  /* Remove the pair: the unlink of the master, which is not open, frees
+   * it, its slave and the minor (pty_destroy()).
+   */
+
+  unregister_driver(devname);
+  return ret;
 }
 
 /****************************************************************************

Reply via email to