summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2019-02-18 19:56:28 -0600
committerSamuel Holland <samuel@sholland.org>2019-02-18 19:56:28 -0600
commit40e85cf9ed75982d65182039a2aa22436a3469ee (patch)
treea5ac12b7e1d2accd3632198384d341358b5295b3
parent70add83428603fe757098a9531a22e611a759017 (diff)
downloadgcompat-40e85cf9ed75982d65182039a2aa22436a3469ee.tar.gz
gcompat-40e85cf9ed75982d65182039a2aa22436a3469ee.tar.bz2
gcompat-40e85cf9ed75982d65182039a2aa22436a3469ee.tar.xz
gcompat-40e85cf9ed75982d65182039a2aa22436a3469ee.zip
pthread: Fix pthread_getname_np
Remove the trailing newline, and ensure the string returned is always null-terminated. Signed-off-by: Samuel Holland <samuel@sholland.org>
-rw-r--r--libgcompat/pthread.c10
1 files 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;
}