Skip to content

Commit

Permalink
Add more HRESULT checks
Browse files Browse the repository at this point in the history
On multiple places, there is an interaction with endpointVolume and the function is expected to return something, 
but the return value of endpointVolume is not checked. Therefore, undefined values may be returned.
This commit adds return value checks with default values as return values for failing endpointVolume interactions.
This should fix null pointer dereference issues.
  • Loading branch information
JJ-8 authored Mar 2, 2024
1 parent 344ab7b commit a7808ea
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cppsrc/win/win-sound-mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,22 @@ void Device::SetMute(bool volume)
float Device::GetVolume()
{
float volume;
endpointVolume->GetMasterVolumeLevelScalar(&volume);
HRESULT res = endpointVolume->GetMasterVolumeLevelScalar(&volume);
if (res != S_OK) {
return 0.F;
}
return volume;
}

bool Device::GetMute()
{
BOOL mute;
endpointVolume->GetMute(&mute);
HRESULT res = endpointVolume->GetMute(&mute);
if (res != S_OK) {
// if we can't retrieve the GetMute state, there is probably something wrong with the audio endpoint
// and the user can't hear it anyway, so just return true for the mute state.
return true;
}
return (bool)mute;
}

Expand Down Expand Up @@ -423,9 +431,17 @@ VolumeBalance Device::GetVolumeBalance()
return result;
}
result.stereo = true;
endpointVolume->GetChannelVolumeLevelScalar(RIGHT, &result.right);
endpointVolume->GetChannelVolumeLevelScalar(LEFT, &result.left);
HRESULT res1 = endpointVolume->GetChannelVolumeLevelScalar(RIGHT, &result.right);
if (res1 != S_OK) {
result.right = 0.F; // revert right volume
return result;
}

HRESULT res2 = endpointVolume->GetChannelVolumeLevelScalar(LEFT, &result.left);
if (res2 != S_OK) {
result.left = 0.F; // revert left volume
return result;
}
return result;
}

Expand Down

0 comments on commit a7808ea

Please sign in to comment.