From: Petr Vorel <pvorel@suse.cz>
To: Cyril Hrubis <chrubis@suse.cz>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 1/4] lib/tst_tmpdir: Normalize user defined TMPDIR
Date: Thu, 21 Mar 2024 08:14:54 +0100 [thread overview]
Message-ID: <20240321071454.GA529433@pevik> (raw)
In-Reply-To: <Zdi8NN-jwhz-eAxP@yuki>
> Hi!
> > + if (env_tmpdir[strlen(env_tmpdir)-1] == '/') {
> > + env_tmpdir[strlen(env_tmpdir)-1] = '\0';
> > + fixed = 1;
> > + }
> > +
> > + while ((p = strstr(env_tmpdir, "//")) != NULL) {
> > + memmove(p, p + 1, strlen(env_tmpdir) - (p - env_tmpdir));
> > + fixed = 1;
> > + }
> > +
> > + if (fixed) {
> > + tst_resm(TINFO, "WARNING: Remove double or trailing slashes from TMPDIR,"
> > + " please fix your setup to: TMPDIR='%s'",
> > + env_tmpdir);
> > + }
> > +
> This whole thing can be just a single loop (beware untested):
> size_t s = 0, d = 0;
> char prev_c = 0;
> for (;;) {
> switch (env_tmpdir[s]) {
NOTE you never set prev_c, it would have to be:
if (s)
prev_c = env_tmpdir[s-1];
> case '/':
> if (prev_c != '/')
> d++;
> s++;
> break;
> case '\0':
> if (d && prev_c == '/')
> env_tmpdir[d-1] = '\0';
> break;
And also you don't break for loop, because the break above is for case, goto or
return would have to be used).
> break;
> default:
> env_tmpdir[d++] = env_tmpdir[s++];
> }
> }
Also if TMPDIR is not set, env_tmpdir = TEMPDIR; will lead to segfault.
strdup() will need to be used:
if (!env_tmpdir)
env_tmpdir = strdup(TEMPDIR);
But even with these changes I was not able to find why / is eaten:
char *env_tmpdir = getenv("TMPDIR");
size_t s = 0, d = 0;
char prev_c = 0;
if (!env_tmpdir)
env_tmpdir = strdup(TEMPDIR);
for (;;) {
if (s)
prev_c = env_tmpdir[s-1];
switch (env_tmpdir[s]) {
case '/':
if (prev_c != '/')
d++;
s++;
break;
case '\0':
if (d && prev_c == '/')
env_tmpdir[d-1] = '\0';
goto end;
break;
default:
env_tmpdir[d++] = env_tmpdir[s++];
}
}
end:
return env_tmpdir;
}
So how about using for loop? It's not that compact, but maybe easily readable.
const char *tst_get_tmpdir_root(void)
{
char *env_tmpdir = getenv("TMPDIR");
char prev_c = 0;
size_t k = 0;
if (!env_tmpdir)
env_tmpdir = strdup(TEMPDIR);
if (env_tmpdir[0] != '/') {
tst_brkm(TBROK, NULL, "You must specify an absolute "
"pathname for environment variable TMPDIR");
return NULL;
}
for (int i = 0; env_tmpdir[i] != '\0'; i++) {
if (i)
prev_c = env_tmpdir[i-1];
if (env_tmpdir[i] != '/' || prev_c != '/')
env_tmpdir[k++] = env_tmpdir[i];
}
env_tmpdir[k] = '\0';
if (env_tmpdir[k-1] == '/')
env_tmpdir[k-1] = '\0';
return env_tmpdir;
}
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2024-03-21 7:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-07 16:06 [LTP] [PATCH 0/4] Remove double or trailing slashes in TMPDIR in C API Petr Vorel
2024-02-07 16:06 ` [LTP] [PATCH 1/4] lib/tst_tmpdir: Normalize user defined TMPDIR Petr Vorel
2024-02-07 16:17 ` Petr Vorel
2024-02-23 15:39 ` Cyril Hrubis
2024-03-21 7:14 ` Petr Vorel [this message]
2024-02-23 15:41 ` Cyril Hrubis
2024-02-07 16:06 ` [LTP] [PATCH 2/4] lib: Add test for tst_tmpdir Petr Vorel
2024-02-07 16:18 ` Petr Vorel
2024-02-07 16:06 ` [LTP] [PATCH 3/4] tst_test.sh: Print warning if slashes in $TMPDIR Petr Vorel
2024-02-07 16:06 ` [LTP] [PATCH 4/4] lib: Improve doc related to $TMPDIR default value Petr Vorel
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=20240321071454.GA529433@pevik \
--to=pvorel@suse.cz \
--cc=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