From d867bd612097979ca40b07852abbf7332538ab98 Mon Sep 17 00:00:00 2001 From: "Niall Douglas (s [underscore] sourceforge {at} nedprod [dot] com)" Date: Mon, 19 Jul 2021 17:43:22 +0100 Subject: [PATCH] Fix potential write outside of array in dynamic_thread_pool_group's native Linux implementation. --- .../llfio/v2.0/detail/impl/dynamic_thread_pool_group.ipp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/llfio/v2.0/detail/impl/dynamic_thread_pool_group.ipp b/include/llfio/v2.0/detail/impl/dynamic_thread_pool_group.ipp index 4192306ca..a476496b3 100644 --- a/include/llfio/v2.0/detail/impl/dynamic_thread_pool_group.ipp +++ b/include/llfio/v2.0/detail/impl/dynamic_thread_pool_group.ipp @@ -850,6 +850,7 @@ namespace detail int fd = ::open(path, O_RDONLY); if(-1 == fd) { + threadearlyexited: // Thread may have exited since we last populated if(item->blocked_since == std::chrono::steady_clock::time_point()) { @@ -863,7 +864,11 @@ namespace detail char buffer[1024]; auto bytesread = ::read(fd, buffer, sizeof(buffer)); ::close(fd); - buffer[std::max((size_t) bytesread, sizeof(buffer) - 1)] = 0; + if(bytesread <= 0) + { + goto threadearlyexited; + } + buffer[std::min((size_t) bytesread, sizeof(buffer) - 1)] = 0; char state = 0; unsigned long majflt = 0, utime = 0, stime = 0; sscanf(buffer, "%*d %*s %c %*d %*d %*d %*d %*d %*u %*u %*u %lu %*u %lu %lu", &state, &majflt, &utime, &stime);