提交 e6e67c4b 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!4561 Remove `strerror()` function and check string size before use `substr()`.

Merge pull request !4561 from ZhangQinghua/master1
...@@ -90,11 +90,18 @@ class KernelBuildClient { ...@@ -90,11 +90,18 @@ class KernelBuildClient {
std::string res; std::string res;
*dp_ >> res; *dp_ >> res;
// Filter out the interference // Filter out the interference
if (res.empty()) {
MS_LOG(EXCEPTION) << "Response is empty";
}
auto start = res.find(kTag); auto start = res.find(kTag);
if (start == std::string::npos) { if (start == std::string::npos) {
MS_LOG(EXCEPTION) << "Response seems incorrect, res: " << res; MS_LOG(EXCEPTION) << "Response seems incorrect, res: " << res;
} }
res = res.substr(start + std::strlen(kTag), res.size() - start); auto pos = start + std::strlen(kTag);
if (pos > res.size()) { // Safe check for codedex
MS_LOG(EXCEPTION) << "Response seems incorrect, res(" << res.size() << "): {" << res << "}, start: " << start;
}
res = res.substr(pos);
// Revert the line feed and space // Revert the line feed and space
if (res != kSuccess && res != kAck && res != kErr && res != kTrue) { if (res != kSuccess && res != kAck && res != kErr && res != kTrue) {
ReplaceStr(&res, kLF, '\n'); ReplaceStr(&res, kLF, '\n');
...@@ -113,25 +120,30 @@ class KernelBuildClient { ...@@ -113,25 +120,30 @@ class KernelBuildClient {
std::shared_ptr<DuplexPipe> dp_; std::shared_ptr<DuplexPipe> dp_;
}; };
static inline std::string GetScriptFilePath(const std::string cmd_env, const std::string &cmd_script) { static std::string GetScriptFilePath(const std::string cmd_env, const std::string &cmd_script) {
std::string cmd = cmd_env; std::string cmd = cmd_env;
(void)cmd.append(1, ' ').append(cmd_script); (void)cmd.append(1, ' ').append(cmd_script);
FILE *fpipe = popen(cmd.c_str(), "r"); FILE *fpipe = popen(cmd.c_str(), "r");
if (fpipe == nullptr) { if (fpipe == nullptr) {
MS_LOG(EXCEPTION) << "popen failed, " << strerror(errno) << "(" << errno << ")"; MS_LOG(EXCEPTION) << "popen failed, errno: " << errno;
} }
bool start = false; bool start = false;
std::string result; std::string result;
char buf[kBufferSize]; char buf[kBufferSize];
while (std::fgets(buf, sizeof(buf), fpipe) != nullptr) { while (std::fgets(buf, sizeof(buf), fpipe) != nullptr) {
auto len = std::strlen(buf);
if (len == 0 || len >= kBufferSize) {
// Safe check for codedex
// Should never reach here
MS_LOG(EXCEPTION) << "fgets() failed, len: " << len << ", errno: " << errno;
}
if (std::strncmp(buf, kTag, std::strlen(kTag)) == 0) { if (std::strncmp(buf, kTag, std::strlen(kTag)) == 0) {
start = true; start = true;
} }
// Filter with 'kTAG' and '\n' // Filter with 'kTAG' and '\n'
if (start) { if (start) {
auto size = std::strlen(buf); bool line_end = buf[len - 1] == '\n';
bool line_end = buf[size - 1] == '\n'; result.append(buf, line_end ? len - 1 : len);
result.append(buf, line_end ? size - 1 : size);
if (line_end) { if (line_end) {
break; break;
} }
...@@ -142,6 +154,9 @@ static inline std::string GetScriptFilePath(const std::string cmd_env, const std ...@@ -142,6 +154,9 @@ static inline std::string GetScriptFilePath(const std::string cmd_env, const std
if (result.empty() || result.rfind(py_suffix) != (result.length() - py_suffix.length())) { if (result.empty() || result.rfind(py_suffix) != (result.length() - py_suffix.length())) {
MS_LOG(EXCEPTION) << "py file seems incorrect, result: {" << result << "}"; MS_LOG(EXCEPTION) << "py file seems incorrect, result: {" << result << "}";
} }
if (strlen(kTag) > result.size()) { // Safe check for codedex
MS_LOG(EXCEPTION) << "result size seems incorrect, result(" << result.size() << "): {" << result << "}";
}
result = result.substr(strlen(kTag)); result = result.substr(strlen(kTag));
MS_LOG(DEBUG) << "result: " << result; MS_LOG(DEBUG) << "result: " << result;
return result; return result;
......
...@@ -24,12 +24,12 @@ ...@@ -24,12 +24,12 @@
namespace mindspore { namespace mindspore {
int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fds) { int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fds) {
if (pipe(fd1_) == -1) { if (pipe(fd1_) == -1) {
DP_EXCEPTION << "pipe 1 failed, " << strerror(errno) << "(" << errno << ")"; DP_EXCEPTION << "pipe 1 failed, errno: " << errno;
} }
if (pipe(fd2_) == -1) { if (pipe(fd2_) == -1) {
close(fd1_[0]); close(fd1_[0]);
close(fd1_[1]); close(fd1_[1]);
DP_EXCEPTION << "pipe 2 failed, " << strerror(errno) << "(" << errno << ")"; DP_EXCEPTION << "pipe 2 failed, errno: " << errno;
} }
pid_ = fork(); pid_ = fork();
...@@ -38,7 +38,7 @@ int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fd ...@@ -38,7 +38,7 @@ int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fd
close(fd1_[1]); close(fd1_[1]);
close(fd2_[0]); close(fd2_[0]);
close(fd2_[1]); close(fd2_[1]);
DP_EXCEPTION << "fork failed, " << strerror(errno) << "(" << errno << ")"; DP_EXCEPTION << "fork failed, errno: " << errno;
} else if (pid_ == 0) { // Remote process } else if (pid_ == 0) { // Remote process
DP_INFO << "Remote process, pid: " << getpid() << ", " << fd1_[0] << "/" << fd2_[1]; DP_INFO << "Remote process, pid: " << getpid() << ", " << fd1_[0] << "/" << fd2_[1];
remote_stdout_ = dup(STDOUT_FILENO); remote_stdout_ = dup(STDOUT_FILENO);
...@@ -61,7 +61,7 @@ int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fd ...@@ -61,7 +61,7 @@ int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fd
} }
args.emplace_back(nullptr); args.emplace_back(nullptr);
if (execvp(args[0], const_cast<char *const *>(&args[0])) == -1) { if (execvp(args[0], const_cast<char *const *>(&args[0])) == -1) {
DP_EXCEPTION << "execute " << args[0] << " failed, " << strerror(errno) << "(" << errno << ")"; DP_EXCEPTION << "execute " << args[0] << " failed, errno: " << errno;
} }
} else { // Local process } else { // Local process
DP_INFO << "Local process, id: " << getpid() << ", " << fd2_[0] << "/" << fd1_[1]; DP_INFO << "Local process, id: " << getpid() << ", " << fd2_[0] << "/" << fd1_[1];
...@@ -77,13 +77,13 @@ int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fd ...@@ -77,13 +77,13 @@ int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fd
void DuplexPipe::Write(const std::string &buf, bool flush) { void DuplexPipe::Write(const std::string &buf, bool flush) {
// Write the string into pipe // Write the string into pipe
if (write(fd1_[1], buf.data(), buf.size()) == -1) { if (write(fd1_[1], buf.data(), buf.size()) == -1) {
DP_ERROR << "write failed, error: " << strerror(errno) << "(" << errno << ")"; DP_ERROR << "write failed, errno: " << errno;
return; return;
} }
if (flush) { if (flush) {
// Flush into the pipe // Flush into the pipe
if (write(fd1_[1], "\n", 1) == -1) { if (write(fd1_[1], "\n", 1) == -1) {
DP_ERROR << "write failed, error: " << strerror(errno) << "(" << errno << ")"; DP_ERROR << "write failed, errno: " << errno;
return; return;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册