1

Boostのシリアル化コードを使用してデータをバイナリストリームに書き込み始めました。無効にできる次の警告が表示されます(D_SCL_SECURE_NO_WARNINGS)が、安全かどうか疑問に思います。どのコードが警告を引き起こしているのか、より具体的な情報を取得するにはどうすればよいですか?私は.cppファイルしか知りません。

これは、VisualStudio2008からの出力です。

4>g:\winlib\boost\1_42_0\boost/archive/basic_binary_iprimitive.hpp(162) : warng C4996: 'std::basic_streambuf<_Elem,_Traits>::sgetn': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>        E:\Visual Studio 9.0\VC\include\streambuf(121) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::sgetn'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iprimitive.hpp(152) : while compiling class template member function 'void boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::load_binary(void *,size_t)'
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(107) : see reference to function template instantiation 'void boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::load<T>(T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(356) : see reference to function template instantiation 'void boost::archive::load_access::load_primitive<Archive,T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(433) : see reference to function template instantiation 'void boost::archive::detail::load_non_pointer_type<Archive>::load_primitive::invoke<T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(580) : see reference to function template instantiation 'void boost::archive::detail::load_non_pointer_type<Archive>::invoke<T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/common_iarchive.hpp(68) : see reference to function template instantiation 'void boost::archive::load<Archive,T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iarchive.hpp(67) : see reference to function template instantiation 'void boost::archive::detail::common_iarchive<Archive>::load_override<T>(T &,int)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/binary_iarchive_impl.hpp(50) : see reference to function template instantiation 'void boost::archive::basic_binary_iarchive<Archive>::load_override<T>(T &,int)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/interface_iarchive.hpp(61) : see reference to function template instantiation 'void boost::archive::binary_iarchive_impl<Archive,Elem,Tr>::load_override<T>(T &,int)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iarchive.hpp(77) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_iarchive<Archive>::operator >><unsigned char>(T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iarchive.hpp(74) : while compiling class template member function 'void boost::archive::basic_binary_iarchive<Archive>::load_override(boost::archive::version_type &,int)'
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/binary_iarchive_impl.hpp(36) : see reference to class template instantiation 'boost::archive::basic_binary_iarchive<Archive>' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/binary_iarchive.hpp(42) : see reference to class template instantiation 'boost::archive::binary_iarchive_impl<Archive,Elem,Tr>' being compiled
4>        with
4>        [
4>            Archive=boost::archive::naked_binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>
4>        ]
4>g:\winlib\boost\1_42_0\boost/archive/basic_binary_iprimitive.hpp(176) : warning C4996: 'std::basic_streambuf<_Elem,_Traits>::sgetn': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>        E:\Visual Studio 9.0\VC\include\streambuf(121) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::sgetn'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>g:\winlib\boost\1_42_0\boost/archive/basic_binary_iprimitive.hpp(162) : warning C4996: 'std::basic_streambuf<_Elem,_Traits>::sgetn': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>        E:\Visual Studio 9.0\VC\include\streambuf(121) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::sgetn'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iprimitive.hpp(152) : while compiling class template member function 'void boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::load_binary(void *,size_t)'
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(107) : see reference to function template instantiation 'void boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::load<T>(T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(356) : see reference to function template instantiation 'void boost::archive::load_access::load_primitive<Archive,T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(433) : see reference to function template instantiation 'void boost::archive::detail::load_non_pointer_type<Archive>::load_primitive::invoke<T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/iserializer.hpp(580) : see reference to function template instantiation 'void boost::archive::detail::load_non_pointer_type<Archive>::invoke<T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/common_iarchive.hpp(68) : see reference to function template instantiation 'void boost::archive::load<Archive,T>(Archive &,T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iarchive.hpp(67) : see reference to function template instantiation 'void boost::archive::detail::common_iarchive<Archive>::load_override<T>(T &,int)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/binary_iarchive_impl.hpp(50) : see reference to function template instantiation 'void boost::archive::basic_binary_iarchive<Archive>::load_override<T>(T &,int)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/detail/interface_iarchive.hpp(61) : see reference to function template instantiation 'void boost::archive::binary_iarchive_impl<Archive,Elem,Tr>::load_override<T>(T &,int)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iarchive.hpp(77) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_iarchive<Archive>::operator >><unsigned char>(T &)' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            T=unsigned char
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/basic_binary_iarchive.hpp(74) : while compiling class template member function 'void boost::archive::basic_binary_iarchive<Archive>::load_override(boost::archive::version_type &,int)'
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/binary_iarchive_impl.hpp(36) : see reference to class template instantiation 'boost::archive::basic_binary_iarchive<Archive>' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive
4>        ]
4>        g:\winlib\boost\1_42_0\boost/archive/binary_iarchive.hpp(76) : see reference to class template instantiation 'boost::archive::binary_iarchive_impl<Archive,Elem,Tr>' being compiled
4>        with
4>        [
4>            Archive=boost::archive::binary_iarchive,
4>            Elem=char,
4>            Tr=std::char_traits<char>
4>        ]
4>g:\winlib\boost\1_42_0\boost/archive/basic_binary_iprimitive.hpp(176) : warning C4996: 'std::basic_streambuf<_Elem,_Traits>::sgetn': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]
4>        E:\Visual Studio 9.0\VC\include\streambuf(121) : see declaration of 'std::basic_streambuf<_Elem,_Traits>::sgetn'
4>        with
4>        [
4>            _Elem=char,
4>            _Traits=std::char_traits<char>
4>        ]

