【问题标题】:boost asio ssl stream socket compilation issueboost asio ssl流套接字编译问题
【发布时间】:2011-02-13 21:18:30
【问题描述】:

在 c++ 应用程序中使用 boost 1.4.2 asio 并收到我不理解的 linux 编译器警告。

还在吗?
我正在开发的应用程序需要一个“套接字”,它可能是 ssl 套接字或常规 tcp 套接字,所以我们隐藏了细节 在采用 ssl 套接字类或 tcp 套接字类作为模板参数 - 下面是 ssl 类代码。
应用程序正常运行,未开启任何优化;问题是当我在 linux g++ 4.4.1 下编译时 并在 -02 或更高时打开优化,打开 -fstrict-aliasing 标志。 编译会导致严格的别名警告,如下所示: “警告:取消引用类型双关指针将破坏严格的别名规则” 我在任何地方取消引用 _psock(例如 _psock->handshake)

我想知道为什么会发出这个警告,它是否表示设计问题...

class socket_ssl_cli
{
public:

    typedef ba::ssl::stream<ba::ip::tcp::socket> socket_type;
    socket_ssl_cli(ba::io_service& io_svc, SocketConfig & sockcfg)
        : _io_svc(io_svc)
        , _ctxt(_io_svc, ba::ssl::context::tlsv1)
        , _psock(0)
    {
        try {
            // the one and only ssl context
            // hardcoded for test, but get these values from config
            _ctxt.set_options(ba::ssl::context::default_workarounds | 
                                ba::ssl::context::verify_none);

            _psock = new socket_type(_io_svc, _ctxt);
        } catch (bs::system_error & x) {
            throw std::runtime_error(x.code().message());
        }

    }

    ~socket_ssl_cli() 
    { 
        if (_psock) {
            bs::error_code ec;
            close_socket(ec); 
            delete _psock; 
        }
    }

    socket_type & raw_socket() { return *_psock; }

    void setup(bs::error_code & ec)
    {
        _psock->handshake(ba::ssl::stream_base::client, ec);
    }

    void close_socket(bs::error_code & ec) 
    { 
        // shut down ssl, then shutdown socket, then close socket
        _psock->shutdown(ec);
        _psock->lowest_layer().shutdown(ba::socket_base::shutdown_both, ec);
        _psock->lowest_layer().close(ec);
    }

private:

    ba::io_service & _io_svc;
    ba::ssl::context _ctxt;
    socket_type * _psock;
};

所有痛苦的编译输出都打开了 -02,导致 -fstrict-aliasing

../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:321:警告:取消引用类型双关指针将破坏严格别名规则 ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:325:警告:取消引用类型双关指针将破坏严格别名规则 ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:在静态成员函数'static void boost::detail::function::functor_manager_common::manage_small (const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >]': ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:360: 从 'static void boost::detail::function::functor_manager::manager 实例化(const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type, mpl_::true_) [with Functor = boost::_bi:: bind_t > > >, boost::_bi::list1 > >*> > >]' ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:406: 从 'static void boost::detail::function::functor_manager::manager 实例化(const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type, boost::detail::function::function_obj_tag) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >]' ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:434: 从 'static void boost::detail::function::functor_manager::manage 实例化(const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:913: 从 'void boost::function0::assign_to(Functor) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:722:从 'boost::function0::function0(Functor, typename boost::enable_if_c ::type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:1064: 从'boost::function::function(Functor, typename boost::enable_if_c ::type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:1105: 从 'typename boost::enable_if_c&>::type boost::function:: 实例化operator=(Functor) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/detail/openssl_operation.hpp:134:从'boost::asio::ssl::detail: :openssl_operation::openssl_operation(boost::asio::ssl::detail::ssl_primitive_func, Stream&, boost::asio::ssl::detail::net_buffer&, SSL*, BIO*) [with Stream = boost::asio ::basic_stream_socket >]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/detail/openssl_stream_service.hpp:510:从'boost::system::error_code boost::asio实例化::ssl::detail::openssl_stream_service::handshake(boost::asio::ssl::detail::openssl_stream_service::impl_struct*&, Stream&, boost::asio::ssl::stream_base::handshake_type, boost: :system::error_code&) [with Stream = boost::asio::basic_stream_socket >]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/stream_service.hpp:100:从'boost::system::error_code boost::asio::实例化ssl::stream_service::handshake(boost::asio::ssl::detail::openssl_stream_service::impl_struct*&, Stream&, boost::asio::ssl::stream_base::handshake_type, boost::system::error_code& ) [与 Stream = boost::asio::basic_stream_socket >]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/stream.hpp:207:从 'boost::system::error_code boost::asio:: 实例化ssl::stream::handshake(boost::asio::ssl::stream_base::handshake_type, boost::system::error_code&) [with Stream = boost::asio::basic_stream_socket >, Service = boost::asio: :ssl::stream_service]' ../sockets/socket_ssl_cli.h:45:从这里实例化

【问题讨论】:

    标签: c++ g++ boost-asio


    【解决方案1】:

    阅读解释编译器警告指南的 boost wiki

    https://svn.boost.org/trac/boost/wiki/Guidelines/WarningsGuidelines

    他们有一整节专门用于 -fstrict-aliasing

    -fstrict-aliasing - 也被 -O2、-O3 和 -Os 打开。告诉编译器可以根据表达式的类型进行某类优化。特别是,您通过使用此标志来保证一种类型的对象不会与不兼容类型的对象驻留在同一地址。 -fno-strict-aliasing - 关闭此优化。如果这改变了您代码的行为,那么您的代码就有问题。

    这试图让您知道您正在要求编译器执行未定义的行为,并且它可能不会执行您认为它会执行的操作。随着优化级别的提高,您不喜欢它所做的事情的可能性也会增加。稍后我将展示一个简单的示例,当打开任何级别的优化时,它会出人意料地生成错误的结果。忽略此警告,后果自负。您不太可能关心导致的未定义行为。

    【讨论】:

      【解决方案2】:

      我在 gcc-4.4.4 (Debian Squeeze) 上遇到了类似的问题。我浏览了与此相关的 Boost 邮件列表 thread。但是,最后我仍然对这是谁的问题感到困惑。无论是 GCC 的还是 Boost 的。 :-S 我在我的代码中使用了 -fno-strict-aliasing。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        • 2022-12-11
        • 2021-11-28
        • 2011-10-15
        • 2011-12-05
        • 1970-01-01
        • 2013-02-25
        相关资源
        最近更新 更多