Commit bec6fa4a authored by Max Kellermann's avatar Max Kellermann

lib/icu/Converter: throw exception on error

parent ac7ce735
...@@ -114,11 +114,12 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs) ...@@ -114,11 +114,12 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs)
return FixSeparators(path_fs); return FixSeparators(path_fs);
#ifdef HAVE_FS_CHARSET #ifdef HAVE_FS_CHARSET
try {
const auto buffer = fs_converter->ToUTF8(path_fs); const auto buffer = fs_converter->ToUTF8(path_fs);
if (buffer.IsNull())
return PathTraitsUTF8::string();
return FixSeparators(PathTraitsUTF8::string(buffer.c_str())); return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
} catch (const std::runtime_error &) {
return PathTraitsUTF8::string();
}
#endif #endif
#endif #endif
} }
......
...@@ -90,8 +90,11 @@ DoConvert(iconv_t conv, const char *src) ...@@ -90,8 +90,11 @@ DoConvert(iconv_t conv, const char *src)
size_t n = iconv(conv, &in, &in_left, &out, &out_left); size_t n = iconv(conv, &in, &in_left, &out, &out_left);
if (n == static_cast<size_t>(-1) || in_left > 0) if (n == static_cast<size_t>(-1))
return nullptr; throw MakeErrno("Charset conversion failed");
if (in_left > 0)
throw std::runtime_error("Charset conversion failed");
return AllocatedString<>::Duplicate(buffer, sizeof(buffer) - out_left); return AllocatedString<>::Duplicate(buffer, sizeof(buffer) - out_left);
} }
...@@ -100,7 +103,7 @@ DoConvert(iconv_t conv, const char *src) ...@@ -100,7 +103,7 @@ DoConvert(iconv_t conv, const char *src)
AllocatedString<char> AllocatedString<char>
IcuConverter::ToUTF8(const char *s) const IcuConverter::ToUTF8(const char *s) const
try { {
#ifdef HAVE_ICU #ifdef HAVE_ICU
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
...@@ -116,26 +119,23 @@ try { ...@@ -116,26 +119,23 @@ try {
&source, source + strlen(source), &source, source + strlen(source),
nullptr, true, &code); nullptr, true, &code);
if (code != U_ZERO_ERROR) if (code != U_ZERO_ERROR)
return nullptr; throw std::runtime_error(FormatString("Failed to convert to Unicode: %s",
u_errorName(code)).c_str());
const size_t target_length = target - buffer; const size_t target_length = target - buffer;
return UCharToUTF8({buffer, target_length}); return UCharToUTF8({buffer, target_length});
#elif defined(HAVE_ICONV) #elif defined(HAVE_ICONV)
return DoConvert(to_utf8, s); return DoConvert(to_utf8, s);
#endif #endif
} catch (const std::runtime_error &) {
return nullptr;
} }
AllocatedString<char> AllocatedString<char>
IcuConverter::FromUTF8(const char *s) const IcuConverter::FromUTF8(const char *s) const
try { {
#ifdef HAVE_ICU #ifdef HAVE_ICU
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
const auto u = UCharFromUTF8(s); const auto u = UCharFromUTF8(s);
if (u.IsNull())
return nullptr;
ucnv_resetFromUnicode(converter); ucnv_resetFromUnicode(converter);
...@@ -149,15 +149,14 @@ try { ...@@ -149,15 +149,14 @@ try {
nullptr, true, &code); nullptr, true, &code);
if (code != U_ZERO_ERROR) if (code != U_ZERO_ERROR)
return nullptr; throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
u_errorName(code)).c_str());
return AllocatedString<>::Duplicate(buffer, target); return AllocatedString<>::Duplicate(buffer, target);
#elif defined(HAVE_ICONV) #elif defined(HAVE_ICONV)
return DoConvert(from_utf8, s); return DoConvert(from_utf8, s);
#endif #endif
} catch (const std::runtime_error &) {
return nullptr;
} }
#endif #endif
...@@ -79,7 +79,7 @@ public: ...@@ -79,7 +79,7 @@ public:
/** /**
* Convert the string to UTF-8. * Convert the string to UTF-8.
* *
* Returns AllocatedString::Null() on error. * Throws std::runtime_error on error.
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
AllocatedString<char> ToUTF8(const char *s) const; AllocatedString<char> ToUTF8(const char *s) const;
...@@ -87,7 +87,7 @@ public: ...@@ -87,7 +87,7 @@ public:
/** /**
* Convert the string from UTF-8. * Convert the string from UTF-8.
* *
* Returns AllocatedString::Null() on error. * Throws std::runtime_error on error.
*/ */
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
AllocatedString<char> FromUTF8(const char *s) const; AllocatedString<char> FromUTF8(const char *s) const;
......
...@@ -54,8 +54,11 @@ public: ...@@ -54,8 +54,11 @@ public:
CPPUNIT_ASSERT(converter != nullptr); CPPUNIT_ASSERT(converter != nullptr);
for (const auto i : invalid_utf8) { for (const auto i : invalid_utf8) {
try {
auto f = converter->FromUTF8(i); auto f = converter->FromUTF8(i);
CPPUNIT_ASSERT_EQUAL(true, f.IsNull()); CPPUNIT_FAIL("Exception expected");
} catch (const std::runtime_error &) {
}
} }
for (const auto i : latin1_tests) { for (const auto i : latin1_tests) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment