Ignore invalid Unicode strings in C++/CX.

This commit is contained in:
levlam 2021-04-08 14:43:47 +03:00
parent 2f687edf17
commit 9bb79b4b27
4 changed files with 14 additions and 6 deletions

View File

@ -42,12 +42,16 @@ string winerror_to_string(int code) {
wchar_t wbuf[size];
auto res_size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, code, 0, wbuf, size - 1, nullptr);
if (res_size == 0) {
return "Unknown windows error";
return "Unknown Windows error";
}
while (res_size != 0 && (wbuf[res_size - 1] == '\n' || wbuf[res_size - 1] == '\r')) {
res_size--;
}
return from_wstring(wbuf, res_size).ok();
auto error_message = from_wstring(wbuf, res_size);
if (error_message.is_error()) {
return "Invalid Windows error";
}
return error_message.move_as_ok();
}
#endif

View File

@ -85,7 +85,11 @@ inline std::string string_to_unmanaged(String^ str) {
if (!str) {
return std::string();
}
return td::from_wstring(str->Data(), str->Length()).ok();
auto r_unmanaged_str = td::from_wstring(str->Data(), str->Length());
if (r_unmanaged_str.is_error()) {
return std::string();
}
return r_unmanaged_str.move_as_ok();
}
inline String^ string_from_unmanaged(const std::string &from) {

View File

@ -470,7 +470,7 @@ CSlice get_temporary_dir() {
}
auto rs = from_wstring(buf);
LOG_IF(FATAL, rs.is_error()) << "GetTempPathW failed: " << rs.error();
temporary_dir = rs.ok();
temporary_dir = rs.move_as_ok();
}
if (temporary_dir.size() > 1 && temporary_dir.back() == TD_DIR_SLASH) {
temporary_dir.pop_back();

View File

@ -18,7 +18,7 @@ namespace td {
Result<std::wstring> to_wstring(CSlice slice) {
if (!check_utf8(slice)) {
return Status::Error("Wrong encoding");
return Status::Error("Wrong string encoding");
}
size_t wstring_len = utf8_utf16_length(slice);
@ -65,7 +65,7 @@ Result<string> from_wstring(const wchar_t *begin, size_t size) {
}
}
return Status::Error("Wrong encoding");
return Status::Error("Wrong wstring encoding");
}
result_len += 1 + (cur >= 0x80) + (cur >= 0x800);
}