From 6c1e15947086ede7a43079a1ac531a0fc6578279 Mon Sep 17 00:00:00 2001 From: Avery King Date: Tue, 2 Aug 2022 21:35:32 -0700 Subject: [PATCH] Convert type before multiplication Accounting for several CodeQL alerts. I'm tired now... Signed-off-by: Avery King --- src/ImageManipulation.cpp | 4 ++-- src/Sequence.cpp | 6 +++--- src/SpectrumAnalyst.cpp | 4 ++-- src/TrackPanel.cpp | 14 +++++++++----- src/VoiceKey.cpp | 24 ++++++++++++------------ src/WaveTrack.cpp | 6 +++++- src/effects/Compressor.cpp | 2 +- src/effects/Contrast.cpp | 2 +- src/prefs/SpectrogramSettings.cpp | 2 +- 9 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/ImageManipulation.cpp b/src/ImageManipulation.cpp index 38cab933a0..04ec3e397d 100644 --- a/src/ImageManipulation.cpp +++ b/src/ImageManipulation.cpp @@ -136,7 +136,7 @@ std::unique_ptr OverlayImage(wxImage * background, wxImage * foreground //Make a NEW image the size of the background auto dstImage = std::make_unique(bgWidth, bgHeight); unsigned char *dst = dstImage->GetData(); - memcpy(dst, bg, bgWidth * bgHeight * 3); + memcpy(dst, bg, static_cast(bgWidth) * bgHeight * 3); // Go through the foreground image bit by bit and mask it on to the @@ -211,7 +211,7 @@ std::unique_ptr OverlayImage(teBmps eBack, teBmps eForeground, //Make a NEW image the size of the background auto dstImage = std::make_unique(bgWidth, bgHeight); unsigned char *dst = dstImage->GetData(); - memcpy(dst, bg, bgWidth * bgHeight * 3); + memcpy(dst, bg, static_cast(bgWidth) * bgHeight * 3); // If background image has tranparency, then we want to blend with the // current background colour. diff --git a/src/Sequence.cpp b/src/Sequence.cpp index 35d6fefbd6..d357d402c2 100644 --- a/src/Sequence.cpp +++ b/src/Sequence.cpp @@ -324,7 +324,7 @@ float Sequence::GetRMS(sampleCount start, sampleCount len, bool mayThrow) const const auto fileLen = sb->GetSampleCount(); const auto blockRMS = results.RMS; - sumsq += blockRMS * blockRMS * fileLen; + sumsq += static_cast(blockRMS) * blockRMS * fileLen; length += fileLen; } @@ -344,7 +344,7 @@ float Sequence::GetRMS(sampleCount start, sampleCount len, bool mayThrow) const auto results = sb->GetMinMaxRMS(s0, l0, mayThrow); const auto partialRMS = results.RMS; - sumsq += partialRMS * partialRMS * l0; + sumsq += static_cast(partialRMS) * partialRMS * l0; length += l0; } @@ -358,7 +358,7 @@ float Sequence::GetRMS(sampleCount start, sampleCount len, bool mayThrow) const auto results = sb->GetMinMaxRMS(0, l0, mayThrow); const auto partialRMS = results.RMS; - sumsq += partialRMS * partialRMS * l0; + sumsq += static_cast(partialRMS) * partialRMS * l0; length += l0; } diff --git a/src/SpectrumAnalyst.cpp b/src/SpectrumAnalyst.cpp index 529724ef3f..b0489a3ba9 100644 --- a/src/SpectrumAnalyst.cpp +++ b/src/SpectrumAnalyst.cpp @@ -352,8 +352,8 @@ float SpectrumAnalyst::GetProcessedValue(float freq0, float freq1) const float bin0, bin1, binwidth; if (mAlg == Spectrum) { - bin0 = freq0 * mWindowSize / mRate; - bin1 = freq1 * mWindowSize / mRate; + bin0 = freq0 * static_cast(mWindowSize) / mRate; + bin1 = freq1 * static_cast(mWindowSize) / mRate; } else { bin0 = freq0 * mRate; bin1 = freq1 * mRate; diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 67f97f6af1..fb9f21e184 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -1150,10 +1150,11 @@ void DrawTrackName( }; wxImage image( textWidth + MarginsX + SecondMarginsX, - textHeight + MarginsY + SecondMarginsY ); + textHeight + MarginsY + SecondMarginsY + ); image.InitAlpha(); unsigned char *alpha=image.GetAlpha(); - memset(alpha, wxIMAGE_ALPHA_TRANSPARENT, image.GetWidth()*image.GetHeight()); + memset(alpha, wxIMAGE_ALPHA_TRANSPARENT, static_cast(image.GetWidth()) * image.GetHeight()); { std::unique_ptr< wxGraphicsContext > @@ -1164,12 +1165,14 @@ void DrawTrackName( // Draw at 1,1, not at 0,0 to avoid clipping of the antialiasing. gc.DrawRoundedRectangle( SecondMarginX, SecondMarginY, - textWidth + MarginsX, textHeight + MarginsY, 8.0 ); + textWidth + MarginsX, textHeight + MarginsY, 8.0 + ); // destructor of gc updates the wxImage. } wxBitmap bitmap( image ); dc.DrawBitmap( bitmap, - nameRect.x - SecondMarginX, nameRect.y - SecondMarginY ); + nameRect.x - SecondMarginX, nameRect.y - SecondMarginY + ); #endif dc.SetTextForeground(theTheme.Colour( clrTrackPanelText )); dc.DrawText(t->GetName(), @@ -1229,7 +1232,8 @@ struct EmptyCell final : CommonTrackPanelCell { TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass ) override { - if ( iPass == TrackArtist::PassMargins ) { + if ( iPass == TrackArtist::PassMargins ) + { // Draw a margin area of TrackPanel auto dc = &context.dc; diff --git a/src/VoiceKey.cpp b/src/VoiceKey.cpp index db4e771042..8dfa76ec3c 100644 --- a/src/VoiceKey.cpp +++ b/src/VoiceKey.cpp @@ -866,22 +866,22 @@ double VoiceKey::TestEnergy ( Floats buffer{ blockSize }; //Get a sampling buffer while(len > 0) - { - //Figure out how much to grab - auto block = limitSampleBufferSize ( t.GetBestBlockSize(s), len ); - - t.GetFloats(buffer.get(), s,block); //grab the block; + { + //Figure out how much to grab + auto block = limitSampleBufferSize ( t.GetBestBlockSize(s), len ); - //Now, go through the block and calculate energy - for(decltype(block) i = 0; i< block; i++) - { - sum += buffer[i]*buffer[i]; - } + t.GetFloats(buffer.get(), s,block); //grab the block; - len -= block; - s += block; + //Now, go through the block and calculate energy + for(decltype(block) i = 0; i< block; i++) + { + sum += static_cast(buffer[i]) * buffer[i]; } + len -= block; + s += block; + } + return sum / originalLen.as_double(); } diff --git a/src/WaveTrack.cpp b/src/WaveTrack.cpp index 992bc9283f..64278d0a1f 100644 --- a/src/WaveTrack.cpp +++ b/src/WaveTrack.cpp @@ -1875,7 +1875,11 @@ float WaveTrack::GetRMS(double t0, double t1, bool mayThrow) const clip->TimeToSamplesClip(wxMax(t0, clip->GetStartTime()), &clipStart); clip->TimeToSamplesClip(wxMin(t1, clip->GetEndTime()), &clipEnd); - sumsq += cliprms * cliprms * (clipEnd - clipStart).as_float(); + // GP: trying to fix conversion after multiplication. The 2nd static_cast + // is probably unnecessary, but I'm tired... + sumsq += static_cast(cliprms) * + static_cast(cliprms) * + (clipEnd - clipStart).as_double(); length += (clipEnd - clipStart); } } diff --git a/src/effects/Compressor.cpp b/src/effects/Compressor.cpp index 6455fcf47b..f2e7337b4f 100644 --- a/src/effects/Compressor.cpp +++ b/src/effects/Compressor.cpp @@ -521,7 +521,7 @@ float EffectCompressor::AvgCircle(float value) // Calculate current level from root-mean-squared of // circular buffer ("RMS") mRMSSum -= mCircle[mCirclePos]; - mCircle[mCirclePos] = value*value; + mCircle[mCirclePos] = static_cast(value) * value; mRMSSum += mCircle[mCirclePos]; level = sqrt(mRMSSum/mCircleSize); mCirclePos = (mCirclePos+1)%mCircleSize; diff --git a/src/effects/Contrast.cpp b/src/effects/Contrast.cpp index 00bb8bbc4b..4b5321c5ef 100644 --- a/src/effects/Contrast.cpp +++ b/src/effects/Contrast.cpp @@ -115,7 +115,7 @@ bool ContrastDialog::GetDB(float &dB) // Don't throw in this analysis dialog rms = t->GetRMS(mT0, mT1, false); - meanSq += rms * rms; + meanSq += static_cast(rms) * rms; } // TODO: This works for stereo, provided the audio clips are in both channels. // We should really count gaps between clips as silence. diff --git a/src/prefs/SpectrogramSettings.cpp b/src/prefs/SpectrogramSettings.cpp index c71b0e3bf0..35f3c21578 100644 --- a/src/prefs/SpectrogramSettings.cpp +++ b/src/prefs/SpectrogramSettings.cpp @@ -603,7 +603,7 @@ size_t SpectrogramSettings::GetFFTLength() const #ifndef EXPERIMENTAL_ZERO_PADDED_SPECTROGRAMS return windowSize; #else - return windowSize * ((algorithm != algPitchEAC) ? zeroPaddingFactor : 1); + return static_cast(windowSize) * ((algorithm != algPitchEAC) ? zeroPaddingFactor : 1); #endif }