SAFE_READLINK() should return zero-terminated target buffer
According to the man(2) page of readlink(), a null byte is not appended
to the target buffer. So applications need to make sure that the target
buffer is zero-initialized, otherwise random bytes at the end of the
returned string may exist.
Currently all users of SAFE_READLINK() get it wrong. This include the
openat03, getcwd03 and open14 testcases which pass a temporary,
un-initialized on-stack buffer to readlink().
This patch fixes SAFE_READLINK to always return a zero-terminated string
and thus fixes the the failure of getcwd03 on the hppa/parisc
architecture (and probably others).
Signed-off-by: Helge Deller <deller@gmx.de>
Acked-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index bffc5a1..a6b4ff7 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -411,6 +411,13 @@
tst_brkm(TBROK | TERRNO, cleanup_fn,
"%s:%d: readlink(%s,%p,%zu) failed",
file, lineno, path, buf, bufsize);
+ } else {
+ /* readlink does not append a NUL byte to the buffer.
+ * Add it now. */
+ if ((size_t) rval < bufsize)
+ buf[rval] = '\0';
+ else
+ buf[bufsize-1] = '\0';
}
return rval;