Re: [PATCH v4 6/8] perf tests: Use scandirat for shell script finding

From: Namhyung Kim
Date: Fri Feb 16 2024 - 15:22:44 EST


On Tue, Feb 13, 2024 at 8:54 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> Avoid filename appending buffers by using openat, faccessat and
> scandirat more widely. Turn the script's path back to a file name
> using readlink from /proc/<pid>/fd/<fd>.
>
> Read the script's description using api/io.h to avoid fdopen
> conversions. Whilst reading perform additional sanity checks on the
> script's contents.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
> tools/perf/tests/builtin-test.c | 21 ++---
> tools/perf/tests/tests-scripts.c | 144 ++++++++++++++++++-------------
> tools/perf/tests/tests-scripts.h | 1 -
> 3 files changed, 95 insertions(+), 71 deletions(-)
>
> diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> index eff3c62e9b47..6d5001daaf63 100644
> --- a/tools/perf/tests/builtin-test.c
> +++ b/tools/perf/tests/builtin-test.c
> @@ -300,22 +300,20 @@ static int test_and_print(struct test_suite *t, int subtest)
> }
>
> struct shell_test {
> - const char *dir;
> const char *file;
> };
>
> static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
> {
> int err;
> - char script[PATH_MAX];
> struct shell_test *st = test->priv;
> + char *cmd;
>
> - path__join(script, sizeof(script) - 3, st->dir, st->file);
> -
> - if (verbose > 0)
> - strncat(script, " -v", sizeof(script) - strlen(script) - 1);
> -
> - err = system(script);
> + asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "");
> + if (!cmd)
> + return TEST_FAIL;

It fails to build.

tests/tests-scripts.c: In function ‘shell_test__run’:
tests/tests-scripts.c:130:9: error: ignoring return value of
‘asprintf’ declared with attribute ‘warn_unused_result’
[-Werror=unused-result]
130 | asprintf(&cmd, "%s%s", file, verbose ? " -v" : "");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks,
Namhyung


> + err = system(cmd);
> + free(cmd);
> if (!err)
> return TEST_OK;
>