Boost C++ Libraries: Ticket #7743: utf_traits::decode does not check for correct UTF-8 trailing bytes https://svn.boost.org/trac10/ticket/7743 <p> This program shows the erroneous behavior. An exception should be thrown since 0xdf is the start byte of a 2-byte sequence. However it is followed by a 1-byte character. </p> <p> Boost.Locale does not throw an exception. </p> <p> #include &lt;iostream&gt; #include &lt;stdexcept&gt; #include &lt;boost/locale/utf.hpp&gt; </p> <p> template&lt;typename T&gt; std::basic_string&lt;T&gt; checkUtf(const std::basic_string&lt;T&gt;&amp; p_str) { </p> <blockquote> <p> const std::string encodingType = sizeof(T) == 1 ? "UTF-8" : (sizeof(T) == 2 ? "UTF-16" : "UTF-32"); typename std::basic_string&lt;T&gt;::const_iterator it = p_str.begin(); while (it != p_str.end()) { </p> <blockquote> <p> const boost::locale::utf::code_point cp = boost::locale::utf::utf_traits&lt;T&gt;::decode(it, p_str.end()); if (cp == boost::locale::utf::illegal) </p> <blockquote> <p> throw std::runtime_error("Source string contains illegal " + encodingType + " byte sequences"); </p> </blockquote> <p> else if (cp == boost::locale::utf::incomplete) </p> <blockquote> <p> throw std::runtime_error("Source string contains imcomplete " + encodingType + " byte sequences"); </p> </blockquote> </blockquote> <p> } return p_str; </p> </blockquote> <p> } </p> <p> int main(void) { </p> <blockquote> <p> try { </p> <blockquote> <p> checkUtf("A"+std::string(1,0xdf)+"A"); return 0; </p> </blockquote> <p> } catch (const std::exception&amp; e) { </p> <blockquote> <p> std::cout &lt;&lt; e.what() &lt;&lt; std::endl; </p> </blockquote> <p> } return 1; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7743 Trac 1.4.3 anonymous Tue, 27 Nov 2012 12:30:13 GMT attachment set https://svn.boost.org/trac10/ticket/7743 https://svn.boost.org/trac10/ticket/7743 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">utf.hpp.patch</span> </li> </ul> <p> Patch that adds the necessary checks </p> Ticket Artyom Beilis Tue, 27 Nov 2012 15:44:38 GMT status, severity changed https://svn.boost.org/trac10/ticket/7743#comment:1 https://svn.boost.org/trac10/ticket/7743#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>severity</strong> <span class="trac-field-old">Problem</span> → <span class="trac-field-new">Showstopper</span> </li> </ul> <p> Yes, you are 100% right. </p> <p> I'll apply the patch ASAP </p> Ticket Artyom Beilis Tue, 27 Nov 2012 16:07:19 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7743#comment:2 https://svn.boost.org/trac10/ticket/7743#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in trunk in changeset <a class="changeset" href="https://svn.boost.org/trac10/changeset/81590" title="Fixed bug #7743 invalid encoding asccepted as valid ">[81590]</a>. </p> Ticket