Fixed: string to wstring conversion crash when string is too big

This commit is contained in:
aristocratos 2022-02-15 07:11:22 +01:00
parent 58846af516
commit 2aeae24b88
2 changed files with 9 additions and 3 deletions

View file

@ -171,7 +171,7 @@ namespace Tools {
size_t wide_ulen(const string& str) {
unsigned int chars = 0;
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
auto w_str = conv.from_bytes(str.c_str());
auto w_str = conv.from_bytes((str.size() > 10000 ? str.substr(0, 10000).c_str() : str.c_str()));
for (auto c : w_str) {
chars += utf8::wcwidth(c);
@ -194,7 +194,7 @@ namespace Tools {
if (len < 1 or str.empty()) return "";
if (wide) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
auto w_str = conv.from_bytes(str.c_str());
auto w_str = conv.from_bytes((str.size() > 10000 ? str.substr(0, 10000).c_str() : str.c_str()));
while (wide_ulen(w_str) > len)
w_str.pop_back();
str = conv.to_bytes(w_str);

View file

@ -1493,7 +1493,13 @@ namespace Proc {
pread.open(d.path() / "cmdline");
if (not pread.good()) continue;
long_string.clear();
while(getline(pread, long_string, '\0')) new_proc.cmd += long_string + ' ';
while(getline(pread, long_string, '\0')) {
new_proc.cmd += long_string + ' ';
if (new_proc.cmd.size() > 1000) {
new_proc.cmd.resize(1000);
break;
}
}
pread.close();
if (not new_proc.cmd.empty()) new_proc.cmd.pop_back();