Skip to content
Snippets Groups Projects
Commit a7001318 authored by Arkadiusz Hiler's avatar Arkadiusz Hiler
Browse files

tests/tools_test: Use readlink() properly

Turns out that `readlink()` does not NULL-terminate the string and since
it resides on the stack we may end up with some junk:

  `/proc/self/exe point to /opt/igt/libexec/igt-gpu-tools/tools_test7<CD>\t<8A>^?`

That in turn confuses `dirname()` and we end up doing `chdir("/")`,
which explain the sporadic failures of this test where it was not able
to locate the tools.

Let's zero out the variable first and allow `readlink()` to write at
most `sizeof()-1` bytes to it, so it is always properly terminated.

Cc: Petri Latvala <petri.latvala@intel.com>
Cc: Tomi Sarvela <tomi.p.sarvela@intel.com>
Issue: #12


Signed-off-by: default avatarArkadiusz Hiler <arkadiusz.hiler@intel.com>
Reviewed-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
parent 4bb46f08
No related branches found
No related tags found
No related merge requests found
Pipeline #80424 passed
...@@ -77,7 +77,8 @@ static bool chdir_to_tools_dir(void) ...@@ -77,7 +77,8 @@ static bool chdir_to_tools_dir(void)
igt_info("Failed to cd to %s\n", TOOLS); igt_info("Failed to cd to %s\n", TOOLS);
/* Try TOOLS and install dir relative to test binary */ /* Try TOOLS and install dir relative to test binary */
if (readlink("/proc/self/exe", path, sizeof(path)) > 0) { memset(path, 0, sizeof(path)); /* readlink() does not append NUL */
if (readlink("/proc/self/exe", path, sizeof(path)-1) > 0) {
igt_info("/proc/self/exe point to %s, going to dirname()\n", path); igt_info("/proc/self/exe point to %s, going to dirname()\n", path);
chdir(dirname(path)); chdir(dirname(path));
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment