2011-03-28 50 views
1

我開始使用boost的序列化代碼將數據寫入二進制流。我收到以下可能被禁用的警告(D_SCL_SECURE_NO_WARNINGS),但我不知道這樣做是否安全?我怎樣才能得到更具體的信息哪些代碼導致警告?我只知道.cpp文件。Boost序列化:安全地忽略警告?

這是從Visual Studio 2008的輸出:

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>  ] 

謝謝!

+0

什麼是所有4S? – 2011-03-28 10:07:30

+0

這是來自VS輸出。我在我的解決方案中有幾個項目,它是編譯項目4的輸出。(並行編譯很有幫助) – Philipp 2011-03-28 10:13:18

回答

2

如果您信任Boost不會超出範圍參數,則可以禁用警告。

微軟已經提出了一套C庫新的「更安全」的功能,

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

,並警告大家誰使用舊的。

你也許也可以嘗試一個更新的Boost庫?介紹最新的1.46.1

+0

除了編譯器所抱怨的功能不是C,而是C++,並且它非常安全(或至少和使用char *的東西一樣安全)。 – 2011-03-28 11:20:59

0

如果你的項目是要使用Visual Studio可以在_Sgetn_s更換呼叫sgetn(從預警線)和複製初始的最後一個參數只編譯呼叫。這樣你就可以把呼叫轉換成安全的呼叫。

或者你可以通過下面提供的補丁去,它適用_Sgetn_s爲VC 8.0及更高版本,但保留所有其他的編譯器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 
0

Boost文檔承認有許多警告是安全忽略的。我刪除從加入我在Visual Studio 2008控制檯刺激性警告消息下面我的代碼:

#pragma warning(disable:4099)