[LTP] [PATCH] Add terminating NUL after calling readlink()

Helge Deller deller@gmx.de
Sat May 6 10:52:06 CEST 2017


readlink() does not append a NUL byte to the target buffer. Fix it in
the pidns03 and fsstress testcases to avoid wrong test results.

Signed-off-by: Helge Deller <deller@gmx.de>

diff --git a/testcases/kernel/containers/pidns/pidns03.c b/testcases/kernel/containers/pidns/pidns03.c
index c4dca66d0..553c65aad 100644
--- a/testcases/kernel/containers/pidns/pidns03.c
+++ b/testcases/kernel/containers/pidns/pidns03.c
@@ -63,7 +63,7 @@ int child_func(void *arg)
 	}
 
 	/* self is symlink to directory named after current pid number */
-	if (readlink(PROCDIR"/self", buf, sizeof(buf)) == -1) {
+	if (SAFE_READLINK(NULL, PROCDIR"/self", buf, sizeof(buf)) == -1) {
 		perror("readlink");
 		umount(PROCDIR);
 		return 1;
diff --git a/testcases/kernel/fs/fsstress/fsstress.c b/testcases/kernel/fs/fsstress/fsstress.c
index 629bcf4d1..bfdffd52b 100644
--- a/testcases/kernel/fs/fsstress/fsstress.c
+++ b/testcases/kernel/fs/fsstress/fsstress.c
@@ -1143,6 +1143,8 @@ int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz)
 	int rval;
 
 	rval = readlink(name->path, lbuf, lbufsiz);
+	if (rval >= 0)
+		lbuf[rval] = 0; /* add terminating NUL */
 	if (rval >= 0 || errno != ENAMETOOLONG)
 		return rval;
 	separate_pathname(name, buf, &newname);


More information about the ltp mailing list