Skip to contentSkip to conversation

util: Remove redundant checks in the openpty()

PATCH Superseded AlexChen 2 participants 4 messages Oct 31, 2020 – Nov 2, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

AlexChen

author
#0 lore raw

PATCH 1/1 util: Remove redundant checks in the openpty()

As we can see from the following function call stack, the amaster and the aslave cannot be NULL: char_pty_open() -> qemu_openpty_raw() -> openpty().
In addition, the amaster and the aslave has been dereferenced at the beginning of the openpty(). So the checks on amaster and aslave in the openpty() are redundant.

---
 util/qemu-openpty.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
util/qemu-openpty.c +3 -4
index eb17f5b0bc..427f43a769 100644
@@ -80,10 +80,9 @@ static int openpty(int *amaster, int *aslave, char *name,
             (termp != NULL && tcgetattr(sfd, termp) < 0))
                 goto err;
 
-        if (amaster)
-                *amaster = mfd;
-        if (aslave)
-                *aslave = sfd;
+        *amaster = mfd;
+        *aslave = sfd;
+
         if (winp)
                 ioctl(sfd, TIOCSWINSZ, winp);
 
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · AlexChen

As we can see from the following function call stack, the amaster and the aslave cannot be NULL: char_pty_open() -> qemu_openpty_raw() -> openpty().
In addition, the amaster and the aslave has been dereferenced at the beginning of the openpty(). So the checks on amaster and aslave in the openpty() are redundant.

This function is trying to match the BSD/glibc openpty() function, so the thing to check here is not QEMU's specific current usage but the API specification for openpty():
https://www.gnu.org/software/libc/manual/html_node/Pseudo_002dTerminal-Pairs.html
https://www.freebsd.org/cgi/man.cgi?query=openpty

The spec says that name, termp and winp can all be NULL, but it doesn't say this for amaster and aslave, so indeed the change in this patch is the correct one.

Quoted 11 lines
---
 util/qemu-openpty.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
util/qemu-openpty.c +0 -0
index eb17f5b0bc..427f43a769 100644
@@ -80,10 +80,9 @@ static int openpty(int *amaster, int *aslave, char *name,
             (termp != NULL && tcgetattr(sfd, termp) < 0))
                 goto err;

util/qemu-openpty.c static int openpty(int *amaster, int *aslave, char *name, · line 87

Quoted 10 lines
util/qemu-openpty.c +3 -4
 
-        if (amaster)
-                *amaster = mfd;
-        if (aslave)
-                *aslave = sfd;
+        *amaster = mfd;
+        *aslave = sfd;
+
         if (winp)
                 ioctl(sfd, TIOCSWINSZ, winp);

though you might like to mention in the commit message that the openpty() API doesn't allow NULL amaster or aslave arguments.

thanks
-- PMM

author
#2 lore raw
Quoted 44 lines · Peter Maydell
Quoted 8 lines · AlexChen

As we can see from the following function call stack, the amaster and the aslave cannot be NULL: char_pty_open() -> qemu_openpty_raw() -> openpty().
In addition, the amaster and the aslave has been dereferenced at the beginning of the openpty(). So the checks on amaster and aslave in the openpty() are redundant.

This function is trying to match the BSD/glibc openpty() function, so the thing to check here is not QEMU's specific current usage but the API specification for openpty():
https://www.gnu.org/software/libc/manual/html_node/Pseudo_002dTerminal-Pairs.html
https://www.freebsd.org/cgi/man.cgi?query=openpty

The spec says that name, termp and winp can all be NULL, but it doesn't say this for amaster and aslave, so indeed the change in this patch is the correct one.

Quoted 11 lines
---
 util/qemu-openpty.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
util/qemu-openpty.c +0 -0
index eb17f5b0bc..427f43a769 100644
@@ -80,10 +80,9 @@ static int openpty(int *amaster, int *aslave, char *name,
             (termp != NULL && tcgetattr(sfd, termp) < 0))
                 goto err;

util/qemu-openpty.c static int openpty(int *amaster, int *aslave, char *name, · line 87

Quoted 10 lines
util/qemu-openpty.c +3 -4
 
-        if (amaster)
-                *amaster = mfd;
-        if (aslave)
-                *aslave = sfd;
+        *amaster = mfd;
+        *aslave = sfd;
+
         if (winp)
                 ioctl(sfd, TIOCSWINSZ, winp);
Quoted 4 lines

though you might like to mention in the commit message that the openpty() API doesn't allow NULL amaster or aslave arguments.

Thanks for your review, I will add this description to my commit message in my patch V2. In addition, since the amaster and the aslave are not allow to be NULL,
do we need to check that the amaster and the aslave are NULL in the beginning of the openpty()? such as this modification:

util/qemu-openpty.c +8 -5
index eb17f5b0bc..1aadd39395 100644
@@ -61,6 +61,9 @@ static int openpty(int *amaster, int *aslave, char *name,
         const char *slave;
         int mfd = -1, sfd = -1;
 
+        if (!amaster || !aslave)
+                goto err;
+
         *amaster = *aslave = -1;
 
         mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
@@ -80,10 +83,9 @@ static int openpty(int *amaster, int *aslave, char *name,
             (termp != NULL && tcgetattr(sfd, termp) < 0))
                 goto err;
 
-        if (amaster)
-                *amaster = mfd;
-        if (aslave)
-                *aslave = sfd;
+        *amaster = mfd;
+        *aslave = sfd;
+
         if (winp)
                 ioctl(sfd, TIOCSWINSZ, winp);
 
@@ -92,7 +94,8 @@ static int openpty(int *amaster, int *aslave, char *name,
 err:
         if (sfd != -1)
                 close(sfd);
-        close(mfd);
+        if (mfd != -1)
+                close(mfd);
         return -1;
 }
 #endif
Signature · 4 lines
2.19.1

Thanks,
Alex
#3 lore raw
Quoted 3 lines · AlexChen

Thanks for your review, I will add this description to my commit message in my patch V2. In addition, since the amaster and the aslave are not allow to be NULL,
do we need to check that the amaster and the aslave are NULL in the beginning of the openpty()?

No, we can just assume it. Our coding style doesn't mandate that level of defensive-coding against caller errors.

thanks
-- PMM