首页 文章

提升asio ssl流套接字编译问题

提问于
浏览
0

在c应用程序中使用boost 1.4.2 asio并获得linux编译器警告我不会理解 .

还在?
我正在使用的应用程序需要"socket",它可能是一个ssl套接字或一个普通的tcp套接字,所以我们隐藏模板"socket"类后面的细节,它接受ssl套接字类或tcp套接字类作为模板参数 - 下面是ssl类码 .
该应用程序正常运行,未启用任何优化;问题是当我在linux g 4.4.1下编译并在-02或更高版本打开优化时,-fstrict-aliasing标志被打开 . 编译结果严格别名警告沿着以下几行:"warning: dereferencing type-punned pointer will break strict-aliasing rules"我无法解除引用_psock(例如_psock->握手)

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

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:warning:dereferencing type-punned pointer将破坏严格别名规则../ .. /../../third-party/boost/1.42.0/boost/function/function_base.hpp:325:warning:dereferencing type-punned pointer将打破严格别名规则../../../ . ./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)[与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:从'vo . 实例化id boost :: function0 :: assign_to(Functor)[与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)[使用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)实例化[与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&)[with 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:从这里实例化

2 回答

  • 1

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

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

    他们有一整节致力于-fstrict-aliasing

    -fstrict-aliasing - 也由-O2,-O3和-Os打开 . 告诉编译器可以根据表达式的类型进行某种优化 . 特别是你有希望通过使用这个标志,一种类型的对象不会与不兼容类型的对象驻留在同一地址 . -fno-strict-aliasing - 关闭此优化 . 如果这改变了代码的行为,则代码中存在问题 . 这是试图告诉您,您要求编译器执行未定义的行为,它可能不会执行您认为它将执行的操作 . 随着优化级别的提高,你不喜欢它的功能会增加 . 我稍后展示了一个简单的例子,当打开任何级别的优化时,令人惊讶地产生了错误的结果 . 在你自己的危险中忽略这个警告 . 您不太可能关心导致的未定义行为 .

  • 0

    我和gcc-4.4.4(Debian Squeeze)有类似的问题 . 我浏览了与此相关的Boost邮件列表thread . 然而,在最后,我'm still confused as to whose problem it is. Whether GCC' S或升压's. :-S I'已经使出-fno严格走样在我的代码 .

相关问题