Skip to content

Commit ad22cd7

Browse files
authored
Code review for invalid args validation (#585)
1 parent e1c4d92 commit ad22cd7

12 files changed

+65
-13
lines changed

Audio/DynamicSoundEffectInstance.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class DynamicSoundEffectInstance::Impl : public IVoiceNotify
3131
mBufferNeeded(nullptr),
3232
mObject(object)
3333
{
34+
if (!engine)
35+
throw std::invalid_argument("AudioEngine is required");
36+
3437
if ((sampleRate < XAUDIO2_MIN_SAMPLE_RATE)
3538
|| (sampleRate > XAUDIO2_MAX_SAMPLE_RATE))
3639
{
@@ -55,6 +58,12 @@ class DynamicSoundEffectInstance::Impl : public IVoiceNotify
5558
throw std::invalid_argument("DynamicSoundEffectInstance supports 8 or 16 bit");
5659
}
5760

61+
if (!bufferNeeded)
62+
{
63+
DebugTrace("DynamicSoundEffectInstance requires a valid callback\n");
64+
throw std::invalid_argument("DynamicSoundEffectInstance");
65+
}
66+
5867
mBufferEvent.reset(CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE));
5968
if (!mBufferEvent)
6069
{
@@ -63,7 +72,6 @@ class DynamicSoundEffectInstance::Impl : public IVoiceNotify
6372

6473
CreateIntegerPCM(&mWaveFormat, sampleRate, channels, sampleBits);
6574

66-
assert(engine != nullptr);
6775
engine->RegisterNotify(this, true);
6876

6977
mBase.Initialize(engine, &mWaveFormat, flags);

Audio/SoundCommon.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,9 @@ void DirectX::CreateIntegerPCM(
521521
int channels,
522522
int sampleBits) noexcept
523523
{
524+
if (!wfx)
525+
return;
526+
524527
const int blockAlign = channels * sampleBits / 8;
525528

526529
wfx->wFormatTag = WAVE_FORMAT_PCM;
@@ -541,6 +544,9 @@ void DirectX::CreateFloatPCM(
541544
int sampleRate,
542545
int channels) noexcept
543546
{
547+
if (!wfx)
548+
return;
549+
544550
const int blockAlign = channels * 4;
545551

546552
wfx->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
@@ -563,6 +569,9 @@ void DirectX::CreateADPCM(
563569
int channels,
564570
int samplesPerBlock) noexcept(false)
565571
{
572+
if (!wfx)
573+
return;
574+
566575
if (wfxSize < (sizeof(WAVEFORMATEX) + MSADPCM_FORMAT_EXTRA_BYTES))
567576
{
568577
DebugTrace("CreateADPCM needs at least %zu bytes for the result\n",
@@ -608,6 +617,9 @@ void DirectX::CreateXWMA(
608617
int avgBytes,
609618
bool wma3) noexcept
610619
{
620+
if (!wfx)
621+
return;
622+
611623
wfx->wFormatTag = static_cast<WORD>((wma3) ? WAVE_FORMAT_WMAUDIO3 : WAVE_FORMAT_WMAUDIO2);
612624
wfx->nChannels = static_cast<WORD>(channels);
613625
wfx->nSamplesPerSec = static_cast<DWORD>(sampleRate);

Audio/SoundCommon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "Audio.h"
1414
#include "PlatformHelpers.h"
1515

16+
#include <stdexcept>
17+
1618
#ifdef USING_XAUDIO2_9
1719
#define DIRECTX_ENABLE_XWMA
1820
#endif

Audio/SoundEffect.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class SoundEffect::Impl : public IVoiceNotify
5050
, mXMAMemory(nullptr)
5151
#endif
5252
{
53-
assert(mEngine != nullptr);
53+
if (!engine)
54+
throw std::invalid_argument("AudioEngine is required");
55+
5456
mEngine->RegisterNotify(this, false);
5557
}
5658

Audio/WaveBank.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class WaveBank::Impl : public IVoiceNotify
3333
mPrepared(false),
3434
mStreaming(false)
3535
{
36-
assert(mEngine != nullptr);
36+
if (!engine)
37+
throw std::invalid_argument("AudioEngine is required");
38+
3739
mEngine->RegisterNotify(this, false);
3840
}
3941

Audio/WaveBankReader.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,9 @@ HRESULT WaveBankReader::Impl::Open(const wchar_t* szFileName) noexcept(false)
517517
Close();
518518
Clear();
519519

520+
if (!szFileName)
521+
return E_INVALIDARG;
522+
520523
m_prepared = false;
521524

522525
m_event.reset(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, EVENT_MODIFY_STATE | SYNCHRONIZE));
@@ -1029,7 +1032,7 @@ HRESULT WaveBankReader::Impl::GetWaveData(uint32_t index, const uint8_t** pData,
10291032
#endif
10301033

10311034
if (!waveData)
1032-
return E_FAIL;
1035+
return E_POINTER;
10331036

10341037
if (m_data.dwFlags & BANKDATA::TYPE_STREAMING)
10351038
{
@@ -1223,10 +1226,13 @@ HRESULT WaveBankReader::Open(const wchar_t* szFileName) noexcept
12231226
_Use_decl_annotations_
12241227
uint32_t WaveBankReader::Find(const char* name) const
12251228
{
1226-
auto it = pImpl->m_names.find(name);
1227-
if (it != pImpl->m_names.cend())
1229+
if (name)
12281230
{
1229-
return it->second;
1231+
auto it = pImpl->m_names.find(name);
1232+
if (it != pImpl->m_names.cend())
1233+
{
1234+
return it->second;
1235+
}
12301236
}
12311237

12321238
return uint32_t(-1);

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ For a full change history, see [CHANGELOG.md](https://github.com/microsoft/Direc
8888

8989
* If you want to use XAudio2Redist with Windows 8.1, the CMake project supports this with the build option `BUILD_XAUDIO_REDIST`. The CMake build option `BUILD_XAUDIO_WIN7` was renamed.
9090

91+
* When using XAudio 2.8 for Windows 8.1, there is no xWMA format support. This is available when using XAudio 2.9 or XAudio2Redist.
92+
9193
* Starting with the February 2023 release, the Mouse class implementation of relative mouse movement was updated to accumulate changes between calls to ``GetState``. By default, each time you call ``GetState`` the deltas are reset which works for scenarios where you use relative movement but only call the method once per frame. If you call it more than once per frame, then add an explicit call to ``EndOfInputFrame`` to use an explicit reset model instead.
9294

9395
* As of the September 2022 release, the library makes use of C++11 inline namespaces for differing types that have the same names in the DirectX 11 and DirectX 12 version of the _DirectX Tool Kit_. This provides a link-unique name such as ``DirectX::DX11::SpriteBatch`` that will appear in linker output messages. In most use cases, however, there is no need to add explicit ``DX11`` namespace resolution in client code.
@@ -116,7 +118,7 @@ For a full change history, see [CHANGELOG.md](https://github.com/microsoft/Direc
116118

117119
## Support
118120

119-
For questions, consider using [Stack Overflow](https://stackoverflow.com/questions/tagged/directxtk) with the _directxtk_ tag, or the [DirectX Discord Server](https://discord.gg/directx) in the _dx9-dx11-developers_ channel.
121+
For questions, consider using [Stack Overflow](https://stackoverflow.com/questions/tagged/directxtk) with the _directxtk_ tag, or the [DirectX Discord Server](https://discord.gg/directx) in the _dx9-dx11-developers_ or _input-and-audio_ channels.
120122

121123
For bug reports and feature requests, please use GitHub [issues](https://github.com/microsoft/DirectXTK/issues) for this project.
122124

Src/BinaryReader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace DirectX
2727
explicit BinaryReader(_In_z_ wchar_t const* fileName) noexcept(false);
2828
BinaryReader(_In_reads_bytes_(dataSize) uint8_t const* dataBlob, size_t dataSize) noexcept;
2929

30+
BinaryReader(BinaryReader&&) noexcept;
31+
BinaryReader& operator= (BinaryReader&&) noexcept;
32+
3033
BinaryReader(BinaryReader const&) = delete;
3134
BinaryReader& operator= (BinaryReader const&) = delete;
3235

Src/DGSLEffectFactory.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class DGSLEffectFactory::Impl
3434
mDevice(device),
3535
mSharing(true),
3636
mForceSRGB(false)
37-
{}
37+
{
38+
if (!device)
39+
throw std::invalid_argument("Direct3D device is null");
40+
}
3841

3942
Impl(const Impl&) = delete;
4043
Impl& operator=(const Impl&) = delete;

Src/GraphicsMemory.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class GraphicsMemory::Impl
6464

6565
void Initialize(_In_ ID3D11DeviceX* device, unsigned int backBufferCount)
6666
{
67-
assert(device != nullptr);
67+
if (!device)
68+
throw std::invalid_argument("Direct3D device is null");
69+
6870
mDevice = device;
6971

7072
device->GetImmediateContextX(mDeviceContext.GetAddressOf());
@@ -245,9 +247,11 @@ class GraphicsMemory::Impl
245247
s_graphicsMemory = nullptr;
246248
}
247249

248-
void Initialize(_In_ ID3D11Device* device, unsigned int backBufferCount) noexcept
250+
void Initialize(_In_ ID3D11Device* device, unsigned int backBufferCount)
249251
{
250-
UNREFERENCED_PARAMETER(device);
252+
if (!device)
253+
throw std::invalid_argument("Direct3D device is null");
254+
251255
UNREFERENCED_PARAMETER(backBufferCount);
252256
}
253257

0 commit comments

Comments
 (0)