[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Remove superfluous malloc from pk_cmd_get_next_match
|
From: |
Tim Rühsen |
|
Subject: |
Re: [PATCH] Remove superfluous malloc from pk_cmd_get_next_match |
|
Date: |
Sun, 12 Apr 2020 19:49:04 +0200 |
|
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 |
Also thanks to both of you (review and testsuite extension). Pushed.
On 12.04.20 15:47, Jose E. Marchesi wrote:
>
> I think this patch is good now.
>
> Then this is OK for master.
> Thanks for the review.
>
> Thanks to both of you :)
>
> On Sun, Apr 12, 2020 at 01:50:41PM +0200, Tim R??hsen wrote:
> 2020-04-12 Tim R??hsen <address@hidden>
>
> * src/pk-cmd.c (pk_cmd_get_next_match):
> Remove superfluous malloc within loop.
> ---
> ChangeLog | 5 +++++
> src/pk-cmd.c | 27 ++++++++++-----------------
> 2 files changed, 15 insertions(+), 17 deletions(-)
>
> diff --git a/src/pk-cmd.c b/src/pk-cmd.c
> index f6273586..d4f05fa4 100644
> --- a/src/pk-cmd.c
> +++ b/src/pk-cmd.c
> @@ -775,26 +775,19 @@ pk_cmd_shutdown (void)
> char *
> pk_cmd_get_next_match (int *idx, const char *x, size_t len)
> {
> + /* X must start with a dot */
> + if (len == 0 || *x != '.')
> + return NULL;
> +
> /* Dot commands */
> - for (;;)
> + for (const struct pk_cmd **c = dot_cmds + *idx;
> + *c != &null_cmd;
> + (*idx)++, c++)
> {
> - const struct pk_cmd **c = dot_cmds + *idx;
> - if (*c == &null_cmd)
> - break;
> -
> - /* don't forget the null terminator of name */
> - const size_t name_len = strlen ((*c)->name);
> - char *name = xmalloc (name_len + 2);
> - name[0] = '.';
> - strncpy (name+1, (*c)->name, name_len + 1);
> - if (0 != strncmp (name, x, len))
> - {
> - free (name);
> - (*idx)++;
> - continue;
> - }
> - return name;
> + if (strncmp ((*c)->name, x + 1, len - 1) == 0)
> + return pk_str_concat (".", (*c)->name, NULL);
> }
> +
> return NULL;
> }
>
> --
> 2.26.0
>
>
>
signature.asc
Description: OpenPGP digital signature