* [PATCH] util: Remove redundant checks in the openpty()
@ 2020-10-31 11:03 AlexChen
2020-10-31 15:21 ` Peter Maydell
0 siblings, 1 reply; 4+ messages in thread
From: AlexChen @ 2020-10-31 11:03 UTC (permalink / raw)
To: Peter Maydell, mjt; +Cc: QEMU Trivial, QEMU
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.
Reported-by: Euler Robot <euler.robot@huawei.com>
Signed-off-by: Alex Chen <alex.chen@huawei.com>
---
util/qemu-openpty.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/util/qemu-openpty.c b/util/qemu-openpty.c
index eb17f5b0bc..427f43a769 100644
--- a/util/qemu-openpty.c
+++ b/util/qemu-openpty.c
@@ -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);
--
2.19.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] util: Remove redundant checks in the openpty()
2020-10-31 11:03 [PATCH] util: Remove redundant checks in the openpty() AlexChen
@ 2020-10-31 15:21 ` Peter Maydell
2020-11-02 5:12 ` AlexChen
0 siblings, 1 reply; 4+ messages in thread
From: Peter Maydell @ 2020-10-31 15:21 UTC (permalink / raw)
To: AlexChen; +Cc: QEMU Trivial, Michael Tokarev, QEMU
On Sat, 31 Oct 2020 at 11:04, AlexChen <alex.chen@huawei.com> wrote:
>
> 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.
>
> Reported-by: Euler Robot <euler.robot@huawei.com>
> Signed-off-by: Alex Chen <alex.chen@huawei.com>
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.
> ---
> util/qemu-openpty.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/util/qemu-openpty.c b/util/qemu-openpty.c
> index eb17f5b0bc..427f43a769 100644
> --- a/util/qemu-openpty.c
> +++ b/util/qemu-openpty.c
> @@ -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);
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
though you might like to mention in the commit message that
the openpty() API doesn't allow NULL amaster or aslave
arguments.
thanks
-- PMM
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] util: Remove redundant checks in the openpty()
2020-10-31 15:21 ` Peter Maydell
@ 2020-11-02 5:12 ` AlexChen
2020-11-02 9:50 ` Peter Maydell
0 siblings, 1 reply; 4+ messages in thread
From: AlexChen @ 2020-11-02 5:12 UTC (permalink / raw)
To: Peter Maydell; +Cc: QEMU Trivial, Michael Tokarev, QEMU
On 2020/10/31 23:21, Peter Maydell wrote:
> On Sat, 31 Oct 2020 at 11:04, AlexChen <alex.chen@huawei.com> wrote:
>>
>> 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.
>>
>> Reported-by: Euler Robot <euler.robot@huawei.com>
>> Signed-off-by: Alex Chen <alex.chen@huawei.com>
>
> 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.
>
>> ---
>> util/qemu-openpty.c | 7 +++----
>> 1 file changed, 3 insertions(+), 4 deletions(-)
>>
>> diff --git a/util/qemu-openpty.c b/util/qemu-openpty.c
>> index eb17f5b0bc..427f43a769 100644
>> --- a/util/qemu-openpty.c
>> +++ b/util/qemu-openpty.c
>> @@ -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);
>
> Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
>
> 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:
diff --git a/util/qemu-openpty.c b/util/qemu-openpty.c
index eb17f5b0bc..1aadd39395 100644
--- a/util/qemu-openpty.c
+++ b/util/qemu-openpty.c
@@ -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
--
2.19.1
Thanks,
Alex
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] util: Remove redundant checks in the openpty()
2020-11-02 5:12 ` AlexChen
@ 2020-11-02 9:50 ` Peter Maydell
0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2020-11-02 9:50 UTC (permalink / raw)
To: AlexChen; +Cc: QEMU Trivial, Michael Tokarev, QEMU
On Mon, 2 Nov 2020 at 05:12, AlexChen <alex.chen@huawei.com> wrote:
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-11-02 10:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-31 11:03 [PATCH] util: Remove redundant checks in the openpty() AlexChen
2020-10-31 15:21 ` Peter Maydell
2020-11-02 5:12 ` AlexChen
2020-11-02 9:50 ` Peter Maydell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).