Ticket #7743 (closed Bugs: fixed)

Opened 4 years ago

Last modified 4 years ago

utf_traits::decode does not check for correct UTF-8 trailing bytes

Reported by: peter.klotz@… Owned by: artyom
Milestone: To Be Determined Component: locale
Version: Boost 1.52.0 Severity: Showstopper
Keywords: Cc:


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.

Boost.Locale does not throw an exception.

#include <iostream> #include <stdexcept> #include <boost/locale/utf.hpp>

template<typename T> std::basic_string<T> checkUtf(const std::basic_string<T>& p_str) {

const std::string encodingType = sizeof(T) == 1 ? "UTF-8" : (sizeof(T) == 2 ? "UTF-16" : "UTF-32"); typename std::basic_string<T>::const_iterator it = p_str.begin(); while (it != p_str.end()) {

const boost::locale::utf::code_point cp = boost::locale::utf::utf_traits<T>::decode(it, p_str.end()); if (cp == boost::locale::utf::illegal)

throw std::runtime_error("Source string contains illegal " + encodingType + " byte sequences");

else if (cp == boost::locale::utf::incomplete)

throw std::runtime_error("Source string contains imcomplete " + encodingType + " byte sequences");

} return p_str;


int main(void) {

try {

checkUtf("A"+std::string(1,0xdf)+"A"); return 0;

} catch (const std::exception& e) {

std::cout << e.what() << std::endl;

} return 1;



utf.hpp.patch (902 bytes) - added by anonymous 4 years ago.
Patch that adds the necessary checks

Change History

Changed 4 years ago by anonymous

Patch that adds the necessary checks

comment:1 Changed 4 years ago by artyom

  • Status changed from new to assigned
  • Severity changed from Problem to Showstopper

Yes, you are 100% right.

I'll apply the patch ASAP

comment:2 Changed 4 years ago by artyom

  • Status changed from assigned to closed
  • Resolution set to fixed

Fixed in trunk in changeset [81590].

Last edited 4 years ago by artyom (previous) (diff)

Add a comment

Modify Ticket

Change Properties
<Author field>
as closed
The resolution will be deleted. Next status will be 'reopened'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.