MSVC SDL compliance: avoid calls to strcpy and wcsncpy
This commit is contained in:
parent
6bea21d48c
commit
ab5f08dfff
@ -383,7 +383,7 @@ static bool setFontVista(
|
||||
infoex.cbSize = sizeof(AGENT_CONSOLE_FONT_INFOEX);
|
||||
infoex.dwFontSize.Y = pxSize;
|
||||
infoex.FontWeight = 400;
|
||||
wcsncpy(infoex.FaceName, faceName, COUNT_OF(infoex.FaceName));
|
||||
winpty_wcsncpy_nul(infoex.FaceName, faceName);
|
||||
dumpFontInfoEx(infoex, "setFontVista: setting font to: ");
|
||||
if (!api.SetCurrentConsoleFontEx()(conout, FALSE, &infoex)) {
|
||||
trace("setFontVista: SetCurrentConsoleFontEx call failed");
|
||||
|
@ -22,8 +22,10 @@
|
||||
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#include "winpty_snprintf.h"
|
||||
@ -82,11 +84,14 @@ static const char *getDebugConfig()
|
||||
if (g_debugConfig == NULL) {
|
||||
const int bufSize = 256;
|
||||
char buf[bufSize];
|
||||
DWORD actualSize = GetEnvironmentVariableA("WINPTY_DEBUG", buf, bufSize);
|
||||
if (actualSize == 0 || actualSize >= (DWORD)bufSize)
|
||||
DWORD actualSize =
|
||||
GetEnvironmentVariableA("WINPTY_DEBUG", buf, bufSize);
|
||||
if (actualSize == 0 || actualSize >= static_cast<DWORD>(bufSize)) {
|
||||
buf[0] = '\0';
|
||||
char *newConfig = new char[strlen(buf) + 1];
|
||||
strcpy(newConfig, buf);
|
||||
}
|
||||
const size_t len = strlen(buf) + 1;
|
||||
char *newConfig = new char[len];
|
||||
std::copy(buf, buf + len, newConfig);
|
||||
void *oldValue = InterlockedCompareExchangePointer(
|
||||
&g_debugConfig, newConfig, NULL);
|
||||
if (oldValue != NULL) {
|
||||
|
@ -22,10 +22,15 @@
|
||||
#define STRING_UTIL_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "WinptyAssert.h"
|
||||
|
||||
size_t winpty_wcsnlen(const wchar_t *s, size_t maxlen);
|
||||
std::string utf8FromWide(const std::wstring &input);
|
||||
|
||||
@ -46,4 +51,30 @@ std::vector<T> vectorWithNulFromString(const std::basic_string<T> &str) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// A safer(?) version of wcsncpy that is accepted by MSVC's /SDL mode.
|
||||
template <size_t N>
|
||||
wchar_t *winpty_wcsncpy(wchar_t (&d)[N], const wchar_t *s) {
|
||||
ASSERT(s != nullptr);
|
||||
size_t i = 0;
|
||||
for (; i < N; ++i) {
|
||||
if (s[i] == L'\0') {
|
||||
break;
|
||||
}
|
||||
d[i] = s[i];
|
||||
}
|
||||
for (; i < N; ++i) {
|
||||
d[i] = L'\0';
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
// Like wcsncpy, but ensure that the destination buffer is NUL-terminated.
|
||||
template <size_t N>
|
||||
wchar_t *winpty_wcsncpy_nul(wchar_t (&d)[N], const wchar_t *s) {
|
||||
static_assert(N > 0, "array cannot be 0-size");
|
||||
winpty_wcsncpy(d, s);
|
||||
d[N - 1] = L'\0';
|
||||
return d;
|
||||
}
|
||||
|
||||
#endif // STRING_UTIL_H
|
||||
|
Loading…
Reference in New Issue
Block a user