[LTP] [PATCH v4 3/5] shell: add kconfig parse api

Li Wang liwang@redhat.com
Tue Jan 11 08:52:56 CET 2022


On Tue, Jan 11, 2022 at 2:10 PM Yang Xu <xuyang2018.jy@fujitsu.com> wrote:

> +++ b/testcases/lib/tst_check_kconfigs.c
> @@ -0,0 +1,46 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/* Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.*/
> +
> +#include <stdio.h>
> +#include <string.h>
> +#include <stdlib.h>
> +#include "tst_kconfig.h"
> +
> +int main(int argc, char *argv[])
> +{
> +       char *str = argv[1];
> +       char *delim = argv[2];
> +       unsigned int i, cnt = 1;
> +       int ret = 0;
> +
> +       if (argc == 2 || strlen(delim) == 0) {

I doubt that this syntax really works here.

How can we get the strlen(delim) equal to 0? if the argc is
not 2, why the length of delimi is zero? but if we change ||
to &&, then get a segment fault. I don't understand this.

> +               delim = ",";
> +       } else if (argc == 3) {
> +               if (strlen(delim) > 1) {
> +                       fprintf(stderr, "The delim must be a single character\n");
> +                       return 1;
> +               }
> +       } else {
> +               fprintf(stderr, "Please provide kernel kconfig list and delim "
> +                               "(optinal, default value is ',')\n");
> +               return 1;
> +       }
> +
> +       for (i = 0; str[i]; i++) {
> +               if (str[i] == delim[0])
> +                       cnt++;
> +       }
> +
> +       char **kconfigs = malloc(++i * sizeof(char *));

Shouldn't this be malloc(++cnt * sizeof(char*)) ?

> +
> +       for (i = j0; i < cnt; i++)
> +               kconfigs[i] = strtok_r(str, delim, &str);
> +
> +       kconfigs[i] = NULL;
> +
> +       if (tst_kconfig_check((const char * const*)kconfigs))
> +               ret = 1;
> +
> +       free(kconfigs);
> +       return ret;
> +}

-- 
Regards,
Li Wang



More information about the ltp mailing list