简体   繁体   English

当stderr重定向到管道时,为什么boost :: process在Windows上崩溃?

[英]Why does boost::process crash on Windows when stderr is redirected to a pipe?

This code based on boost::process 1.65.1 samples with some spelling errors fixed: 此代码基于boost::process 1.65.1示例,修复了一些拼写错误:

#include <boost/process.hpp>

int main(int argc, char *argv[])
{
    boost::asio::io_service ios;
    std::future<std::vector<char>> output, error;
    boost::process::child c("hostname.exe",
        boost::process::std_out > output,
        boost::process::std_err > boost::process::null,
        ios);
    ios.run();
    c.wait();
    if (output.valid())
    {
        auto processOutput = output.get();
        processOutput.push_back('\0');
        printf(processOutput.data());
    }
    if (error.valid())
    {
        auto processError = error.get();
        processError.push_back('\0');
        printf(processError.data());
    }
    return 0;
}

It works as expected. 它按预期工作。 It runs hostname.exe and prints its output, ie your computer network name. 它运行hostname.exe并打印其输出,即您的计算机网络名称。

You probably see that error variable is unused. 您可能会看到error变量未使用。 It is logical to read also stderr , even hostname.exe doesn't use it usually, realistic child process may of course use it. 读取stderr也是合乎逻辑的,即使hostname.exe通常也不使用它,现实的子进程当然可以使用它。 It looks trivial to just replace boost::process::std_err > boost::process::null with boost::process::std_err > error . boost::process::std_err > error替换boost::process::std_err > boost::process::null看起来很简单。 However, it results in crash. 但是,它会导致崩溃。 The error message follows: 错误消息如下:

vector iterator not dereferencable

Does anyone know the reason and how to bypass this bug? 有谁知道原因以及如何绕过这个bug?

It is a bug in Boost Process. 这是Boost Process中的一个错误。 The crash appears at include\\boost\\process\\detail\\windows\\async_out.hpp The code that causes crash follows: 崩溃出现在include\\boost\\process\\detail\\windows\\async_out.hpp导致崩溃的代码如下:

arg.resize(buffer->size());
is.read(&*arg.begin(), buffer->size());

It is not important whether we redirect std_err or std_out . 重定向std_errstd_out并不重要。 The problem appears when stream doesn't generate any output. 当流不生成任何输出时,会出现此问题。 For hostname.exe it is std_err , however, if you will try to run pyw.exe , you'll get this error for std_out also. 对于hostname.exe它是std_err ,但是,如果您尝试运行pyw.exe ,则std_out也会出现此错误。

When progam generates no output, buffer->size() returns 0, so the vector becomes an empty one. 当progam不生成输出时, buffer->size()返回0,因此向量变为空向量。 And it looks like attempt to dereference an empty vector results in crash (debug assertion) at Visual C++. 看起来尝试取消引用空向量会导致Visual C ++崩溃(调试断言)。

The fix follows: 修复如下:

auto bufferSize = buffer->size();
if (bufferSize) {
    arg.resize(buffer->size());
    is.read(&*arg.begin(), buffer->size());
}

I didn't submit this bug report to boost, if anyone has time, lets he does it. 我没有提交这个错误报告来提升,如果有人有时间,就让他去做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM