Check that XDG base directories paths are absolute

The [spec][1] reads:

> All paths set in these environment variables must be absolute. If an
> implementation encounters a relative path in any of these variables it should
> consider the path invalid and ignore it.

and

> If $XDG_DATA_HOME is either not set or empty, a default equal to
> $HOME/.local/share should be used.

Testing that the path is absolute also entails that is is non-empty.

[1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

Signed-off-by: Antonin Décimo <antonin.decimo@gmail.com>
This commit is contained in:
Antonin Décimo 2022-03-11 14:08:49 +01:00 committed by Simon Ser
parent f710d01663
commit 9434e8d69f
8 changed files with 13 additions and 12 deletions

View file

@ -40,7 +40,7 @@ static const char *
require_xdg_runtime_dir(void)
{
char *val = getenv("XDG_RUNTIME_DIR");
assert(val && "set $XDG_RUNTIME_DIR to run this test");
assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
return val;
}

View file

@ -40,7 +40,7 @@ static const char *
require_xdg_runtime_dir(void)
{
char *val = getenv("XDG_RUNTIME_DIR");
assert(val && "set $XDG_RUNTIME_DIR to run this test");
assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
return val;
}

View file

@ -51,7 +51,7 @@ static const char *
require_xdg_runtime_dir(void)
{
char *val = getenv("XDG_RUNTIME_DIR");
assert(val && "set $XDG_RUNTIME_DIR to run this test");
assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
return val;
}

View file

@ -180,7 +180,7 @@ set_xdg_runtime_dir(void)
xrd_env = getenv("XDG_RUNTIME_DIR");
/* if XDG_RUNTIME_DIR is not set in environ, fallback to /tmp */
assert((snprintf(xdg_runtime_dir, PATH_MAX, "%s/wayland-tests-XXXXXX",
xrd_env ? xrd_env : "/tmp") < PATH_MAX)
(xrd_env && xrd_env[0] == '/') ? xrd_env : "/tmp") < PATH_MAX)
&& "test error: XDG_RUNTIME_DIR too long");
assert(mkdtemp(xdg_runtime_dir) && "test error: mkdtemp failed");
@ -200,7 +200,7 @@ static void
rmdir_xdg_runtime_dir(void)
{
const char *xrd_env = getenv("XDG_RUNTIME_DIR");
assert(xrd_env && "No XDG_RUNTIME_DIR set");
assert(xrd_env && xrd_env[0] == '/' && "No XDG_RUNTIME_DIR set");
/* rmdir may fail if some test didn't do clean up */
if (rmdir(xrd_env) == -1)