C26485, refactor to avoid array-to-pointer decay.

This commit is contained in:
Michael Niksa 2019-09-03 09:40:31 -07:00
parent 230e7f43e0
commit 7d4096bbbf
3 changed files with 23 additions and 22 deletions

View file

@ -147,13 +147,12 @@ DxEngine::~DxEngine()
// D3D11_CREATE_DEVICE_DEBUG |
D3D11_CREATE_DEVICE_SINGLETHREADED;
const D3D_FEATURE_LEVEL FeatureLevels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_1,
};
std::array<D3D_FEATURE_LEVEL, 5> FeatureLevels;
FeatureLevels.at(0) = D3D_FEATURE_LEVEL_11_1;
FeatureLevels.at(1) = D3D_FEATURE_LEVEL_11_0;
FeatureLevels.at(2) = D3D_FEATURE_LEVEL_10_1;
FeatureLevels.at(3) = D3D_FEATURE_LEVEL_10_0;
FeatureLevels.at(4) = D3D_FEATURE_LEVEL_9_1;
// Trying hardware first for maximum performance, then trying WARP (software) renderer second
// in case we're running inside a downlevel VM where hardware passthrough isn't enabled like
@ -162,8 +161,8 @@ DxEngine::~DxEngine()
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
DeviceFlags,
FeatureLevels,
ARRAYSIZE(FeatureLevels),
FeatureLevels.data(),
gsl::narrow<UINT>(FeatureLevels.size()),
D3D11_SDK_VERSION,
&_d3dDevice,
nullptr,
@ -175,8 +174,8 @@ DxEngine::~DxEngine()
D3D_DRIVER_TYPE_WARP,
nullptr,
DeviceFlags,
FeatureLevels,
ARRAYSIZE(FeatureLevels),
FeatureLevels.data(),
gsl::narrow<UINT>(FeatureLevels.size()),
D3D11_SDK_VERSION,
&_d3dDevice,
nullptr,

View file

@ -10,16 +10,14 @@ using namespace Microsoft::Console::Types;
using namespace Microsoft::Console::Types::ScreenInfoUiaProviderTracing;
// A helper function to create a SafeArray Version of an int array of a specified length
SAFEARRAY* BuildIntSafeArray(_In_reads_(length) const int* const data, const int length) noexcept
SAFEARRAY* BuildIntSafeArray(std::basic_string_view<int> data) noexcept
{
SAFEARRAY* psa = SafeArrayCreateVector(VT_I4, 0, length);
SAFEARRAY* psa = SafeArrayCreateVector(VT_I4, 0, gsl::narrow<ULONG>(data.size()));
if (psa != nullptr)
{
const auto dataSpan = gsl::make_span(data, length);
for (long i = 0; i < length; i++)
for (long i = 0; i < data.size(); i++)
{
if (FAILED(SafeArrayPutElement(psa, &i, (void*)&(dataSpan.at(i)))))
if (FAILED(SafeArrayPutElement(psa, &i, (void*)&(data.at(i)))))
{
SafeArrayDestroy(psa);
psa = nullptr;
@ -257,9 +255,13 @@ IFACEMETHODIMP ScreenInfoUiaProviderBase::GetRuntimeId(_Outptr_result_maybenull_
*ppRuntimeId = nullptr;
// AppendRuntimeId is a magic Number that tells UIAutomation to Append its own Runtime ID(From the HWND)
const int rId[] = { UiaAppendRuntimeId, -1 };
std::array<int, 2> rId;
rId.at(0) = UiaAppendRuntimeId;
rId.at(1) = -1;
const auto span = std::basic_string_view<int>(rId.data(), rId.size());
// BuildIntSafeArray is a custom function to hide the SafeArray creation
*ppRuntimeId = BuildIntSafeArray(rId, 2);
*ppRuntimeId = BuildIntSafeArray(span);
RETURN_IF_NULL_ALLOC(*ppRuntimeId);
return S_OK;

View file

@ -29,12 +29,12 @@ short Utils::ClampToShortMax(const long value, const short min) noexcept
// - a string representation of the GUID. On failure, throws E_INVALIDARG.
std::wstring Utils::GuidToString(const GUID guid)
{
wchar_t guid_cstr[39];
const int written = swprintf(guid_cstr, sizeof(guid_cstr), L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
std::array<wchar_t, 39> guid_cstr;
const int written = swprintf(guid_cstr.data(), guid_cstr.size(), L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
THROW_HR_IF(E_INVALIDARG, written == -1);
return std::wstring(guid_cstr);
return std::wstring(guid_cstr.data(), guid_cstr.size());
}
// Method Description: