From 5607854bcb48a36aa714ce04feda318f6eb6b36d Mon Sep 17 00:00:00 2001 From: Galaxy Date: Tue, 4 Jun 2024 17:55:29 -0700 Subject: [PATCH] Fix bug: Infinite loop in GetTgid. When reading /proc//status of short lived processes, file is successfully opened by ifstream. But before reading stream, the process disappears and leave a bad stream, then the loop is endless. Solution: Check stream by calling ifstream.bad() after reading. --- util/process_map.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util/process_map.cpp b/util/process_map.cpp index d6b88e0..89938b5 100644 --- a/util/process_map.cpp +++ b/util/process_map.cpp @@ -120,6 +120,11 @@ static int GetTgid(pid_t pid) return -1; } statusFile >> token; + if (statusFile.bad()) { + // The file may be successfully opened before while loop, + // but disappear before reading stream. + return -1; + } if (token == "Tgid:") { foundTgid = true; continue; -- Gitee