ありがとう!

4

3 に答える 3

2

Boostが範囲外のパラメーターを渡さないことを信頼している場合は、警告を無効にすることができます。

Microsoftは、Cライブラリ用の一連の新しい「より安全な」関数を提案しました。

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1225.pdf

古いものを使用するすべての人に警告しています。

新しいBoostライブラリを試すこともできますか?最新は1.46.1です

于 2011-03-28T10:58:32.467 に答える
0

プロジェクトがVisualStudioのみを使用してコンパイルされる場合は、_Sgetn_sで(警告の行から)sgetnへの呼び出しを置き換え最初呼び出しの最後のパラメーターを複製できます。そうすれば、通話を安全な通話に変換できます。

または、以下に提供されているパッチを使用することもできます。これは、VC 8.0以降には_Sgetn_sを適用しますが、他のすべてのコンパイラにはsgetnを残します。

Index: basic_binary_iprimitive.hpp
===================================================================

@@ -143,6 +143,11 @@
     void
     load_binary(void *address, std::size_t count);
 };
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) //VC 8.0 +
+#   define BOOST_sgetn_safe( buf, count ) _Sgetn_s( buf, count, count )
+#else 
+#   define BOOST_sgetn_safe( buf, count ) sgetn( buf, count )
+#endif

 template<class Archive, class Elem, class Tr>
 inline void
@@ -157,6 +162,7 @@
     );
     std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
-    std::streamsize scount = m_sb.sgetn(
+    std::streamsize scount = m_sb.BOOST_sgetn_safe(
         static_cast<Elem *>(address), 
         s
     );
@@ -173,7 +179,7 @@
 //                archive_exception(archive_exception::stream_error)
 //        );
         Elem t;
-        scount = m_sb.sgetn(& t, 1);
+        scount = m_sb.BOOST_sgetn_safe(& t, 1);
         if(scount != 1)
             boost::serialization::throw_exception(
                 archive_exception(archive_exception::input_stream_error)
@@ -181,6 +187,7 @@
         std::memcpy(static_cast<char*>(address) + (count - s), &t, s);
     }
 }
+#undef BOOST_sgetn_safe

 } // namespace archive
 } // namespace boost
于 2011-08-10T20:58:26.053 に答える
0

Boostのドキュメントは、無視しても安全な警告がたくさんあることを認めています。コードに次を追加して、VisualStudio2008のコンソールから不快な警告メッセージを削除しました。

#pragma warning(disable:4099)
于 2011-12-17T05:11:15.593 に答える