about summary refs log tree commit diff
path: root/sysdeps
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps')
-rw-r--r--sysdeps/unix/sysv/linux/ttyname.c12
-rw-r--r--sysdeps/unix/sysv/linux/ttyname_r.c5
2 files changed, 14 insertions, 3 deletions
diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c
index 20c794daf4..0dd9c704af 100644
--- a/sysdeps/unix/sysv/linux/ttyname.c
+++ b/sysdeps/unix/sysv/linux/ttyname.c
@@ -112,6 +112,7 @@ ttyname (fd)
   int dostat = 0;
   char *name;
   int save = errno;
+  int len;
 
   if (!__isatty (fd))
     return NULL;
@@ -130,10 +131,17 @@ ttyname (fd)
 	}
     }
 
-  if (__readlink (procname, buf, buflen) != -1
+  len = __readlink (procname, buf, buflen);
+  if (len != -1
       /* This is for Linux 2.0.  */
       && buf[0] != '[')
-    return buf;
+    {
+      if (len >= buflen)
+	return NULL;
+      /* readlink need not terminate the string.  */
+      buf[len] = '\0';
+      return buf;
+    }
 
   if (__fxstat (_STAT_VER, fd, &st) < 0)
     return NULL;
diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c
index b92c712fcc..9a0ce4cea9 100644
--- a/sysdeps/unix/sysv/linux/ttyname_r.c
+++ b/sysdeps/unix/sysv/linux/ttyname_r.c
@@ -134,7 +134,10 @@ __ttyname_r (fd, buf, buflen)
 
   ret = __readlink (procname, buf, buflen - 1);
   if (ret != -1 && buf[0] != '[')
-    return 0;
+    {
+      buf[ret] = '\0';
+      return 0;
+    }
   if (ret == -1 && errno == ENAMETOOLONG)
     {
       __set_errno (ERANGE);