Time: fix string conversion routine 35/119935/1
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Mon, 20 Mar 2017 15:43:55 +0000 (16:43 +0100)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Mon, 20 Mar 2017 15:46:16 +0000 (16:46 +0100)
The previous routine may caused buffer overflows if unicode codepoints
couldn't be encoded on single byte.

The return value of i18n_ustring_get_length was incorrectly understood
as number of character in utf-8 encoding. The proper function is now
used instead.

Change-Id: Ie5ad77a31914298460d38a410b6b01906dcde32d

clock/src/Utils/Time.cpp

index 475056087ea1f02a2a9f79936436108abc8adf9e..0c3846df122a683e3dfaa866b67fe352eafbe84f 100644 (file)
@@ -114,15 +114,17 @@ I18nString::~I18nString()
 std::string I18nString::std_string() const
 {
        std::string str;
-       size_t new_size = i18n_ustring_get_length(data_);
-       if (get_last_result() != I18N_ERROR_NONE) {
-               FAT("i18n_ustring_copy_ua_n failed");
+       i18n_error_code_e error = I18N_ERROR_NONE;
+       int32_t new_size;
+       i18n_ustring_to_UTF8(nullptr, 0, &new_size, data_, -1, &error);
+       if ((error != I18N_ERROR_NONE) && (error != I18N_ERROR_BUFFER_OVERFLOW)) {
+               FAT("i18n_ustring_to_UTF8 failed: %s", get_error_message(error));
        }
        str.reserve(new_size + 1);
        str.resize(new_size);
-       i18n_ustring_copy_au(&str[0], data_);
-       if (get_last_result() != I18N_ERROR_NONE) {
-               FAT("i18n_ustring_copy_ua_n failed");
+       i18n_ustring_to_UTF8(&str[0], str.capacity(), &new_size, data_, -1, &error);
+       if (error != I18N_ERROR_NONE) {
+               FAT("i18n_ustring_to_UTF8 failed: %s", get_error_message(error));
        }
        return str;
 }