From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [RFC PATCH 2/3] lib: Add $LTPROOT/testcases/bin into PATH
Date: Wed, 16 Jun 2021 11:57:38 +0200 [thread overview]
Message-ID: <YMnLEjDAVQqwH7Wq@yuki> (raw)
In-Reply-To: <20210615163307.10755-3-pvorel@suse.cz>
Hi!
> diff --git a/lib/tst_test.c b/lib/tst_test.c
> index 36a4809c7..14a739eb5 100644
> --- a/lib/tst_test.c
> +++ b/lib/tst_test.c
> @@ -1149,15 +1149,31 @@ static unsigned long long get_time_ms(void)
> static void add_paths(void)
> {
> char *old_path = getenv("PATH");
> + const char *ltproot = getenv("LTPROOT");
> const char *start_dir;
> - char *new_path;
> + char *bin_dir, *new_path = NULL;
>
> start_dir = tst_get_startwd();
>
> + /* : - current directory */
> if (old_path)
> - SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
> + SAFE_ASPRINTF(&new_path, "%s:", old_path);
> else
> - SAFE_ASPRINTF(&new_path, "::%s", start_dir);
> + strcat(new_path, ":");
I personally think that strcat() function is broken be desing and that
we should avoid using it.
Also in this place is guaranteed SEGFAULT since you strcat() to NULL.
> + /* empty for library C API tests */
> + if (strlen(start_dir) > 0)
> + SAFE_ASPRINTF(&new_path, "%s:%s", new_path, start_dir);
^
This is a memory leak.
As far as I can tell the asprintf() does not free th strp if non-NULL.
> + if (ltproot) {
> + SAFE_ASPRINTF(&bin_dir, "%s/testcases/bin", ltproot);
> +
> + if (access(bin_dir, F_OK) == -1)
> + tst_res(TWARN, "'%s' does not exist, is $LTPROOT set correctly?",
> + bin_dir);
> + else
> + SAFE_ASPRINTF(&new_path, "%s:%s", new_path, bin_dir);
^
And this as well.
> + }
I guess that we can also fairly simplify the code by expecting that PATH
is never unset from the start, maybe we should just check it and WARN if
it's not. Also we can assume that if LTPROOT is set we do not have to
add the start_dir since the start_dir is only useful when tests are
executed from the git checkout.
Something as:
const char *old_path = getenv("PATH");
const char *ltproot = getenv("LTPROOT");
const char *start_dir = tst_get_startwd();
char *new_path;
if (!old_path) {
tst_res(TWARN, "\$PATH not set!");
return;
}
if (ltproot)
SAFE_ASPRINTF(&new_path, "%s::%s/testcases/bin/", old_path, ltproot);
else
SAFE_ASPRINTF(&new_path, "%s::%s", old_path, start_dir);
> SAFE_SETENV("PATH", new_path, 1);
> free(new_path);
> --
> 2.32.0
>
--
Cyril Hrubis
chrubis@suse.cz
next prev parent reply other threads:[~2021-06-16 9:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-15 16:33 [LTP] [RFC PATCH 0/3] C, shell API: Add $LTPROOT/testcases/bin into PATH Petr Vorel
2021-06-15 16:33 ` [LTP] [RFC PATCH 1/3] tst_test.sh: " Petr Vorel
2021-06-15 16:33 ` [LTP] [RFC PATCH 2/3] lib: " Petr Vorel
2021-06-16 9:57 ` Cyril Hrubis [this message]
2021-06-16 10:42 ` Petr Vorel
2021-06-15 16:33 ` [LTP] [RFC PATCH 3/3] doc: Update LTPROOT and PATH environment variables Petr Vorel
2021-06-16 9:42 ` Li Wang
2021-06-16 10:01 ` Petr Vorel
2021-06-16 10:21 ` Li Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YMnLEjDAVQqwH7Wq@yuki \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox