Warn usage of (v)sprintf (v3)
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
Doxygen Action / build (push) Has been cancelled

Until all projects using this are reviewed, please keep this warning in
place.

Signed-off-by: Simon Rozman <simon.rozman@amebis.si>
This commit is contained in:
2026-01-16 14:35:16 +01:00
parent 7ac7c7ef1b
commit f25e815a19
3 changed files with 18 additions and 3 deletions

View File

@@ -69,8 +69,11 @@ namespace winstd
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else
}
else {
#pragma warning(suppress: 4996) // Checked to comply.
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
}
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;

View File

@@ -330,6 +330,7 @@ static int vsprintf(_Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_z
/// \returns Number of characters in result.
///
template<class _Elem, class _Traits, class _Ax>
[[deprecated("Behavior of this function changed from append to assign. Please, review call usage.")]]
static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
{
va_list arg;
@@ -1283,6 +1284,7 @@ namespace winstd
}
}
}
#pragma warning(suppress: 4996) // Checked to comply.
sprintf(sResult, "msg %u", nId);
return sResult;
}
@@ -1322,6 +1324,7 @@ namespace winstd
}
}
}
#pragma warning(suppress: 4996) // Checked to comply.
sprintf(sResult, "msg %u", nId);
return sResult;
}
@@ -1467,8 +1470,11 @@ namespace winstd
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else
}
else {
#pragma warning(suppress: 4996) // Checked to comply.
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
}
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;
@@ -1522,6 +1528,7 @@ namespace winstd
va_list arg;
va_start(arg, nFormatID);
#pragma warning(suppress: 4996) // Checked to comply.
vsprintf(*this, format, arg);
va_end(arg);
}
@@ -1540,6 +1547,7 @@ namespace winstd
va_list arg;
va_start(arg, nFormatID);
#pragma warning(suppress: 4996) // Checked to comply.
vsprintf(*this, format, arg);
va_end(arg);
}
@@ -1709,6 +1717,7 @@ namespace winstd
///
basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
{
#pragma warning(suppress: 4996) // Checked to comply.
sprintf<_Elem, _Traits, _Ax>(*this, format,
guid.Data1,
guid.Data2,

View File

@@ -2104,8 +2104,11 @@ namespace winstd
if (ntdll.valid() && FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, (HMODULE)ntdll, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else
}
else {
#pragma warning(suppress: 4996) // Checked to comply.
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
}
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;