Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Convert type before multiplication
Browse files Browse the repository at this point in the history
Accounting for several CodeQL alerts. I'm tired now...

Signed-off-by: Avery King <[email protected]>
  • Loading branch information
generic-pers0n committed Aug 3, 2022
1 parent c78735d commit 6c1e159
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/ImageManipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ std::unique_ptr<wxImage> OverlayImage(wxImage * background, wxImage * foreground
//Make a NEW image the size of the background
auto dstImage = std::make_unique<wxImage>(bgWidth, bgHeight);
unsigned char *dst = dstImage->GetData();
memcpy(dst, bg, bgWidth * bgHeight * 3);
memcpy(dst, bg, static_cast<size_t>(bgWidth) * bgHeight * 3);


// Go through the foreground image bit by bit and mask it on to the
Expand Down Expand Up @@ -211,7 +211,7 @@ std::unique_ptr<wxImage> OverlayImage(teBmps eBack, teBmps eForeground,
//Make a NEW image the size of the background
auto dstImage = std::make_unique<wxImage>(bgWidth, bgHeight);
unsigned char *dst = dstImage->GetData();
memcpy(dst, bg, bgWidth * bgHeight * 3);
memcpy(dst, bg, static_cast<size_t>(bgWidth) * bgHeight * 3);

// If background image has tranparency, then we want to blend with the
// current background colour.
Expand Down
6 changes: 3 additions & 3 deletions src/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(blockRMS) * blockRMS * fileLen;
length += fileLen;
}

Expand All @@ -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<double>(partialRMS) * partialRMS * l0;
length += l0;
}

Expand All @@ -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<double>(partialRMS) * partialRMS * l0;
length += l0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/SpectrumAnalyst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(mWindowSize) / mRate;
bin1 = freq1 * static_cast<float>(mWindowSize) / mRate;
} else {
bin0 = freq0 * mRate;
bin1 = freq1 * mRate;
Expand Down
14 changes: 9 additions & 5 deletions src/TrackPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(image.GetWidth()) * image.GetHeight());

{
std::unique_ptr< wxGraphicsContext >
Expand All @@ -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(),
Expand Down Expand Up @@ -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;

Expand Down
24 changes: 12 additions & 12 deletions src/VoiceKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(buffer[i]) * buffer[i];
}

len -= block;
s += block;
}

return sum / originalLen.as_double();
}

Expand Down
6 changes: 5 additions & 1 deletion src/WaveTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(cliprms) *
static_cast<double>(cliprms) *
(clipEnd - clipStart).as_double();
length += (clipEnd - clipStart);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/effects/Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(value) * value;
mRMSSum += mCircle[mCirclePos];
level = sqrt(mRMSSum/mCircleSize);
mCirclePos = (mCirclePos+1)%mCircleSize;
Expand Down
2 changes: 1 addition & 1 deletion src/effects/Contrast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(rms) * rms;
}
// TODO: This works for stereo, provided the audio clips are in both channels.
// We should really count gaps between clips as silence.
Expand Down
2 changes: 1 addition & 1 deletion src/prefs/SpectrogramSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(windowSize) * ((algorithm != algPitchEAC) ? zeroPaddingFactor : 1);
#endif
}

Expand Down

0 comments on commit 6c1e159

Please sign in to comment.