Fix (v)sprintf
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
Doxygen Action / build (push) Has been cancelled

9f5a68e789 changed its behavior to append
rather than set the output string. Which is misleading. This reverts
(v)sprintf back to set the string.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman
2025-11-14 15:12:32 +01:00
parent 9136c74309
commit acf2419ef0

View File

@@ -261,7 +261,7 @@ static int vsprintf(_Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_z_ _
int count = _vsnprintf(buf, _countof(buf), format, arg);
if (0 <= count && count < _countof(buf)) {
// Copy from stack.
str.append(buf, count);
str.assign(buf, count);
return count;
}
if (count < 0) {
@@ -275,9 +275,8 @@ static int vsprintf(_Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_z_ _
default: throw std::runtime_error("failed to format string");
}
}
size_t offset = str.size();
str.resize(offset + count);
if (_vsnprintf(&str[offset], static_cast<size_t>(count) + 1, format, arg) != count)
str.resize(count);
if (_vsnprintf(&str[0], static_cast<size_t>(count) + 1, format, arg) != count)
throw std::runtime_error("failed to format string");
return count;
}
@@ -300,7 +299,7 @@ static int vsprintf(_Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_z
int count = _vsnwprintf(buf, _countof(buf), format, arg);
if (0 <= count && count < _countof(buf)) {
// Copy from stack.
str.append(buf, count);
str.assign(buf, count);
return count;
}
if (count < 0) {
@@ -314,9 +313,8 @@ static int vsprintf(_Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_z
default: throw std::runtime_error("failed to format string");
}
}
size_t offset = str.size();
str.resize(offset + count);
if (_vsnwprintf(&str[offset], static_cast<size_t>(count) + 1, format, arg) != count)
str.resize(count);
if (_vsnwprintf(&str[0], static_cast<size_t>(count) + 1, format, arg) != count)
throw std::runtime_error("failed to format string");
return count;
}