From 12bcf2228f4668a058b39939779c9590494c3b4a Mon Sep 17 00:00:00 2001 From: liberize Date: Mon, 9 Dec 2024 21:28:28 +0800 Subject: [PATCH] optimize --- src/untraceable.h | 7 +++---- src/utils.h | 6 +++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/untraceable.h b/src/untraceable.h index d6e9921..da41706 100644 --- a/src/untraceable.h +++ b/src/untraceable.h @@ -56,11 +56,10 @@ FORCE_INLINE void check_debugger(bool full, bool parent) { snprintf(path, sizeof(path), OBF("/proc/%d/status"), parent ? getppid() : getpid()); std::ifstream ifs(path); std::string line, needle = OBF("TracerPid:\t"); - int tracer_pid = 0; + pid_t tracer_pid = 0; while (std::getline(ifs, line)) { - auto idx = line.find(needle); - if (idx != std::string::npos) { - tracer_pid = atoi(line.c_str() + idx + needle.size()); + if (str_starts_with(line, needle)) { + tracer_pid = atoi(line.c_str() + needle.size()); break; } } diff --git a/src/utils.h b/src/utils.h index 912610c..25db55a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -66,6 +66,10 @@ FORCE_INLINE std::string base_name(const std::string& s) { return s.substr(s.find_last_of("\\/") + 1); } +FORCE_INLINE bool str_starts_with(const std::string& s, const std::string& e) { + return s.size() >= e.size() && s.compare(0, e.size(), e) == 0; +} + FORCE_INLINE bool str_ends_with(const std::string& s, const std::string& e) { return s.size() >= e.size() && s.compare(s.size() - e.size(), e.size(), e) == 0; } @@ -180,7 +184,7 @@ FORCE_INLINE void check_pipe_reader(int fd) { if (entry->d_type != DT_DIR) continue; char *end = nullptr; - auto pid = strtoul(entry->d_name, &end, 10); + auto pid = strtol(entry->d_name, &end, 10); if (!pid || *end) continue; if (pid == mypid || pid == ppid)