Boost C++ Libraries: Ticket #4154: Posix_time_zone_base constructor dereferences invalid iterator https://svn.boost.org/trac10/ticket/4154 <p> The constructor taking a string segfaults if the string does not contain an appropriate number of commas for the specific timezone format. The calling code shouldn't have to know how many commas are appropriate; an exception should be thrown if there are not enough. </p> <pre class="wiki">posix_time_zone_base(const string_type&amp; s) ... { const char_type sep_chars[2] = {','}; char_separator_type sep(sep_chars); tokenizer_type tokens(s, sep); tokenizer_iterator_type it = tokens.begin(); calc_zone(*it++); //Here if "" if(has_dst_){ string_type tmp_str = *it++; //Here if "EST5EDT" calc_rules(tmp_str, *it); //Here if "EST5EDT,M3.5.0/2" } } </pre><p> The iterator <code>it</code> should be checked against <code>tokens.end()</code>. </p> <p> Sample code to demonstrate: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/date_time/posix_time/posix_time.hpp&gt; #include &lt;boost/date_time/local_time/local_time.hpp&gt; int main() { using namespace boost::local_time; using namespace boost::posix_time; try { std::string badtzstr("EST5EDT"); time_zone_ptr tz; tz = time_zone_ptr(new posix_time_zone(badtzstr)); } catch (const std::exception&amp; e) { std::cout &lt;&lt; "passed: " &lt;&lt; e.what() &lt;&lt; std::endl; } return 0; } </pre><p> Results under a checked build: </p> <pre class="wiki">Assertion failed: valid_, file c:/bwaysource/vendor/boost_1_40_0\boost/token_iterator.hpp, line 51 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. </pre><p> A workaround is to scan for non-digits, digits, non-digits up to the end of string or first comma, and if matched, only construct the timezone if the string contains at least two commas (otherwise just check that the string is non-empty). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4154 Trac 1.4.3 Andrey Semashev Wed, 09 Jun 2010 19:29:51 GMT <link>https://svn.boost.org/trac10/ticket/4154#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4154#comment:1</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/62685" title="Refs #4154. Added iterator validity checks. If TZ string is not valid, ...">[62685]</a>) Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4154" title="#4154: Bugs: Posix_time_zone_base constructor dereferences invalid iterator (closed: fixed)">#4154</a>. Added iterator validity checks. If TZ string is not valid, the time zone constructor throws instead of crashing. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Thu, 10 Jun 2010 17:24:40 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4154#comment:2 https://svn.boost.org/trac10/ticket/4154#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/62751" title="Fixes #4154. Added iterator validity checks. If TZ string is not ...">[62751]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4154" title="#4154: Bugs: Posix_time_zone_base constructor dereferences invalid iterator (closed: fixed)">#4154</a>. Added iterator validity checks. If TZ string is not valid, the time zone constructor throws instead of crashing. </p> Ticket