Re: [hackers] [sbase][PATCH] Minor optimizations for 'yes'

From: Michael Forney <mforney_AT_mforney.org>
Date: Thu, 27 Jun 2019 19:25:32 -0700

On 2019-06-27, aidanwillie0317 <aidanwillie0317_AT_protonmail.com> wrote:
> Or, even simpler:

Thanks, this looks good. Only a couple minor comments.

Could you update the man page to reflect the fact that now only one
string is supported?

> ---
> yes.c | 10 +++-------
> 1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/yes.c b/yes.c
> index dd97ea6..5f3b2e1 100644
> --- a/yes.c
> +++ b/yes.c
> _AT_@ -6,14 +6,10 @@
> int
> main(int argc, char *argv[])
> {
> - char **p;
> + char *p = argv[1] ? argv[1] : "y";
>
> - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;

This line is actually necessary. The C standard only guarantees that
argc is non-negative, and argv[argc] is a null pointer[0]. In this
case, argv[1] would be an access beyond the end of the argv array.
Since we technically don't print argv0 anywhere (either directly, or
through eprintf), we could get around this by just checking `argc >
1`, but for consistency with the rest of the tools I would suggest

        argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
        p = argc ? argv[0] : "y";

[0] http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p2
Received on Fri Jun 28 2019 - 04:25:32 CEST

This archive was generated by hypermail 2.3.0 : Fri Jun 28 2019 - 04:36:25 CEST