mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-05-23 22:45:05 -04:00
Fix OSReport strings corruption with non-trivial format (#709)
* Fix OSReport strings corruption with non-trivial format * Limit format attempts to 3
This commit is contained in:
+31
-8
@@ -21,16 +21,39 @@ static bool checkEnabled() {
|
||||
return !__OSReport_disable || dusk::OSReportReallyForceEnable;
|
||||
}
|
||||
|
||||
#ifndef va_copy
|
||||
#define va_copy(d, s) ((d) = (s))
|
||||
#endif
|
||||
|
||||
static std::string FormatToString(const char* msg, va_list list) {
|
||||
int ret = vsnprintf(nullptr, 0, msg, list);
|
||||
if (ret <= 0) {
|
||||
return {};
|
||||
size_t size = (strlen(msg) * 2) + 50;
|
||||
std::string str;
|
||||
va_list ap;
|
||||
int attempts = 0;
|
||||
while (true) {
|
||||
str.resize(size);
|
||||
va_copy(ap, list);
|
||||
int n = vsnprintf(str.data(), size, msg, ap);
|
||||
va_end(ap);
|
||||
if (n > -1 && n < size) {
|
||||
str.resize(n);
|
||||
break;
|
||||
}
|
||||
|
||||
++attempts;
|
||||
if (attempts >= 3) {
|
||||
if (n == -1) {
|
||||
str.clear();
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (n > -1) {
|
||||
size = n + 1;
|
||||
} else {
|
||||
size *= 2;
|
||||
}
|
||||
}
|
||||
++ret;
|
||||
std::unique_ptr<char[]> buf(new char[ret]);
|
||||
vsnprintf(buf.get(), ret, msg, list);
|
||||
buf[ret - 1] = '\0';
|
||||
return {buf.get()};
|
||||
return str;
|
||||
}
|
||||
|
||||
void OSReport(const char* fmt, ...) {
|
||||
|
||||
Reference in New Issue
Block a user