c++ - 如何确保在将无效字符串作为参数传递时 posix_time_zone 构造函数不会崩溃

标签 c++ boost posix

我正在尝试在我们的项目中使用如下的 posix_time_zone。

time_zone_ptr zone(new posix_time_zone(localTimeZone.c_str()));
const string hasDST = zone->has_dst() ? "true" : "false";
const int Year = second_clock::local_time().date().year();
const string stTime = to_iso_extended_string(zone->dst_local_start_time(Year));
const string endTime = to_iso_extended_string(zone->dst_local_end_time(Year));

localTimeZone 将由项目中的另一个应用程序设置。我需要使用该值通过 posix_time_zone 获取 dst 相关参数。我无法控制另一个应用程序设置的值,我遇到了以下崩溃。

(gdb) bt
#0  0xb5a7f8a6 in ?? () from /lib/libc.so.6
#1  0xb5a8dcde in raise () from /lib/libc.so.6
#2  0xb5a905b6 in abort () from /lib/libc.so.6
#3  0xb5c3e1e6 in __gnu_cxx::__verbose_terminate_handler () at ../../../../gcc-linaro-4.8-2014.11/libstdc++-v3/libsupc++/vterminate.cc:95
#4  0xb5c3ce60 in __cxxabiv1::__terminate (handler=<optimized out>) at ../../../../gcc-linaro-4.8-2014.11/libstdc++-v3/libsupc++/eh_terminate.cc:38
#5  0xb5c3ce7c in std::terminate () at ../../../../gcc-linaro-4.8-2014.11/libstdc++-v3/libsupc++/eh_terminate.cc:48
#6  0xb5c3d068 in __cxxabiv1::__cxa_throw (obj=0xb4f00a90, tinfo=0xb60a7e34 <typeinfo for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >>,
dest=0xb60665e1 <boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >::~clone_impl()>) at ../../../../gcc-linaro-4.8-2014.11/libstdc++-v3/libsupc++/eh_throw.cc:87
#7  0xb6062dc0 in void boost::throw_exception<boost::bad_lexical_cast>(boost::bad_lexical_cast const&) () from /usr/local/lib/libmbtime.so
#8  0xb60631aa in boost::detail::lexical_cast_do_cast<int, std::string>::lexical_cast_impl(std::string const&) () from /usr/local/lib/libmbtime.so
#9  0xb605f3ea in int boost::lexical_cast<int, std::string>(std::string const&) () from /usr/local/lib/libmbtime.so
#10 0xb605b686 in boost::posix_time::time_duration boost::date_time::str_from_delimited_time_duration<boost::posix_time::time_duration, char>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()

来自/usr/local/lib/libmbtime.so #11 0xb605c80e in boost::local_time::posix_time_zone_base::calc_zone(std::string const&) () 来自/usr/local/lib/libmbtime.so #12 0xb6059440 in boost::local_time::posix_time_zone_base::posix_time_zone_base(std::string const&) () 来自/usr/local/lib/libmbtime.so #13 PlatformTime::TimeZoneHandler(void*) () 中的 0xb6054e1e 来自/usr/local/lib/libmbtime.so #14 0xb6f7d170 在 ?? () 来自/lib/libpthread.so.0 回溯停止:前一帧与此帧相同(损坏的堆栈?) (gdb) 退出

这可能是因为 localTimeZone 有一些无效值(另一个问题是我正在使用 c 字符串,我需要更改 C++ 字符串)。任何人都可以提出任何建议如何测试 localTimeZone 是否具有预期的正确格式 boost 。

最佳答案

boost::local_time::posix_time_zone 的构造函数抛出 std::invalid_argumentstd::out_of_rangeboost::bad_lexical_cast(在这种情况下)如果传递的值无效。捕获这些异常(或捕获它们共同的父级 std::exception)并且它不会崩溃。

关于c++ - 如何确保在将无效字符串作为参数传递时 posix_time_zone 构造函数不会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36411557/

相关文章:

c++ - CPU 数量的增加会降低性能,CPU 负载不变并且没有通信

c++ - 将指针传递给另一个线程的正确方法

c++ - Visual Studio 中解决方案范围的构建配置?

android - Android POSIX 兼容吗?

c - 如何在C中保存字符串的最后一个字符?

c++ - 如何在 Qt5 中使 QOpenGLContext 成为当前没有表面的?

C++函数调用顺序(boost)——困惑

C++:ptr_container 比较

c++ - boost::optional 对 bool 的隐式转换消失了吗?

c - mq_open 为四个队列返回 0