From 4618b8f47531c4631b4fad0f7119f5ed21be434d Mon Sep 17 00:00:00 2001 From: Fancy Zhang Date: Sat, 27 Jun 2020 10:06:04 +0800 Subject: [PATCH] fix #5: segfault when program not exist --- src/cgroup_attach.cpp | 2 +- src/common.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cgroup_attach.cpp b/src/cgroup_attach.cpp index 4eb5a79..0ce0cf2 100644 --- a/src/cgroup_attach.cpp +++ b/src/cgroup_attach.cpp @@ -24,7 +24,7 @@ string get_cgroup2_mount_point() { char buf[READ_SIZE_MAX]; while (fgets(buf, READ_SIZE_MAX, fp.get()) != NULL) { buffer << buf; } string s = buffer.str(); - s.pop_back(); // remove newline character + if (!s.empty()) s.pop_back(); // remove newline character return s; } diff --git a/src/common.cpp b/src/common.cpp index 18dd4bb..8871064 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -67,7 +67,7 @@ string bash_which(const string &name) { char buf[READ_SIZE_MAX]; while (fgets(buf, READ_SIZE_MAX, fp.get()) != NULL) { buffer << buf; } string s = buffer.str(); - s.pop_back(); // remove newline character + if (!s.empty()) s.pop_back(); // remove newline character return s; } @@ -79,7 +79,7 @@ string bash_readlink(const string &path) { char buf[READ_SIZE_MAX]; while (fgets(buf, READ_SIZE_MAX, fp.get()) != NULL) { buffer << buf; } string s = buffer.str(); - s.pop_back(); // remove newline character + if (!s.empty()) s.pop_back(); // remove newline character return s; }