From 40e85cf9ed75982d65182039a2aa22436a3469ee Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Mon, 18 Feb 2019 19:56:28 -0600 Subject: pthread: Fix pthread_getname_np Remove the trailing newline, and ensure the string returned is always null-terminated. Signed-off-by: Samuel Holland --- libgcompat/pthread.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libgcompat/pthread.c b/libgcompat/pthread.c index 0456c40..f15e9f1 100644 --- a/libgcompat/pthread.c +++ b/libgcompat/pthread.c @@ -38,15 +38,17 @@ weak_alias(__register_atfork, register_atfork); int pthread_getname_np(pthread_t thread, char *name, size_t len) { int fd = open("/proc/thread-self/comm", O_RDONLY | O_CLOEXEC); - char dummy; + ssize_t n; if (fd < 0) return errno; - if (read(fd, name, len) < 0) + n = read(fd, name, len); + if (n < 0) return errno; - /* If there's more to read, the buffer was too small. */ - if (read(fd, &dummy, 1) > 0) + /* If the trailing newline was not read, the buffer was too small. */ + if (n == 0 || name[n - 1] != '\n') return ERANGE; + name[n - 1] = '\0'; return 0; } -- cgit v1.2.3-60-g2f50