-
Notifications
You must be signed in to change notification settings - Fork 241
Fixes #3161: "Multiplication result converted to larger type" in client.cpp #3162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1257,15 +1257,15 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd ) | |
| if ( bMuteOutStream ) | ||
| { | ||
| iUnused = opus_custom_encode ( CurOpusEncoder, | ||
| &vecZeros[i * iNumAudioChannels * iOPUSFrameSizeSamples], | ||
| &vecZeros[static_cast<size_t> ( i * iNumAudioChannels * iOPUSFrameSizeSamples )], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this fixes it. The multiplication is still done at the smaller size, before casting the result to the larger size, which is already implicit in the To do it by casting, I think you only cast
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiplication will be done in the type of the first operant. Nevertheless if the result is assigned to a smaller type there should still be a check to handle any overflows. |
||
| iOPUSFrameSizeSamples, | ||
| &vecCeltData[0], | ||
| iCeltNumCodedBytes ); | ||
| } | ||
| else | ||
| { | ||
| iUnused = opus_custom_encode ( CurOpusEncoder, | ||
| &vecsStereoSndCrd[i * iNumAudioChannels * iOPUSFrameSizeSamples], | ||
| &vecsStereoSndCrd[static_cast<size_t> ( i * iNumAudioChannels * iOPUSFrameSizeSamples )], | ||
| iOPUSFrameSizeSamples, | ||
| &vecCeltData[0], | ||
| iCeltNumCodedBytes ); | ||
|
|
@@ -1311,7 +1311,7 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd ) | |
| iUnused = opus_custom_decode ( CurOpusDecoder, | ||
| pCurCodedData, | ||
| iCeltNumCodedBytes, | ||
| &vecsStereoSndCrd[i * iNumAudioChannels * iOPUSFrameSizeSamples], | ||
| &vecsStereoSndCrd[static_cast<size_t> ( i * iNumAudioChannels * iOPUSFrameSizeSamples )], | ||
| iOPUSFrameSizeSamples ); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should calculate an upper bound of these and check if that's valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i < iSndCrdFrameSizeFactorwhich is a small number (usually 1, 2 or 4).iNumAudioChannelsis 1 or 2.SYSTEM_FRAME_SIZE_SAMPLESis 64 andiOPUSFrameSizeSamplesis either that or twice that.So the result should be < 16K.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, which illustrates why the CodeQL warning is silly in a lot of contexts, including this one.
I have mentioned an alternative way to fix it in the comments on #3161, and have what I think is an even better way just compiling and about to push to a branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're more likely to see buffer overruns -- but we don't.
vecZerosandvecsStereoSndCrdare 2 *Sound.Init ( iPrefMonoFrameSize ), so likely to be < 16K as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
iSndCrdFrameSizeFactoris used to ensure the buffer overruns don't happen, in fact - calculated from theSound.Initresult.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CodeQL warnings we are addressing are nothing to do with buffer overruns. Just a perceived arithmetic overflow that will never happen with the values we are using.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
i,iNumAudioChannelsandiOPUSFrameSizeSamplesasint, however, MAX_INT * MAX_INT * MAX_INT would be out of bounds for the index type.