All of lore.kernel.org
 help / color / mirror / Atom feed
* [igt-dev] [PATCH i-g-t] tests/tools_test: Use readlink() properly
@ 2019-11-18 12:08 Arkadiusz Hiler
  2019-11-18 12:11 ` Chris Wilson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Arkadiusz Hiler @ 2019-11-18 12:08 UTC (permalink / raw)
  To: igt-dev; +Cc: Tomi Sarvela, Petri Latvala

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: https://gitlab.freedesktop.org/drm/igt-gpu-tools/issues/12
Signed-off-by: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
---
 tests/tools_test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tests/tools_test.c b/tests/tools_test.c
index e3f73ac2..d486afb9 100644
--- a/tests/tools_test.c
+++ b/tests/tools_test.c
@@ -77,7 +77,8 @@ static bool chdir_to_tools_dir(void)
 	igt_info("Failed to cd to %s\n", TOOLS);
 
 	/* 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 NULL */
+	if (readlink("/proc/self/exe", path, sizeof(path)-1) > 0) {
 		igt_info("/proc/self/exe point to %s, going to dirname()\n", path);
 		chdir(dirname(path));
 	}
-- 
2.23.0

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-11-18 17:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-18 12:08 [igt-dev] [PATCH i-g-t] tests/tools_test: Use readlink() properly Arkadiusz Hiler
2019-11-18 12:11 ` Chris Wilson
2019-11-18 12:43 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork
2019-11-18 17:42 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.