From 10b231fb7193559f6036afbd6cedffdfbce24c68 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:20:13 -0600 Subject: [PATCH] silent_audio: volume setters refuse NaN; master volume and mute under the lock SimpleVolume::SetMasterVolume, StreamVolume::SetChannelVolume and StreamVolume::SetAllVolumes tested `level < 0 || level > 1`, which is false for NaN, so a NaN level was stored (kevincommits' finding in #1608). They now share volume_level(), which accepts only 0 <= level <= 1, as SpatialObject::SetVolume in the same file already does. SimpleVolume also read and wrote AudioClient::master_ and mute_ without the client's lock, unlike the channel volumes. They are now private and reached through locked accessors. silent_audio_test gains checks that each setter refuses NaN and keeps the previous level (3 fail with the old test), plus a mute round trip. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/silent_audio.cpp | 38 +++++++++++++++++++++++------- runtime/test/silent_audio_test.cpp | 21 +++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/runtime/shims/silent_audio.cpp b/runtime/shims/silent_audio.cpp index 0e02186bb3..beac65a13e 100644 --- a/runtime/shims/silent_audio.cpp +++ b/runtime/shims/silent_audio.cpp @@ -415,8 +415,23 @@ class AudioClient final : public Unknown { Lock l(lock_); if (i < kMaxChannels) channel_volume_[i] = v; } - float master_ = 1.0f; - BOOL mute_ = FALSE; + // ISimpleAudioVolume's state, under the same lock as the channel volumes. + float master() { + Lock l(lock_); + return master_; + } + void set_master(float v) { + Lock l(lock_); + master_ = v; + } + BOOL mute() { + Lock l(lock_); + return mute_; + } + void set_mute(BOOL m) { + Lock l(lock_); + mute_ = m; + } private: struct Lock { @@ -511,6 +526,8 @@ class AudioClient final : public Unknown { uint64_t played_at_anchor_ = 0; int64_t anchor_qpc_ = 0; float channel_volume_[kMaxChannels]{}; + float master_ = 1.0f; + BOOL mute_ = FALSE; }; // The services hold their client. @@ -551,26 +568,29 @@ class Clock final : public Service { } }; +// A volume level in [0, 1]. Written so NaN fails too: `level < 0 || level > 1` is false for NaN. +bool volume_level(float level) { return level >= 0.0f && level <= 1.0f; } + class SimpleVolume final : public Service { public: using Service::Service; HRESULT STDMETHODCALLTYPE SetMasterVolume(float level, LPCGUID) override { - if (level < 0.0f || level > 1.0f) return E_INVALIDARG; - client_->master_ = level; + if (!volume_level(level)) return E_INVALIDARG; + client_->set_master(level); return S_OK; } HRESULT STDMETHODCALLTYPE GetMasterVolume(float* level) override { if (!level) return E_POINTER; - *level = client_->master_; + *level = client_->master(); return S_OK; } HRESULT STDMETHODCALLTYPE SetMute(BOOL mute, LPCGUID) override { - client_->mute_ = mute; + client_->set_mute(mute); return S_OK; } HRESULT STDMETHODCALLTYPE GetMute(BOOL* mute) override { if (!mute) return E_POINTER; - *mute = client_->mute_; + *mute = client_->mute(); return S_OK; } }; @@ -584,7 +604,7 @@ class StreamVolume final : public Service { return S_OK; } HRESULT STDMETHODCALLTYPE SetChannelVolume(UINT32 index, const float level) override { - if (index >= client_->channels() || level < 0.0f || level > 1.0f) return E_INVALIDARG; + if (index >= client_->channels() || !volume_level(level)) return E_INVALIDARG; client_->set_volume(index, level); return S_OK; } @@ -598,7 +618,7 @@ class StreamVolume final : public Service { if (!levels) return E_POINTER; if (count != client_->channels()) return E_INVALIDARG; for (UINT32 i = 0; i < count; ++i) { - if (levels[i] < 0.0f || levels[i] > 1.0f) return E_INVALIDARG; + if (!volume_level(levels[i])) return E_INVALIDARG; } for (UINT32 i = 0; i < count; ++i) client_->set_volume(i, levels[i]); return S_OK; diff --git a/runtime/test/silent_audio_test.cpp b/runtime/test/silent_audio_test.cpp index eac3121b9d..a786be8b4c 100644 --- a/runtime/test/silent_audio_test.cpp +++ b/runtime/test/silent_audio_test.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "shims.h" #include "silent_audio.h" @@ -424,11 +425,31 @@ void test_render_stream() { if (!render || !clock || !simple || !stream) return; UINT32 channels = 0; float level = 0.0f; + BOOL muted = FALSE; check(stream->GetChannelCount(&channels) == S_OK && channels == 8 && stream->SetChannelVolume(7, 0.5f) == S_OK && stream->GetChannelVolume(7, &level) == S_OK && level == 0.5f, "IAudioStreamVolume keeps a channel volume"); check(simple->SetMasterVolume(0.25f, nullptr) == S_OK && simple->GetMasterVolume(&level) == S_OK && level == 0.25f, "ISimpleAudioVolume keeps the master volume"); + // NaN is no volume (kevincommits' finding in #1608): `level < 0 || level > 1` let it through and stored it. + const float nan = std::numeric_limits::quiet_NaN(); + check(simple->SetMasterVolume(nan, nullptr) == E_INVALIDARG && simple->GetMasterVolume(&level) == S_OK && + level == 0.25f, + "SetMasterVolume(NaN) is refused and keeps the master volume"); + check(stream->SetChannelVolume(7, nan) == E_INVALIDARG && stream->GetChannelVolume(7, &level) == S_OK && + level == 0.5f, + "SetChannelVolume(NaN) is refused and keeps the channel volume"); + { + float before = -1.0f; + stream->GetChannelVolume(0, &before); + float levels[8] = {0.125f, 0.125f, 0.125f, 0.125f, 0.125f, 0.125f, 0.125f, nan}; + check(stream->SetAllVolumes(8, levels) == E_INVALIDARG && stream->GetChannelVolume(0, &level) == S_OK && + level == before && before != 0.125f, + "SetAllVolumes with a NaN level is refused and sets none"); + } + check(simple->SetMute(TRUE, nullptr) == S_OK && simple->GetMute(&muted) == S_OK && muted == TRUE && + simple->SetMute(FALSE, nullptr) == S_OK && simple->GetMute(&muted) == S_OK && muted == FALSE, + "ISimpleAudioVolume keeps the mute state"); UINT64 freq = 0; check(clock->GetFrequency(&freq) == S_OK && freq == 48000, "IAudioClock counts frames at 48 kHz");